Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Can a stored procedure parameter be optional

0 views
Skip to first unread message

CK

unread,
Mar 7, 2006, 2:58:16 PM3/7/06
to
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


SQL

unread,
Mar 7, 2006, 3:26:38 PM3/7/06
to
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/

Tom Moreau

unread,
Mar 7, 2006, 3:14:01 PM3/7/06
to
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_kett...@hotmail.com> wrote in message
news:sBlPf.43261$F_3....@newssvr29.news.prodigy.net...

Erland Sommarskog

unread,
Mar 7, 2006, 5:13:40 PM3/7/06
to

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, esq...@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

--CELKO--

unread,
Mar 8, 2006, 9:25:44 AM3/8/06
to
Yes, as you have seen, but you might want to review your old Software
Engineering notes about coupling and cohesion in code modules.

Doug

unread,
Mar 8, 2006, 5:33:03 PM3/8/06
to
>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?????

Erland Sommarskog

unread,
Mar 8, 2006, 5:47:13 PM3/8/06
to
SQL (denis...@gmail.com) writes:
> 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

SQL

unread,
Mar 9, 2006, 8:05:10 AM3/9/06
to
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/

0 new messages