Solved

Calling a stored proc with GetDate() as a parameter

Posted on 2004-04-16
5
4,769 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

932 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

15 Experts available now in Live!

Get 1:1 Help Now