• subscribe
October 15, 2007 12:00 AM

Jump Start: Stored Procedure Parameters

SQL Server Pro
InstantDoc ID #97259

In "Inside Stored Procedure Templates" (www.sqlmag.com/Article/ArticleID/97201 ), I explained how to customize the generic stored procedure template that's generated by SQL Server Management Studio Express (SSMSE). My sample procedure, which I named MyNwdSP, uses one parameter and one variable and contains the following T-SQL source code:

CREATE PROCEDURE MyNwdSP
	@P1city nvarchar(15)
AS
	SELECT * FROM Customers WHERE city = @P1city
GO
MyNwdSP's parameter is @P1city. The first thing to note is that parameters are used as variables within stored procedures, and all T-SQL variables begin with the @ symbol. The name can be anything you designate. I used @P1city to make it clear that this is the first parameter in the stored procedure and that it's used for the city column, but I could have given it a different name, such as @parm1, @InputCity, or @SomethingElse.

Let's modify this stored procedure to accept two variables and to provide default values for those variables. The modified stored procedure is

CREATE PROCEDURE MyNwdSP
	@P1city nvarchar(15) = 'Seattle',
	@P2country nvarchar(15) = 'USA'
AS
	SELECT * FROM Customers WHERE city = @P1city
GO
Multiple variables must be separated by commas, and each parameter variable must have a unique name. You specify default values for a parameter by adding an equals sign (=) and the default value after the parameter. Now that you've defined default values, you can call this stored procedure either with or without parameter values. When the call provides no values for the parameters, the result set will return rows where the country is USA and the city is Seattle.


ARTICLE TOOLS

Comments
    There are no comments to display. Be the first one!
You must log on before posting a comment.

Are you a new visitor? Register Here
  • SP1?
    I know there is a SP1 for SQL 2008 R2 available....and there is a "feature pack" as well... ...
  • SQL database mirroring
    I have SQL Server 2008 R2 Enterprise 64bit on Windows 2008 R2 Enterprise 64bit.  Each SQL Server has...
  • Dell Compellent Disk Drive
    Does anybody has experience with Dell Compellent Disk Drive? Basically, this system manages all disk...
  • Sql server performance tuning
    I need to find a tool that help me to optimize sql server,queries,improve the performance and solve ...