September 21, 2007 at 3:50 am
CREATE TABLE #tmpt1
(
Col1TINYINTNOT NULL IDENTITY(1,1),
Col2TINYINT
)
INSERT#tmpt1(Col2)
SELECT1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5
DECLARE @a VARCHAR(100)
SET @a = '1, 2, 3'
UPDATE #tmpt1 SET col2 = 10 WHERE col1 in ( @a )
Now my query is, I want to update the table with search criteria provided in string format. This can be done using dynamic query but is there is any solutions without Dynamic Query..
Abhijit More.
September 21, 2007 at 6:21 am
look in the script contributions here on SSC and find the split() function. it takes a string, and converts it to a table based on the delimiter.
select dbo.split(@a,',') would return a table witht eh values, so you can use your IN() sdtatment:
UPDATE #tmpt1 SET col2 = 10 WHERE col1 in (SELECTdbo.split(@a,',') )
Lowell
September 21, 2007 at 6:53 am
thank you lowell, for your reply. but i want to do it without creating additional functions / SP.
is it possible...
September 21, 2007 at 6:56 am
yes...but you have to take the same code from that function, which uses charindex and other stuff, and incorporate it into your query.
That makes it harder to read, but that's how to do it in order to do it inline. otherwise, it's dynamic SQL.
Lowell
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply