Iterate through Dynamic Sql...

Last Post 14 Jan 2008 06:45 AM by nosepicker. 1 Replies.
AddThis - Bookmarking and Sharing Button
Author Messages
kj2w2ndAccount
New Member
New Member

--
14 Jan 2008 06:10 AM
Ok i have figured out how to use a dynamic variable with relation to an 'IN' clause:

DECLARE @strInClause varchar(255)
declare @SQL nvarchar(4000)

SET @strInClause = '1, 13'

set @SQL = '
SELECT
Col1
Types_FK
FROM
dbo.tbl1 d inner join
dbo.tbl2 x on d.ColX = x.ColX
WHERE
Col3 in
(' + @strInClause + ')'

exec sp_executesql @SQL

With a normal static select statement I would use a cursor to look at the results rows by rows, but is it possible to use a cursor with this 'exec sp_executesql @SQL'?

J
nosepicker
New Member
New Member

--
14 Jan 2008 06:45 AM
Yes, but I'm not sure why you'd want to (it would probably be less efficient). What do you need to do?


Acceptable Use Policy
---