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
 Transact-SQL (2000)
 WHERE someField IN (@test)

Author  Topic 

Zath
Constraint Violating Yak Guru

298 Posts

Posted - 2009-07-22 : 09:07:32
A parameter is a list, or string, of ints.

Doing a simple select using IN but this breaks....

DECLARE @test VARCHAR(256)
SET @test = '1303, 1308, 1309, 1305, 1311, 1307, 1313, 1306, 1312 '

SELECT * FROM myTable WHERE myIntField IN (@test)

Syntax error converting the varchar value '1303, 1308, 1309, 1305, 1311, 1307, 1313, 1306, 1312 ' to a column of data type int.

Should there be a convert here?

Thanks,

Zath

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-07-22 : 09:12:05
http://vyaskn.tripod.com/passing_arrays_to_stored_procedures.htm
http://www.sommarskog.se/arrays-in-sql-2000.html


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-07-23 : 00:19:31
try any one of these

DECLARE @test VARCHAR(256)
SET @test = '1303, 1308, 1309, 1305, 1311, 1307, 1313, 1306, 1312 '

SELECT * FROM myTable where '%,' + @test+ ',%' LIKE '%,' + CAST( myIntField AS VARCHAR(255)) +',%'

exec('select * from mytable where CAST( myIntField AS VARCHAR(255)) in('+@test+')')

SELECT * FROM myTable where patindex('%,' + CAST( empid AS VARCHAR(255)) +',%',','+@test+',' )>0
Go to Top of Page
   

- Advertisement -