Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 sp_executesql with CSV in IN clause

Author  Topic 

simondeutsch
Aged Yak Warrior

547 Posts

Posted - 2002-12-09 : 23:12:33
I am having a hard time using sp_executesql with this kind of query:
N'INSERT INTO #Temp SELECT * FROM Table WHERE Code IN(@Code)'....
(I have the syntax correct, but not posting it fully for brevity.)
and @Code should be a CSV of strings, which normally would translate as
...SELECT * FROM Table WHERE Code IN('AA','BB','CC')
but I can't figure out how to assign @Code it should run properly. Have tried:
@Code = '''AA'',''BB'',CC'''
@Code = 'AA'',''BB'',''CC'
@Code = 'AA,BB,CC'
Any which way returns no rows, although running the query non-dynamically returns data. @Code is VARCHAR(1000).

Sarah Berger MCSD

harshal_in
Aged Yak Warrior

633 Posts

Posted - 2002-12-10 : 00:21:42
quote:

I am having a hard time using sp_executesql with this kind of query:
N'INSERT INTO #Temp SELECT * FROM Table WHERE Code IN(@Code)'....
(I have the syntax correct, but not posting it fully for brevity.)
and @Code should be a CSV of strings, which normally would translate as
...SELECT * FROM Table WHERE Code IN('AA','BB','CC')
but I can't figure out how to assign @Code it should run properly. Have tried:
@Code = '''AA'',''BB'',CC'''
@Code = 'AA'',''BB'',''CC'
@Code = 'AA,BB,CC'
Any which way returns no rows, although running the query non-dynamically returns data. @Code is VARCHAR(1000).

Sarah Berger MCSD




i think this should work:
N'INSERT INTO #Temp SELECT * FROM Table WHERE Code IN('+@Code+')'....

Go to Top of Page

simondeutsch
Aged Yak Warrior

547 Posts

Posted - 2002-12-10 : 10:43:28
Actually, QA doesn't exactly like this code either.
quote:
i think this should work:
N'INSERT INTO #Temp SELECT * FROM Table WHERE Code IN('+@Code+')'....

rather, you must put the whole Insert string into a variable, and pass the variable to sp_executesql. But now it does work. Thanks!

Sarah Berger MCSD
Go to Top of Page
   

- Advertisement -