Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
SQLBoy14
Yak Posting Veteran
70 Posts |
Posted - 2014-03-09 : 14:34:54
|
Hi all,How do I pass the multiple value in the single parameter?Here is the select simple select statement with two fields (ID and Map_Area):SELECT *FROM TestParamOneHere is the output:ID Map_Are12345 KK45657463 IIY734345 FGD44342 DFRE4646 DSAW424245 DSAW12121 DSAW5753 FRDE575737 FRDE1121 FRDE1121 F5FR646462 F5FR8568 F5FRHere is my simple stored proc. Right know I only can execute with one value at the time but I am trying to run the stored proc with more than one value. CREATE PROCEDURE TestParam1@Map varchar(4)ASBEGIN SET NOCOUNT ON; SELECT ID, Map_Area FROM TestParamOne WHERE Map_Area=@Map ENDGOAnyone can help me?SQLBoy |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2014-03-09 : 19:28:08
|
http://vyaskn.tripod.com/passing_arrays_to_stored_procedures.htm KH[spoiler]Time is always against us[/spoiler] |
|
|
SQLBoy14
Yak Posting Veteran
70 Posts |
Posted - 2014-03-09 : 19:33:04
|
Hi Khtan, I try the method # 1 and it does not return the correct data. I put two values of Map Area and it return nothing. Any idea?SQLBoy |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2014-03-09 : 19:49:26
|
can you show us your query ? KH[spoiler]Time is always against us[/spoiler] |
|
|
SQLBoy14
Yak Posting Veteran
70 Posts |
Posted - 2014-03-09 : 20:05:04
|
This is the simple stored proc I did. Thank you Khtan.SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER PROCEDURE TestParam1(@Map varchar(500))ASBEGIN SET NOCOUNT ON DECLARE @sql Varchar(600) SET @sql= 'SELECT ID, Map_Area FROM TestParamOne WHERE Map_Area IN ('+@Map+')' Exec(@SQL) ENDGOSQLBoy |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2014-03-09 : 20:23:11
|
do a PRINT @sql before exec(@sql) and you will see the issue.in the example OrderID from Orders table in Northwind is an integer. Your map_area is string. If you pass in @Map = 'KK45,IIY7' the form statement will be likeWHERE Map_Area IN (KK45, IIY7) so you will need to pass in the value as @Map = '''KK45'',''IIY7''' KH[spoiler]Time is always against us[/spoiler] |
|
|
SQLBoy14
Yak Posting Veteran
70 Posts |
Posted - 2014-03-09 : 20:41:50
|
Hi Khtan,That is correct. The data type is varchar and you are correct, I would need to insert in the where clause. Now, let's say if you have 100 or 500 different values, how would you handle that?SQLBoy |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2014-03-09 : 21:17:11
|
your calling application would need to format that value accordingly.there are also other method describe in the link, you can also explore it KH[spoiler]Time is always against us[/spoiler] |
|
|
|
|
|
|
|