Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server ASP
Password Reminder
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
SQL Server ASP Discussions about ASP programming with Microsoft's SQL Server. For more ASP forums, see the ASP forum category.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server ASP section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 8th, 2003, 07:17 AM
Authorized User
Join Date: Nov 2003
Location: , , .
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default SQL Stored Procedure...


I have a stored procedure like this...

(@UList [varchar](50))
select * from [User] where UserID in (@UList)

The statement that's required for execution would be,
exec Test '10,11'

But, this gives me an error.
How do I do this part of passing multiple UserIDs for this table?


Reply With Quote
  #2 (permalink)  
Old December 8th, 2003, 03:38 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts

Hi there,

Unfortunately, you can't do that directly. The IN clause expects a comma separated list of values, not a string that holds a list of comma separated values.

There are a couple of ways around this. One solution is to build the SQL statement dynamically, and then use EXEC to execute it against the database. Another solution is to parse the string, insert the values in a temp table and then join that table with your original table.

Take a look here for a more detailed discussion on the options you have available:


Another solution that has been posted to this list (the temp table solution):
CREATE FUNCTION Inlist (@list varchar(8000))
 RETURNS @tbl TABLE (val int not null)  AS  
        Declare @index int,
                @pos int,
                @str varchar(8000),
                @num int
        Set @pos = 1
        Set @index = 1
        While @index > 0
                set @index = charindex(',', @list, @pos)
                if @index > 0
                        Set @str = substring(@list, @pos, @index - @pos)
                        Set @str = substring(@list, @pos, Len(@list))
                     Set @str = ltrim(rtrim(@str))
                Set @num = cast(@str as integer)
                Insert @tbl (val) values (@num)
                Set @pos = @index + 1
 Used in a select statement as followed:
"SELECT * FROM MyTable WHERE ID NOT IN (select val from inlist('1,2,3'))"


Imar Spaanjaars
Everyone is unique, except for me.
Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't See SQL tables and Stored Procedure azambee SQL Server 2000 1 October 24th, 2008 01:03 PM
SQL in C# class vs. Dynamic Stored Procedure holf BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 3 October 26th, 2006 06:45 PM
SQL Server Stored Procedure doug SQL Server 2000 2 February 22nd, 2005 03:04 PM
Sql server Stored procedure ranakdinesh BOOK: Professional C#, 2nd and 3rd Editions 2 May 29th, 2004 12:08 AM
dymanic sql vs stored procedure prox_lae Oracle 0 February 19th, 2004 10:30 PM

All times are GMT -4. The time now is 09:11 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.