Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi Team,

I am very new to this page. I am facing two issues when i tried to make a SP in sql 2005.


SQL
CREATE proc Dept (@department varchar(30))      
as      
	Select * from test1      
	where [Department] in (@department)


the above sp is working find when i am passing one value i.e

SQL
exec dept 'a'


But when i try to execute more then one value in @department. Its not working fine i.e.

SQL
exec dept 'a','b'


but when i tried to check my query. its giving me results

SQL
Select * from test1      
	where [Department] in ('a','b')


Why its not working in SP.

The second this i would like to pass @department from asp.net through C#
Posted
Updated 29-Apr-12 21:52pm
v2

Your stored procedure is defined to accept only one parameter. If you try to pass multiple parameters then it wont work.

If you need to pass variable number of parameters to sp then perhaps you will have to imclude some delimiter in your parameter list and pass that list as single patameter into the SP.

i.e.

SQL
exec dept 'a, b' 

(watch the single quote)


and inside your sp you will have to extract the values from this comma separated list and then you them in the actual query.
 
Share this answer
 
Comments
Jaggi_singh 30-Apr-12 4:05am    
Rahul, You can suggest some code bro.

i need to use this in asp.net to pass a string to fetch some data from table in a list box :)
Rahul Rajat Singh 30-Apr-12 4:28am    
To achieve what I just said just refer this article and you will get what you want

http://www.codeproject.com/Articles/5986/Passing-a-Comma-Delimited-Parameter-to-a-Stored-Pr
Hi,

Can you try it as follows?

SQL
exec dept 'a,b'


It should work.

Regards
Sebastian
 
Share this answer
 
Comments
Rahul Rajat Singh 30-Apr-12 4:29am    
How will it work if the SP itself is not tuned to handle comma separated values.
Hi Sebastian,

Its not working :(

Exec Dept ('a,b')

a,b are the values of a table with Header Department
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900