Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Calling a stored proc with GetDate() as a parameter

Posted on 2004-04-16
5
Medium Priority
?
5,223 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 200 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

879 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