Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server stored procedure parameters

I am developing a framework, where in I am a calling stored procedure with dynamically created parameters. I am building parameter collection at the runtime.

The problem occurs when I am passing a parameter to stored procedure, but stored proc doesn't accept such parameter.

For example, my stored procedure is:

CREATE PROCEDURE GetTaskEvents     @TaskName varchar(50) AS BEGIN -- SP Logic END 

Calling stored procedure as:

EXEC GetTaskEvents @TaskName = 'TESTTASK', @ID = 2 

This throws below error:

Msg 8144, Level 16, State 2, Procedure GetTaskEvents, Line 0 Procedure or function GetTaskEvents has too many arguments specified. 

This works fine in Sybase ASE, which simply ignores any additional parameters. Could this be achieved with MSSQL server 2008? Any help, much appreciated. Thanks

like image 988
Narayan Akhade Avatar asked Mar 03 '13 22:03

Narayan Akhade


People also ask

Can a stored procedure use parameters?

A stored procedure can have zero or more INPUT and OUTPUT parameters. 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.

How do I execute a SQL stored procedure with parameters?

Expand the database that you want, expand Programmability, and then expand Stored Procedures. Right-click the user-defined stored procedure that you want and select Execute Stored Procedure. In the Execute Procedure dialog box, specify a value for each parameter and whether it should pass a null value.

Can we pass parameters to stored procedure in SQL?

SQL Stored Procedures for SQL ServerYou can also pass parameters to a stored procedure, so that the stored procedure can act based on the parameter value(s) that is passed.


2 Answers

SQL Server doesn't allow you to pass parameters to a procedure that you haven't defined. I think the closest you can get to this sort of design is to use optional parameters like so:

CREATE PROCEDURE GetTaskEvents     @TaskName varchar(50),     @ID int = NULL AS BEGIN -- SP Logic END; 

You would need to include every possible parameter that you might use in the definition. Then you'd be free to call the procedure either way:

EXEC GetTaskEvents @TaskName = 'TESTTASK', @ID = 2; EXEC GetTaskEvents @TaskName = 'TESTTASK'; -- @ID gets NULL here 
like image 185
Yuck Avatar answered Oct 06 '22 08:10

Yuck


Why would you pass a parameter to a stored procedure that doesn't use it?

It sounds to me like you might be better of building dynamic SQL statements and then executing them. What you are trying to do with the SP won't work, and even if you could change what you are doing in such a way to accommodate varying numbers of parameters, you would then essentially be using dynamically generated SQL you are defeating the purpose of having/using a SP in the first place. SP's have a role, but there are not the solution in all cases.

like image 20
E.J. Brennan Avatar answered Oct 06 '22 07:10

E.J. Brennan