optional parameter

  • Can I have an optional parameter within an SP? If so, what is the syntax (how do I declare it)?

    Sam

  • Yes, you can... best way to learn this one is to look up CREATE PROCEDURE in Books Online and read about it

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Sam,

    Here is a very basic example using an optional paramater.  The parameter @title is given a default value so users do not have to pass in a value unless they want a different result set.

    USE Northwind

    GO

    CREATE PROCEDURE dbo.nwSample

      @Title nvarchar(30) ='Sales Representative'

    AS

    SELECT LastName, FirstName, Title, HireDate

    FROM dbo.Employees

    WHERE Title = @title 

    --Then try

    exec nwSample  --no paramater

    exec nwSample 'Sales Manager' --with parameter

    --DROP PROCEDURE nwSample

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply