Unix Technical Forum

Passing an IN (a, b, c) list to a sproc as a string -- best method?

This is a discussion on Passing an IN (a, b, c) list to a sproc as a string -- best method? within the SQL Server forums, part of the Microsoft SQL Server category; --> I want to do something like this in a stored proc: ------ Create Procedure dbo.GetPatients @PatientIdList varchar(200) -- comma ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 08:26 PM
Joel Thornton
 
Posts: n/a
Default Passing an IN (a, b, c) list to a sproc as a string -- best method?

I want to do something like this in a stored proc:

------

Create Procedure dbo.GetPatients
@PatientIdList varchar(200) -- comma separated list of PatientIDs
As

Select *
From Patients
Where PatientId In (@PatientIdList)

------

I know the above won't work, but of course what I want is if
@PatientIdList = '1,2,3' then I want Patient records with PatientIds
1, 2, and 3 returned.

It looks like the only way to do this is to build the SQL statement as
a string within the stored procedure ... which pretty much defeats the
usefulness of using precompiled sprocs as I understand it (better off
building a dynamic query against a View in that case).


Thoughts?

Joel Thornton ~ <groups@joelpt.eml.cc>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 08:26 PM
oj
 
Posts: n/a
Default Re: Passing an IN (a, b, c) list to a sproc as a string -- best method?

Joel,

Erland has a decent writing on this topic.

http://www.sommarskog.se/arrays-in-sql.html

--
-oj
http://www.rac4sql.net


"Joel Thornton" <joelpt@eml.cc> wrote in message
news:c190a45a.0401072012.5c38ba06@posting.google.c om...
> I want to do something like this in a stored proc:
>
> ------
>
> Create Procedure dbo.GetPatients
> @PatientIdList varchar(200) -- comma separated list of PatientIDs
> As
>
> Select *
> From Patients
> Where PatientId In (@PatientIdList)
>
> ------
>
> I know the above won't work, but of course what I want is if
> @PatientIdList = '1,2,3' then I want Patient records with PatientIds
> 1, 2, and 3 returned.
>
> It looks like the only way to do this is to build the SQL statement as
> a string within the stored procedure ... which pretty much defeats the
> usefulness of using precompiled sprocs as I understand it (better off
> building a dynamic query against a View in that case).
>
>
> Thoughts?
>
> Joel Thornton ~ <groups@joelpt.eml.cc>



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 10:10 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com