2010-04-23 88 views
2

另一位开发人员创建了一个存储过程,该存储过程设置为每个月都作为一个sql作业运行。它需要一个datetime参数。当我尝试在作业中或在查询窗口中调用它时,出现错误Incorrect syntax near ')'。执行它的调用是:sql作业和日期时间参数

exec CreateHeardOfUsRecord getdate() 

当我给它一个硬编码的日期一样exec CreateHeardOfUsRecord '4/1/2010'它工作正常。任何想法为什么我不能在这种情况下使用getdate()?谢谢。

+0

其实我是不正确的。参数是smalldatetime不是日期时间 – 2010-04-23 17:05:45

回答

3

通过Exec传递的参数must either be constants or variables。 GetDate()被分类为一个函数。您需要声明一个变量来保存GetDate()的结果,然后将其传递给存储过程。

提供的值必须是常数 或变量;您不能指定 函数名称作为参数值。 变量可以是用户定义的或 系统变量,如@@ spid。

1

通过看EXECUTE (Transact-SQL)

[ { EXEC | EXECUTE } ] 
    { 
     [ @return_status = ] 
     { module_name [ ;number ] | @module_name_var } 
     [ [ @parameter = ] { value 
          | @variable [ OUTPUT ] 
          | [ DEFAULT ] 
          } 

你只能通过在一个恒定值或一个变量或默认条款

尝试一下:

create procedure xy_t 
@p datetime 
as 
select @p 
go 

exec xy_t GETDATE() 

输出:

Msg 102, Level 15, State 1, Line 1 
Incorrect syntax near ')'. 
+0

OP提到datetime vs.smalldatetime,但是数据类型并不重要,它会在失败之前失败。使用我的示例程序尝试'exec xy_t'10'+'2'',您会得到'Msg 102,Level 15,State 1,Line 1'+'附近的错误语法。' – 2010-04-23 17:22:06

0

尝试通过转换(VARCHAR,GETDATE(),101)

+0

您无法将表达式传递到存储过程,看我的示例代码。试试你的答案结果如下:'Msg 156,Level 15,State 1,Line 1关键字'Convert'附近的语法错误。 – 2010-04-23 17:19:45

0

这里使用松下的代码是一个办法做到这一点

create procedure xy_t 
@p datetime 
as 
select @p 
go 

declare @date datetime 

set @date = getdate() 

exec xy_t @date