dbTalk Databases Forums  

Passing comma seperated values to a stored proc

microsoft.public.sqlserver.programming microsoft.public.sqlserver.programming


Discuss Passing comma seperated values to a stored proc in the microsoft.public.sqlserver.programming forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
M Smith
 
Posts: n/a

Default Passing comma seperated values to a stored proc - 12-22-2004 , 10:39 AM






I have an application where the user will type in ID numbers into a single
field in an ASP page. They can type in multiple IDs if they are seperated
by a comma. I want to pass this string into a Stored Proc and just use an
"in" command in the where clause to return a recordset back to the ASP page.
The problem is that the ID numbers are integer and the parameter is a
varchar. When I try to pass multiple IDs into the Stored Proc I get an
error.
Here is the Stored Proc
CREATE PROCEDURE test @pvID as varchar(500) AS
Select * from member_t where member_ID in (@pvID)
GO
When I try it by typing - exec test '123456,123457,123458' - I get this
error
"Syntax error converting the varchar value '123456,123457,123458' to a
column of data type int."

How would I go about converting the string to an integer and still use the
In statement.
Thanks





Reply With Quote
  #2  
Old   
Keith Kratochvil
 
Posts: n/a

Default Re: Passing comma seperated values to a stored proc - 12-22-2004 , 10:41 AM






Here is an excellent article:
http://www.sommarskog.se/dynamic_sql.html#List

--
Keith


"M Smith" <msmith (AT) avma (DOT) org> wrote

Quote:
I have an application where the user will type in ID numbers into a single
field in an ASP page. They can type in multiple IDs if they are seperated
by a comma. I want to pass this string into a Stored Proc and just use an
"in" command in the where clause to return a recordset back to the ASP
page.
The problem is that the ID numbers are integer and the parameter is a
varchar. When I try to pass multiple IDs into the Stored Proc I get an
error.
Here is the Stored Proc
CREATE PROCEDURE test @pvID as varchar(500) AS
Select * from member_t where member_ID in (@pvID)
GO
When I try it by typing - exec test '123456,123457,123458' - I get this
error
"Syntax error converting the varchar value '123456,123457,123458' to a
column of data type int."

How would I go about converting the string to an integer and still use the
In statement.
Thanks






Reply With Quote
  #3  
Old   
Alejandro Mesa
 
Posts: n/a

Default RE: Passing comma seperated values to a stored proc - 12-22-2004 , 11:15 AM



Also,

Faking arrays in T-SQL stored procedures
http://www.bizdatasolutions.com/tsql/sqlarrays.asp



AMB

"M Smith" wrote:

Quote:
I have an application where the user will type in ID numbers into a single
field in an ASP page. They can type in multiple IDs if they are seperated
by a comma. I want to pass this string into a Stored Proc and just use an
"in" command in the where clause to return a recordset back to the ASP page.
The problem is that the ID numbers are integer and the parameter is a
varchar. When I try to pass multiple IDs into the Stored Proc I get an
error.
Here is the Stored Proc
CREATE PROCEDURE test @pvID as varchar(500) AS
Select * from member_t where member_ID in (@pvID)
GO
When I try it by typing - exec test '123456,123457,123458' - I get this
error
"Syntax error converting the varchar value '123456,123457,123458' to a
column of data type int."

How would I go about converting the string to an integer and still use the
In statement.
Thanks






Reply With Quote
  #4  
Old   
M Smith
 
Posts: n/a

Default Re: Passing comma seperated values to a stored proc - 12-22-2004 , 11:17 AM



Great article. It really helped a lot. Thank You.
Mike

"Keith Kratochvil" <sqlguy.back2u (AT) comcast (DOT) net> wrote

Quote:
Here is an excellent article:
http://www.sommarskog.se/dynamic_sql.html#List

--
Keith


"M Smith" <msmith (AT) avma (DOT) org> wrote in message
news:OIWACTE6EHA.2592 (AT) TK2MSFTNGP09 (DOT) phx.gbl...
I have an application where the user will type in ID numbers into a
single
field in an ASP page. They can type in multiple IDs if they are
seperated
by a comma. I want to pass this string into a Stored Proc and just use
an
"in" command in the where clause to return a recordset back to the ASP
page.
The problem is that the ID numbers are integer and the parameter is a
varchar. When I try to pass multiple IDs into the Stored Proc I get an
error.
Here is the Stored Proc
CREATE PROCEDURE test @pvID as varchar(500) AS
Select * from member_t where member_ID in (@pvID)
GO
When I try it by typing - exec test '123456,123457,123458' - I get
this
error
"Syntax error converting the varchar value '123456,123457,123458' to a
column of data type int."

How would I go about converting the string to an integer and still use
the
In statement.
Thanks








Reply With Quote
  #5  
Old   
Keith Kratochvil
 
Posts: n/a

Default Re: Passing comma seperated values to a stored proc - 12-22-2004 , 11:25 AM



The real thanks should go to Erland for putting together such a great page!
Anyway, I am glad that I was able to help.

--
Keith


"M Smith" <msmith (AT) avma (DOT) org> wrote

Quote:
Great article. It really helped a lot. Thank You.
Mike

"Keith Kratochvil" <sqlguy.back2u (AT) comcast (DOT) net> wrote in message
news:uCqWcVE6EHA.936 (AT) TK2MSFTNGP12 (DOT) phx.gbl...
Here is an excellent article:
http://www.sommarskog.se/dynamic_sql.html#List

--
Keith


"M Smith" <msmith (AT) avma (DOT) org> wrote in message
news:OIWACTE6EHA.2592 (AT) TK2MSFTNGP09 (DOT) phx.gbl...
I have an application where the user will type in ID numbers into a
single
field in an ASP page. They can type in multiple IDs if they are
seperated
by a comma. I want to pass this string into a Stored Proc and just
use
an
"in" command in the where clause to return a recordset back to the ASP
page.
The problem is that the ID numbers are integer and the parameter is a
varchar. When I try to pass multiple IDs into the Stored Proc I get
an
error.
Here is the Stored Proc
CREATE PROCEDURE test @pvID as varchar(500) AS
Select * from member_t where member_ID in (@pvID)
GO
When I try it by typing - exec test '123456,123457,123458' - I get
this
error
"Syntax error converting the varchar value '123456,123457,123458' to a
column of data type int."

How would I go about converting the string to an integer and still use
the
In statement.
Thanks









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



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.