Nouman Zakir

while (true) { Post.NewArticle(); }
posts - 16, comments - 31, trackbacks - 0

My Links

News

Archives

Post Categories

Personal Website

Optional parameters in SQL Stored Procedures

You can add optional parameters in stored procedures by setting a default value for each parameter that you want to make optional. The default value is typically NULL, but it's not necessary.


Example:

CREATE PROCEDURE dbo.mySP
@firstParam VARCHAR(32) = NULL,
@secondParam INT = NULL
AS

BEGIN

SET NOCOUNT ON
SELECT *
FROM [TableName]
WHERE
Param1 = @firstParam
AND Param2 = @secondParam

END
GO

--
EXEC dbo.
mySP @firstParam ='bar', @secondParam =4
EXEC dbo.
mySP @firstParam ='bar'
EXEC dbo.
mySP @secondParam =4
EXEC dbo.
mySP 'bar',4
EXEC dbo.
mySP 'bar'
EXEC dbo.
mySP
GO

DROP PROCEDURE dbo.
mySP
GO

Print | posted on Saturday, February 28, 2009 5:48 AM | Filed Under [ SQL ]

Feedback

Gravatar

# re: Optional parameters in SQL Stored Procedures

Nice one. What if you have some optional parameters and some mandatory ones? Do they need to come in a certain order?
1/22/2010 10:22 AM | Ed
Gravatar

# re: Optional parameters in SQL Stored Procedures

Red Gate software is basically a software that gives system administrators and developers tools in doing their job. The roots of this software can be traced back to Cambridge in the United Kingdom. It has been in the programming business for quite some time already dating back 1999. - Madison Pharmacy Associates
2/20/2012 6:02 PM | MAdison Pharmacy Associates
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: