Home > SQL > T-SQL Pass GETDATE() as a parameter for stored procedure

T-SQL Pass GETDATE() as a parameter for stored procedure

June 24th, 2009

When you pass getdate() function as a parameter directly to a stored procedure, you will get an error: Incorrect syntax near ‘)’.

EXEC sp_GetProject GETDATE()
 
Error:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ')'.

To avoid this, you will need to create a variable and pass it to the stored procedure. Like this:

DECLARE @dt DATETIME;
SET @dt=GETDATE();
 
EXEC sp_GetProject @dt
VN:F [1.9.11_1134]
Rating: 4.8/5 (14 votes cast)
T-SQL Pass GETDATE() as a parameter for stored procedure, 4.8 out of 5 based on 14 ratings

No related posts.

SQL

  • Sajidddhukka

    When I execute I face Error I.E(Conversion failed when converting date and/or time from character string.)

    Please Reply… 

    DECLARE @PID int
    DECLARE @SDATE DATETIME = NULL 
    DECLARE @ParaName varchar(50)

    SET @ParaName=’MyName’
    SET @SDATE= GETDATE()

    –COALESCE(‘@SDATE’,GETDATE())

    SELECT @PID = ProfileId FROM tGeneralProfileCX where Name=@ParaName AND CreatedOn=CONVERT(Varchar,’@SDATE’,100)
    print @PID:disqus 

    VA:F [1.9.11_1134]
    Rating: 0.0/5 (0 votes cast)
  • http://www.ninethsense.com/ Praveen V Nair

    Hi @ff0232c88014af734a95ba2eb95e5ea4:disqus 

    Your query is not clear for me.

    Here is a full example of stored procedure creation and execution. Hope this helps. Probably you are getting an error which is not related to this blog post.

    /* Create Stored Procedure */

    CREATE PROCEDURE sp_GetProject (@dt DATETIME)ASBEGIN    PRINT @dt;END

    /* Execute Stored Procedure */

    DECLARE @dt DATETIME;SET @dt:disqus =GETDATE();
    EXEC sp_GetProject @dt

    VA:F [1.9.11_1134]
    Rating: 0.0/5 (0 votes cast)