Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 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 November 10th, 2003, 02:57 AM
Authorized User
 
Join Date: Jul 2003
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to sankar
Default Pass a List/Array to a Stored Procedure in SQL Ser

Hi guys,
I am trying to pass a list separated by comma to a SQL stored proc. Can anyone help me with some code example? Any help is appreciated.
Thx in adv.:)

Sankar Sengupta
__________________
Sankar Sengupta
Striving for the BEST
 
Old November 10th, 2003, 03:10 AM
Authorized User
 
Join Date: Sep 2003
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to balakumar1000
Default

do u want code for SQL Stored Proc or code in ASP?

send a parameter that will have values seperated by comma.

In the SP, use a while loop and get the values using Substring menthod. Process it. Continue while loop till the values exhaust
If u can any clarifications on this , just post a reply

Balakumar V.

 
Old November 10th, 2003, 07:20 AM
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,

Here's some code from the archives of P2P, posted some time ago:

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'))"

As this function returns a Table, you'll need at least SQL Server 2000 to use it.

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old November 10th, 2003, 07:24 AM
Authorized User
 
Join Date: Jun 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Take a look at this one:
http://www.algonet.se/~sommar/arrays-in-sql.html

Cheers,
Frank
 
Old November 11th, 2003, 05:01 AM
Authorized User
 
Join Date: Jul 2003
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to sankar
Default

Thanks 2 u all guys. I've done it. Thanks again.

Sankar Sengupta





Similar Threads
Thread Thread Starter Forum Replies Last Post
how to pass array to oracle stored procedure? zhao2007 Hibernate 1 March 21st, 2008 03:51 PM
how to pass value to stored procedure? hertendreef SQL Server 2005 3 February 23rd, 2007 11:43 AM
How to pass a parameter to a stored procedure? MaxGay2 VB.NET 2002/2003 Basics 1 November 8th, 2006 02:48 PM
Pass an Array as a Parameter to a Stored Procedure booksnore2 Oracle 0 August 31st, 2004 09:20 AM
How will I pass stored procedure in an asp shoakat SQL Server 2000 1 July 15th, 2004 10:28 PM





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