Optional Parameter

  • How would i implement an optional parameter in a SPROC??

    I need to pass an optional parameter into a function that uses a SPROC to return data. And i am not sure how sql2000 will act if i try to pass a Null value..

     

    Erik.,..

    Dam again!

  • Erik - you could have a default value for your optional parameter and in your code can specify conditions based on the value of the parameter...

    eg: you can have a proc:

    CREATE PROCEDURE procOptionalParam

    @ID Int,

    @OptionalParam VarChar(35) = ''

    AS

    IF @OptionalParam = ''

    BEGIN

    SELECT * FROM tblMyTable WHERE ID = @ID

    END

    ELSE

    BEGIN

    SELECT * FROM tblMyTable WHERE ID = @ID AND myCol = @OptionalParam

    END

    GO







    **ASCII stupid question, get a stupid ANSI !!!**

  • Thanks For the Quick reply!

    Let me see here...

     

    Erik...

    Dam again!

  • Got a follow up question for you, pLease...

    This is my first Using the IF and the ELSE ,, inside sql2000..

    For the Parameter---->@OptionalParam VarChar(35) = ''

    AS

    IF @OptionalParam = ''

    BEGIN

    This parameter,,,, it is posiable that this parameter will not have the same value every trip to the database...

     

    ??

    Dam again!

  • Erik - the '' that I set was only the default...I don't even know what your procedure is going to do...I gave you a "general guideline"...you can set a default and then pass whatever value you want to your parameter...

    if it is empty - ie - '' - then it won't be used - if it has a value - then it will use the value however you code the proc to have it used - so you can pass whatever value you want to to this parameter...if empty - do nothing - if it has a value then do something else...

    hope that makes more sense...?!?!







    **ASCII stupid question, get a stupid ANSI !!!**

  • I am going to have to do a little study session on this one..

     

    Thank You For the help!

    Erik...

    Dam again!

Viewing 6 posts - 1 through 6 (of 6 total)

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