all groups > sql server programming > november 2003 >
You're in the

sql server programming

group:

Assign multiple values to a variable in a stored procedure



Assign multiple values to a variable in a stored procedure Vamsi
11/16/2003 11:58:18 PM
sql server programming: Hi,

I am quite new to SQL Programming. I need to achieve the
output for a query which is something like this:-

"Select * from xyz where itemid in(2,3,4,5,6)"

I need to create a stored procedure.
I need to declare a variable in the stored procedure and
store all these values(2,3,4,5,6) and should be writing
the query which is like this:
"Select * from xyz where itemid like @x"

where @x is a variable which stores all the values.

How do i go about doing this?? i.e i need to store
multiple values in a variable and refer to this variable
in my query. In future if the business needs to add some
other value then it can be just done in the beginning of
the program by adding it to the variable values rather
than going into the query.

I am able to call if its a single value but how do i
assign multiple values to a single variable??

I hope i was clear.Please help me. I would appreciate any
help.
Thanks,
Re: Assign multiple values to a variable in a stored procedure Vamsi
11/17/2003 1:04:19 AM
Thanks a million.Great this is working for integer
values,, How do i go about if the datatype is varchar!!.
Could you please let me know!!

Vamsi


[quoted text, click to view]
Re: Assign multiple values to a variable in a stored procedure oj
11/17/2003 1:19:45 AM
Erland has written some good info regarding this...

http://www.algonet.se/~sommar/arrays-in-sql.html

--
-oj
RAC v2.2 & QALite!
http://www.rac4sql.net



[quoted text, click to view]

Re: Assign multiple values to a variable in a stored procedure TS
11/17/2003 2:17:57 PM
In your Stored Procedure:

DECLARE @ITMX VARCHAR(100)
DECLARE @STRSQL VARCHAR(2000)
SET @ITMX = '2,3,4,5,6'

SET @STRSQL = 'SELECT * FROM XYZ WHERE ITEMID IN (' + @ITMX + ')'
EXEC (@STRSQL)



[quoted text, click to view]

Re: Assign multiple values to a variable in a stored procedure Tanuja
11/17/2003 3:31:48 PM
DECLARE @STRSQL VARCHAR(2000)
DECLARE @ITMX VARCHAR(100)



SET @ITMX = '''TEXT2'',''TEXT5'''

SET @STRSQL = 'SELECT * FROM XYZ WHERE ITEMNAME IN (' + @ITMX + ')'
EXEC(@STRSQL)



[quoted text, click to view]

Re: Assign multiple values to a variable in a stored procedure Vamsi
11/17/2003 5:21:54 PM
Hi Tanuja,
Yes, got it.This is working with characters. This
was great help.Actually i have referred to many sites and
also sql help, but no where i am able to get the exact
picture i wanted. It would have taken me years if not for
your reply.

Thanks a lot once again.
Vamsi.



[quoted text, click to view]
AddThis Social Bookmark Button