Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a variable number of parameters to a SQL Server stored procedure?

I used SQL Server 2005 for my small web application. I Want pass parameters to SP . But there is one condition. number of parameter that can be change time to time. Think ,this time i pass name and Address , next time i pass name,surname,address ,

this parameter range may be 1-30 ,

like image 215
Kumara Avatar asked Mar 21 '10 01:03

Kumara


People also ask

How do you pass n number of parameters in stored procedure?

Another way you could do this is by serializing the arguments as a JSON string and passing that into your stored procedure. You can use the NewtonSoft. JSON package to do something like this: string json = JsonConvert. SerializeObject(obj); and pass the json var into your proc.

How do I pass multiple parameters to a SQL stored procedure?

Setting up multiple parameters is very easy to do. You just need to list each parameter and the data type separated by a comma as shown below.

Can pass 3 types of parameters to stored procedures What are they?

As a program, a stored procedure can take parameters. There are three types of parameters: IN, OUT and INOUT.

How many parameters can a pass in stored procedure?

A stored procedure can have a maximum of 2100 parameters specified. Each parameter is assigned a name, a data type, and direction like Input, Output, or Return. If a direction is not specified, then by default, it is Input. You can specify a default value for the parameters.


2 Answers

You declare the procedure with default parameters and you invoke it with named parameters instead of positional parameters:

CREATE PROCEDURE usp_myProcedure
  @name varchar(100) = '',
  @surname varchar(100) = '',
  @address varchar(100) = ''
AS
BEGIN
...
END

to invoke it from T-SQL:

exec usp_myProcedure @name='John', @surname = 'Doe';
exec usp_myProcedure @name='Jane', @address = '123 Anystreet';

To invoke it from C#:

SqlCommand cmd = new SqlCommand('usp_MyProcedure', ...);
cmd.CommandType = commandtype.StoredProcedure;
cmd.Parameters.AddWithValue('@name', 'John');
cmd.Parameters.AddWithValue('@surname', 'Doe');
like image 108
Remus Rusanu Avatar answered Sep 23 '22 20:09

Remus Rusanu


You can use XML types. Here is a sample procedure:

CREATE PROCEDURE udsp_VariableParams(@params XML)
AS
BEGIN
    DECLARE @vdoc INT
    EXEC sp_xml_preparedocument @vdoc OUTPUT, @params
    SELECT * FROM OPENXML(@vdoc, '/params/param', 1) WITH ([paramname] VARCHAR(40), [paramvalue] VARCHAR(150))
    EXEC sp_xml_removedocument @vdoc    
END

EXEC udsp_VariableParams 
  '<params>
     <param paramname="name" paramvalue="value"/>
     <param paramname="address" paramvalue="value"/>
  </params>'

EXEC udsp_VariableParams 
  '<params>
     <param paramname="name" paramvalue="value"/>
     <param paramname="surname" paramvalue="value"/>
     <param paramname="address" paramvalue="value"/>
  </params>'
like image 38
stackoverflow Avatar answered Sep 19 '22 20:09

stackoverflow