Query related to store proc in sql

  • Hi,

    I have created a storproc as follows:

    create proc test(@num1 int=null, @num2 int=10)

    as

    begin

    select @num2

    select @num1

    end

    exec test @num1=default,@num2=45

    Output is coming as: 45, null

    I am not getting why value of num2 is coming as 45, ideally it should get overwritten by 10.

    Kindy help.

  • THe procedure is acting exactly as defined. The 10 is a default for the parameter so if the parameter is not passed in it will be assigned the value 10. If you do pass something in to the parameter, in this case 45, that is the value of the parameter. Basically supplying a default to a parameter in a stored procedure makes the parameter optional. The stored procedure you have created can be called like this:

    Exec test

    Exec test @num1 = 1

    Exec test @num2 = 1

    Exec test @num1 = 1, @num2 = 2

    With outputs of:

    10, Null

    10, 1

    1, Null

    1, 2

    Jack Corbett
    Consultant - Straight Path Solutions
    Check out these links on how to get faster and more accurate answers:
    Forum Etiquette: How to post data/code on a forum to get the best help
    Need an Answer? Actually, No ... You Need a Question

  • Thanks, everything is clear now.

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

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