Solved

Calling a stored proc with GetDate() as a parameter

Posted on 2004-04-16
5
4,732 Views
Last Modified: 2010-02-23
I have a stored procedure written that accepts 2 datetime fields as parameters.

It works fine if I use real dates. i.e.:
exec updateduration '4-1-2004', '4-1-2004'

I want to call it using getdate() but it won't work.. why not?
i.e. : exec updateduration getdate(), getdate()
Line 1: Incorrect syntax near ')'.

?!?!HELP!
0
Comment
Question by:gozoliet
5 Comments
 
LVL 1

Accepted Solution

by:
1markmc earned 50 total points
ID: 10845722
Looking at Books On Line it appears that the Execute statement can pass parameters that are either a value or a variable but not an expression as is getdate().  

perhaps you could store the result of getdate() to a variable before passing?
0
 

Expert Comment

by:jschillm
ID: 10845782
try this:

declare @vardate1 datetime
declare @vardate2 datetime

Select @vardate1 = getdate(), @vardate2 = getdate()

Exec updateduration @vardate1, @vardate2

keep in mind that the getdate() function includes the date and time
0
 
LVL 7

Expert Comment

by:ChrisFretwell
ID: 10845930
You can also call the stored procedure without passing the date value and just use getdate() inside the sproc where you were putting the passed variable. There isnt really a reason to pass getdate.
0
 
LVL 4

Author Comment

by:gozoliet
ID: 10845952
So the answer is you just can't do that.

The other answers are also correct.

What I did was partly what CHrisFretwell suggested.. I put in a default parameter of null for those parameters, and if it was null, then i used getdate() inside the procedure.

Thanks all.
0
 

Expert Comment

by:mgexpert1
ID: 26837009
ChrisFretwell asks why anybody would want to pass getDate()... a good example is a generic stored procedure that works with any date, say "getSalesForDate". one could pass '2010-02-22' if the sales for 22-feb-2010 are desired, and for TODAY's sales, one would *want* to pass getDate(). sadly, one would be disappointed to find that getDate() doesn't work as a parameter.

suboptimal solutions (such as declaring a variable, then passing the value of the variable, or making NULL a magic parameter value to mean getDate(), or instead of passing the date in as a datetime, pass it in as a string, and use 'TODAY' as a magic string) all exist to work around this sql server limitation.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how the fundamental information of how to create a table.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now