Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Calling a stored proc with GetDate() as a parameter

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
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…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

808 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