Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Optional Parameters

Author  Topic 

ingineu
Yak Posting Veteran

89 Posts

Posted - 2005-09-15 : 12:06:28
Is there an option in a stored procedure whereby a parameter can be flagged as optional? I have a stored procedure with 2 parameters, Product and Date, and I would like to be able to just pass the Product, or pass Product and Date from an Access project. Is this possible?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2005-09-15 : 12:47:05
Yes it's possible. Just provide a default value for it.

Tara
Go to Top of Page

SamC
White Water Yakist

3467 Posts

Posted - 2005-09-15 : 13:37:10
[code]
CREATE PROCEDURE HooHah ( -- Whimsy
@Parm1 VARCHAR (100) ,
@Parm2 INT = 0, -- default value if not passed
@Parm3 VARCHAR (200) OUTPUT
AS
IF @Parm1 = 'Hoo' BEGIN
SET @Parm3 = 'Hah' + CAST(@Parm2 AS VARCHAR)
END
[/code]
Go to Top of Page

ingineu
Yak Posting Veteran

89 Posts

Posted - 2005-09-15 : 14:30:24
Can I set the default value to NULL?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2005-09-15 : 14:35:37
Yes.

Tara
Go to Top of Page

derrickleggett
Pointy Haired Yak DBA

4184 Posts

Posted - 2005-09-15 : 23:54:49
If you set the default value to NULL, you will want to test for NULL OR a value in your WHERE, etc clauses. Remember NULL to anything, even NULL, is always false. That's unless you have your ANSI_NULLS option set incorrectly of course.

MeanOldDBA
derrickleggett@hotmail.com

When life gives you a lemon, fire the DBA.
Go to Top of Page
   

- Advertisement -