Friday, March 9, 2012

Possible to have a sql query like this...

Say i have a string formatted as...

s = 32,45,2,13,4

in my SQL stored procedure... i want to do something like...

SELECT * FROM t1 WHERE t1.a IN s

I know that will not work with the comma character seperating each value...but is there a way to do it so that it work? i dont think there is...but i just want to make sure.This would work:


CREATE PROCEDURE GetProductsByID
(
@.s varchar(255)
)

AS

DECLARE @.SELECT varchar(500)
SET @.SELECT = 'SELECT * FROM Products WHERE Products.ProductID IN (' + @.s + ')'

EXEC(@.SELECT)

|||do you know why it has to be done this way? would it have worked if a delimiter was | rather than ,?

and say we have 33,23,45,65

will that also return rows wid ID's of 3, b/c of 33?

No comments:

Post a Comment