Learn more about SQL Server tools

 
 

Tutorials          DBA          Dev          BI          Career          Categories          Events          Whitepapers          Today's Tip          Join

Tutorials      DBA      Dev      BI      Categories      Events

DBA    Dev    BI    Categories

 

How to create a SQL Server stored procedure with parameters



By:
Overview

The real power of stored procedures is the ability to pass parameters and have the stored procedure handle the differing requests that are made.  In this topic we will look at passing parameter values to a stored procedure.

Explanation

Just like you have the ability to use parameters with your SQL code you can also setup your stored procedures to accept one or more parameter values.

One Parameter

In this example we will query the Person.Address table from the AdventureWorks database, but instead of getting back all records we will limit it to just a particular city.  This example assumes there will be an exact match on the City value that is passed.

USE AdventureWorks
GO

CREATE PROCEDURE dbo.uspGetAddress @City nvarchar(30)
AS
SELECT * 
FROM Person.Address
WHERE City = @City
GO

To call this stored procedure we would execute it as follows:

EXEC dbo.uspGetAddress @City = 'New York'

We can also do the same thing, but allow the users to give us a starting point to search the data.  Here we can change the "=" to a LIKE and use the "%" wildcard.

USE AdventureWorks
GO

CREATE PROCEDURE dbo.uspGetAddress @City nvarchar(30) 
AS 
SELECT * 
FROM Person.Address 
WHERE City LIKE @City + '%' 
GO

In both of the proceeding examples it assumes that a parameter value will always be passed. If you try to execute the procedure without passing a parameter value you will get an error message such as the following:

Msg 201, Level 16, State 4, Procedure uspGetAddress, Line 0

Procedure or function 'uspGetAddress' expects parameter '@City', which was not supplied.

Default Parameter Values

In most cases it is always a good practice to pass in all parameter values, but sometimes it is not possible.  So in this example we use the NULL option to allow you to not pass in a parameter value.  If we create and run this stored procedure as is it will not return any data, because it is looking for any City values that equal NULL.

USE AdventureWorks
GO

CREATE PROCEDURE dbo.uspGetAddress @City nvarchar(30) = NULL
AS
SELECT *
FROM Person.Address
WHERE City = @City
GO

We could change this stored procedure and use the ISNULL function to get around this.  So if a value is passed it will use the value to narrow the result set and if a value is not passed it will return all records. (Note: if the City column has NULL values this will not include these values. You will have to add additional logic for City IS NULL)

USE AdventureWorks
GO

CREATE PROCEDURE dbo.uspGetAddress @City nvarchar(30) = NULL
AS
SELECT *
FROM Person.Address
WHERE City = ISNULL(@City,City)
GO

Multiple Parameters

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.

USE AdventureWorks
GO

CREATE PROCEDURE dbo.uspGetAddress @City nvarchar(30) = NULL, @AddressLine1 nvarchar(60) = NULL
AS
SELECT *
FROM Person.Address
WHERE City = ISNULL(@City,City)
AND AddressLine1 LIKE '%' + ISNULL(@AddressLine1 ,AddressLine1) + '%'
GO

To execute this you could do any of the following:

EXEC dbo.uspGetAddress @City = 'Calgary'
--or
EXEC dbo.uspGetAddress @City = 'Calgary', @AddressLine1 = 'A'
--or
EXEC dbo.uspGetAddress @AddressLine1 = 'Acardia'
-- etc...





More SQL Server Solutions




Post a comment or let the author know this tip helped.

All comments are reviewed, so stay on subject or we may delete your comment. Note: your email address is not published. Required fields are marked with an asterisk (*).

*Name    *Email    Notify for updates 


Get free SQL tips:

*Enter Code refresh code     



Monday, January 25, 2016 - 9:51:30 AM - raju Back To Top

 REQUIREMENT: Produce a report showing the number of days between successive visits. Leave the column blank for the first visit for each patient.    The results should be as follows:

 

PatientNo              VisitNo           VisitDate         DaysSincePrevious

 

1                            1                      12-Jan-2005

 

1                            2                      23-Jan-2005                11

 

1                            3                      28-Jan-2005                5

 

2                            1                      01-Feb-2005

 

2                            2                      08-Feb-2005                7

 

4                            1                      03-Feb-2005

     4                             2                      08-Feb-2005             5                     

create table patients

(

patient_id int,

patient_no int,

initials varchar(30)

)

insert into patients values (1,1,'ABC'),

                                      (2,3,'DEF'),

      (3,4,'HIJ')

SELECT* FROM PATIENTS

create table visits

(

visit_id int,

patient_id int,

visit_no int,

visit_date date

)

insert into visits values (1,1,1,'12-jan-2005'),

                                  (2,1,2,'23-jan-2005'),

 (3,1,3,'28-jan-2005'),

         (4,2,1,'01-feb-2005'),

 (5,2,2,'08-feb-2005'),

 (6,3,1,'03-feb-2005'),

 (7,3,2,'08-feb-2005')

 

SELECT * FROM VISITS

 

 


Learn more about SQL Server tools