sql job and datetime parameter
Another developer created a stored开发者_Go百科 procedure that is set up to run as a sql job each month. It takes one parameter of datetime. When I try to invoke it in the job or in just a query window I get an error Incorrect syntax near ')'
. The call to execute it is:
exec CreateHeardOfUsRecord getdate()
When I give it a hard coded date like exec CreateHeardOfUsRecord '4/1/2010'
it works fine. Any idea why I can't use getdate()
in this context? Thanks.
Parameters passed with Exec must either be constants or variables. GetDate() is classed as a function. You need to declare a variable to hold the result of GetDate(), then pass it to the stored procedure.
The supplied value must be a constant or a variable; you cannot specify a function name as a parameter value. Variables can be user-defined or system variables such as @@spid.
by looking at EXECUTE (Transact-SQL)
[ { EXEC | EXECUTE } ]
{
[ @return_status = ]
{ module_name [ ;number ] | @module_name_var }
[ [ @parameter = ] { value
| @variable [ OUTPUT ]
| [ DEFAULT ]
}
you can only pass in a constant value or a variable or the DEFAULT clause
try it out:
create procedure xy_t
@p datetime
as
select @p
go
exec xy_t GETDATE()
output:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ')'.
Try passing Convert(varchar, GetDate(), 101)
Using Km's code here is a way to do it
create procedure xy_t
@p datetime
as
select @p
go
declare @date datetime
set @date = getdate()
exec xy_t @date
精彩评论