Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can I pass the entire where condition in one parameter?

Like:
SQL
select * from tbl_customer where + @conditions

and conditions like:
SQL
@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!
Posted
Updated 4-Jul-13 23:46pm
v3
Comments
Sudhakar Shinde 5-Jul-13 5:51am    
As long as you are taking care of syntax you can pass single parameter.However, it will not make any difference to execution of query. You pass as a single parameter or multiple parameters execution does not get affected. Share your error details to answer you correctly.

Hi,

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

SQL
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

Check the following links...Answers or search in Code project articles.
http://www.codeproject.com/script/Answers/Post.aspx?aid=597000[^]
Regards,
GVPrabu
 
Share this answer
 
v3
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[^]
 
Share this answer
 
Comments
gvprabu 5-Jul-13 6:07am    
Give some samples.... for his issue is some thing different... he is comparing address=bangalore like that, It should be address='bangalore' right.
Member 9492907 5-Jul-13 6:35am    
i just gve it for example..actually i am passing all integers...that is not the issue

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900