Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi all, i know this is stupid question, but the fact is i'm stuck with it over the past 1 hour trying to figure out how to do it.
ok, here is my situation,

for example, i'm trying to pass in a parameter to a store proc with the select statement like below:

SQL
DECLARE @strA VARCHAR(20) = '''1'',''2'''
 
select * from tableA
where ColA in (   @strA )


it executed with no error, no result...which is wrong because there should be result return, i tested the query..

SQL
select * from tableA
where ColA in ('1','2')


please advise what is wrong with my store proc. thanks in advance.
Posted
Updated 21-Jun-16 0:13am
v2
Comments
Maciej Los 21-Jun-16 5:46am    
Does the second query returns any data?
kitproj 21-Jun-16 6:06am    
yes, it does. that's why i think is the single quote that causing the result not showing.

1 solution

You have to use dynamic sql if you are going to pass the data through like that
e.g.
SQL
DECLARE @strA VARCHAR(20) = '''1'',''2'''
 
DECLARE @sql NVARCHAR(MAX) = 'select * from tableA where ColA in (' + @strA + ')'
EXEC sp_sqlexec @sql
 
Share this answer
 
Comments
Maciej Los 21-Jun-16 6:17am    
5ed!
kitproj 22-Jun-16 21:06pm    
thanks Chill60.

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