如何在SQLServer 2005中将where子句作为参数传递 [英] How to pass where clause as a parameter in sqlserver 2005
本文介绍了如何在SQLServer 2005中将where子句作为参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的要求是,根据我在Singe SP中的情况,where子句是不同的.这样我就准备了C#中的where子句,如果我的Sp传递了Where子句作为输入参数.如何将select语句与where条件(这里的condition是我的输入参数)绑定在一起
My requirement is where clause is different based in my condition in singe SP. So that i prepared where clause in c# and i passed that Where Clause as input parameter if my Sp. how to bind select statement with where condition(Here where condition is my input parameter)
推荐答案
除非您可以编写一个足以处理所有可能条件的SP,您将必须在某个地方构建SQL字符串.
最简单的方法是在C#中构建它并从那里运行.
如果必须使用SP,则必须在SQL中构建一个语句字符串,然后使用EXEC语句执行它:
http://msdn.microsoft.com/en-us/library/ms188332.aspx [ ^ ]
尼克
Unless you can write a SP that is generic enough to handle all possible conditions, you will have to build a SQL string somewhere.
The easiest way is to build it in C# and run it from there.
If you must use a SP, you will have to build a statement string in SQL and execute it using the EXEC statement:
http://msdn.microsoft.com/en-us/library/ms188332.aspx[^]
Nick
拉梅什,
我认为这对您有帮助.
创建过程DynamicWhereClause
(
@input varchar(100)
)
AS
开始
宣告@sqlCommand varchar(1000)
SET @sqlCommand =``SELECT * FROM Employee''+ @输入
EXEC(@sqlCommand)
END
例子
Hi Ramesh,
i think it helpful for you.
CREATE PROCEDURE DynamicWhereClause
(
@input varchar(100)
)
AS
BEGIN
DECLARE @sqlCommand varchar(1000)
SET @sqlCommand = ''SELECT * FROM Employee '' + @input
EXEC (@sqlCommand)
END
Examples
EX 1): EXEC DynamicWhereClause @input = "WHERE FirstName = 'venky' AND LastName = 'palepu' AND Country = 'India' "
EX 2):EXEC DynamicWhereClause @input = "WHERE FirstName = 'venky' "
谢谢
venkat
Thanks
venkat
这篇关于如何在SQLServer 2005中将where子句作为参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文