我可以在一个参数中传递整个where条件吗?

我可以在一个参数中传递整个where条件吗?

问题描述:

我可以在一个参数中传递整个where条件吗?



喜欢:

Can I pass the entire where condition in one parameter?

Like:

select * from tbl_customer where + @conditions



和条件如:


and conditions like:

@condition= "customerid=7 and address=bangalore and phone=9988776655"



我可以像这样使用吗?它有什么选择吗?



它在我身边显示错误!


Can I use like this? Is there any option for it?

It is showing error in my side!





试试这个...实际问题是你正在比较像班加罗尔这样的VARCHAR值,而且没有单引号。



Hi,

Try This...Actually the problem is You are comparing VARCHAR Values like 'Bangalore' and all without single Quote.

DECLARE @SQLQuery VARCHAR(3000), @conditions VARCHAR(1000)
SELECT @conditions= 'customerid=7 and address=CHAR(39)+bangalore+CHAR(39) and phone=9988776655'
SELECT @SQLQuery='select * from tbl_customer where '+@conditions
PRINT @SQLQuery
EXEC(@SQLQuery)
-- select * from tbl_customer where customerid=7 and address=CHAR(39)+bangalore+CHAR(39) and phone=9988776655



检查以下链接...答案或在Code项目文章中搜索。

http://www.codeproject.com/script/Answers/ Post.aspx?aid = 597000 [ ^ ]

问候,

GVPrabu


Check the following links...Answers or search in Code project articles.
http://www.codeproject.com/script/Answers/Post.aspx?aid=597000[^]
Regards,
GVPrabu


是的你可以但为了做到这一点你必须使用动态sql。使用sp_executesql进行动态SQL执行。查看此网址



http:// msdn .microsoft.com / zh-CN / library / ms188001.aspx [ ^ ]
Yes you can but in order to do that you have to use dynamic sql. Use sp_executesql for dynamic SQL execution. Check this url

http://msdn.microsoft.com/en-us/library/ms188001.aspx[^]