x
login about faq Site discussion (meta-askssc)

optional paramenters in a procedure

i want to have optional paremeters in the procedure. I want give a user an oppurtunity to enter a one single day or a date range. Like the paremeter being 07/05/2011 or a date range of 07-05-2011 to 07-10-2011, is there a way that i can do it ?

 create procedure deletedata
    @transactionalday varchar 

 begin 
 as

    delete  from atable where transactionalday = @transactionalday

  go
more ▼

asked Sep 30 '11 at 12:35 PM in Default

Katie 1 gravatar image

Katie 1
1.4k 108 161 202

(comments are locked)
10|1200 characters needed characters left

2 answers: sort voted first

Just specify the default value for each parameter in the declaration...

create proc deletedata
  @transactionalday varchar = null,
  @beginrange datetime = null,
  @endrange datetime = null,
AS

use them appropriately in the code...

if @transactionalday is null
begin
    delete from atable where transactionalday between @beginrange and @endrange;
  end
else
  begin
    delete from atable where transactionalday = @transactionalday;
  end

then call the procedure using the named parameters...

exec deletedata @transactionalday = 'mytransactionalday';

or

exec deletedata @begindate = mybeginningdate, @enddate = myendingdate;
more ▼

answered Sep 30 '11 at 02:59 PM

KenJ gravatar image

KenJ
12.4k 2 10

(comments are locked)
10|1200 characters needed characters left

Yes you can. Just add "= " for each optional parameter:

create procedure deletedata
@transactionalday varchar = '20110705'

as

begin

delete  from atable where transactionalday = @transactionalday

end

go

When you call it you can specify a value for the optional params but if you don't their default value will be used instead.

more ▼

answered Sep 30 '11 at 03:05 PM

David Wimbush gravatar image

David Wimbush
4.2k 25 29 31

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments



Facebook logo Follow Ask SSC on Facebook
Find Ask SSC on Google+
linkedin logo Find us on LinkedIn

Topics:

x1602
x340
x66
x29

asked: Sep 30 '11 at 12:35 PM

Seen: 415 times

Last Updated: Sep 30 '11 at 01:08 PM

Copyright © 2002-2012 Simple Talk Publishing. All Rights Reserved. If you have any queries, please contact the site administrators.
Ask SQL Server Central is a community service provided by Red Gate.