Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server ASP
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 8th, 2003, 07:17 AM
Authorized User
 
Join Date: Nov 2003
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default SQL Stored Procedure...

Hi,

I have a stored procedure like this...

CREATE PROCEDURE Test
(@UList [varchar](50))
 AS
select * from [User] where UserID in (@UList)
GO

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?

regds,
Babloo

 
Old December 8th, 2003, 03:38 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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:

http://vyaskn.tripod.com/passing_arr...procedures.htm

Another solution that has been posted to this list (the temp table solution):
Code:
CREATE FUNCTION Inlist (@list varchar(8000))
 RETURNS @tbl TABLE (val int not null)  AS  
 BEGIN 
        Declare @index int,
                @pos int,
                @str varchar(8000),
                @num int
        Set @pos = 1
        Set @index = 1
        While @index > 0
        Begin
                set @index = charindex(',', @list, @pos)
                if @index > 0
                        Set @str = substring(@list, @pos, @index - @pos)
                Else
                        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
        End
        Return
 End
 --------------------------------
 Used in a select statement as followed:
"SELECT * FROM MyTable WHERE ID NOT IN (select val from inlist('1,2,3'))"
HtH,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.