{"id":37,"date":"2006-02-14T16:30:27","date_gmt":"2006-02-14T21:30:27","guid":{"rendered":"http:\/\/stein.everybody.org\/brain\/sql-server-t-sql-stored-procedure-in-clause-list-from-parameter\/"},"modified":"2011-03-10T10:57:34","modified_gmt":"2011-03-10T15:57:34","slug":"sql-server-t-sql-stored-procedure-in-clause-list-from-parameter","status":"publish","type":"post","link":"https:\/\/jeremystein.com\/brain\/sql-server-t-sql-stored-procedure-in-clause-list-from-parameter\/","title":{"rendered":"SQL Server T-SQL stored procedure in-clause list from parameter"},"content":{"rendered":"<p>I tried to do this in a T-SQL stored procedure:<\/p>\n<p><code>create procedure myproc<br \/>\n@list nvarchar(128)<br \/>\nas<br \/>\n...<br \/>\nselect *<br \/>\nfrom something<br \/>\nwhere id in (@list)<\/code><\/p>\n<p>Which, of course, you can&#8217;t do.  In the where clause, SQL Server attempts to convert @list into an int, since it appears that I&#8217;m checking for id in a list of one int.  The solution is to do something like this:<\/p>\n<p><code>create procedure myproc<br \/>\n@list nvarchar(128)<br \/>\nas<br \/>\n...<br \/>\nselect *<br \/>\nfrom something<br \/>\nwhere charindex(',' + cast(id as varchar) + ',', ',' + @list + ',') > 0)<br \/>\n<\/code><\/p>\n<p>I got that from <a href=\"http:\/\/www.bizdatasolutions.com\/tsql\/sqlarrays.asp\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I tried to do this in a T-SQL stored procedure: create procedure myproc @list nvarchar(128) as &#8230; select * from something where id in (@list) Which, of course, you can&#8217;t do. In the where clause, SQL Server attempts to convert @list into an int, since it appears that I&#8217;m checking for id in a list [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-37","post","type-post","status-publish","format-standard","hentry","category-sql-server"],"_links":{"self":[{"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/posts\/37","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/comments?post=37"}],"version-history":[{"count":0,"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/posts\/37\/revisions"}],"wp:attachment":[{"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/media?parent=37"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/categories?post=37"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jeremystein.com\/brain\/wp-json\/wp\/v2\/tags?post=37"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}