Incorrect syntax near ')' calling stored procedure with GETDATE

☆樱花仙子☆ 提交于 2019-11-26 09:46:20

问题


Maybe I am having a moment of \'afternoon\', but can anyone explain why I get

Msg 102, Level 15, State 1, Line 2
Incorrect syntax near \')\'.

When running

CREATE PROC DisplayDate 
    (@DateVar DATETIME) 
AS 
BEGIN
    SELECT @DateVar
END
GO

EXEC DisplayDate GETDATE();

回答1:


You can't pass in a function call as an argument to your stored procedure. Instead use an intermediate variable:

DECLARE @tmp DATETIME
SET @tmp = GETDATE()

EXEC DisplayDate @tmp;



回答2:


As Mitch Wheat mentioned you can't pass a function.

If in your case you should pass in a precalculated value or GETDATE() - you can use default value. For example, modify your stored procedure:

ALTER PROC DisplayDate 
(
    @DateVar DATETIME = NULL
) AS 
BEGIN
    set @DateVar=ISNULL(@DateVar,GETDATE())

    --the SP stuff here
    SELECT @DateVar
END
GO

And then try:

EXEC DisplayDate '2013-02-01 00:00:00.000'
EXEC DisplayDate

Remark: Here I supposed that NULL value is not in use for this parameter. If it is not your case - you can use another unused value, for example '1900-01-01 00:00:00.000'



来源:https://stackoverflow.com/questions/2399104/incorrect-syntax-near-calling-stored-procedure-with-getdate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!