dbTalk Databases Forums  

Can a stored procedure parameter be optional

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Can a stored procedure parameter be optional in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
CK
 
Posts: n/a

Default Can a stored procedure parameter be optional - 03-07-2006 , 01:58 PM






I want the procedure to check for the existence of a paramter and if it is
there, it will process these instructions, otherwise it will process these
instructions. Any ideas? Thanks for your advice.

Regards,
CK



Reply With Quote
  #2  
Old   
Tom Moreau
 
Posts: n/a

Default Re: Can a stored procedure parameter be optional - 03-07-2006 , 02:14 PM






Try:

create proc MyProc
(
@parm1 int -- mandatory
, @parm2 int = 5 -- optional
)
as
....

--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinpub.com

"CK" <c_kettenbach (AT) hotmail (DOT) com> wrote

I want the procedure to check for the existence of a paramter and if it is
there, it will process these instructions, otherwise it will process these
instructions. Any ideas? Thanks for your advice.

Regards,
CK




Reply With Quote
  #3  
Old   
SQL
 
Posts: n/a

Default Re: Can a stored procedure parameter be optional - 03-07-2006 , 02:26 PM



optional parameters have to be at the end of a stored proc
CREATE PROCEDURE get_sales_for_title
@something int,
@title varchar(80) = NULL

AS

-- Validate the @title parameter.
IF @title IS NULL
BEGIN
print 'do something here'
END
ELSE
BEGIN
print 'do something else here'
END


now you can call this proc like this
exec get_sales_for_title 1
or like this
exec get_sales_for_title 1,2
you will see that the print statement will be different for the 2 calls

http://sqlservercode.blogspot.com/


Reply With Quote
  #4  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Can a stored procedure parameter be optional - 03-07-2006 , 04:13 PM



CK (c_kettenbach (AT) hotmail (DOT) com) writes:
Quote:
I want the procedure to check for the existence of a paramter and if it is
there, it will process these instructions, otherwise it will process these
instructions. Any ideas? Thanks for your advice.
Yes, consider:

CREATE PROCEDURE some_sp @a int,
@b int = 465 AS
PRINT @a + @b
go
EXEC some_sp 1

Prints 466. You can even say:

EXEC some_sp 1, DEFAULT

to explicitly say that you want the default value to be used.

The most commonly used default value for stored procedure parameters is
probably NULL.

Note that there is no way in the stored procedure to tell whether
the parameter was actually specified in the call, or whether the
default was used. That is, inside some_sp you cannot tell the
difference between

EXEC some_sp 1

and

EXEC some_sp 1, 465


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
  #5  
Old   
--CELKO--
 
Posts: n/a

Default Re: Can a stored procedure parameter be optional - 03-08-2006 , 08:25 AM



Yes, as you have seen, but you might want to review your old Software
Engineering notes about coupling and cohesion in code modules.


Reply With Quote
  #6  
Old   
Doug
 
Posts: n/a

Default Re: Can a stored procedure parameter be optional - 03-08-2006 , 04:33 PM



Quote:
es, as you have seen, but you might want to review your old Software
Engineering notes about coupling and cohesion in code modules.

in your world on a cloudless day, what color is the sky?????



Reply With Quote
  #7  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Can a stored procedure parameter be optional - 03-08-2006 , 04:47 PM



SQL (denis.gobo (AT) gmail (DOT) com) writes:
Quote:
optional parameters have to be at the end of a stored proc
CREATE PROCEDURE get_sales_for_title
@something int,
@title varchar(80) = NULL

AS
No, there is now such law. While it may be practical to have parameters
with default value at the end, this is perfectly legal:

CREATE PROCEDURE some_sp @x int = NULL, @u INT AS
...
go
EXEC some_sp @u = 123

--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
  #8  
Old   
SQL
 
Posts: n/a

Default Re: Can a stored procedure parameter be optional - 03-09-2006 , 07:05 AM



True,
I should have been more specific and should have said that you have to
put them at the end if you want to call the proc with the parameters by
position instead of by name

CREATE PROCEDURE some_sp @x int = NULL, @u INT AS
select getdate()
go
EXEC some_sp @u = 123 --fine

EXEC some_sp 123 --will fail


http://sqlservercode.blogspot.com/


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.