这是我的T-SQL代码,我遇到datetime
OPEN @form_dates
FETCH NEXT FROM @form_dates INTO @cid, @regdate, @compdate/* PRINT @regdate PRINT @compdate This is correctly having values in datetime. eg: Mar 28 2012 6:03PM PRINT @courseno */
WHILE @@FETCH_STATUS = 0 BEGIN SET @SQL = N'update tblname set cs' + CAST (@cid AS VARCHAR(10)) + '_begin = @rstring , cs' + CAST (@cid AS VARCHAR(10))+ '_end = @cstring where UserID = '+ CAST (@tempID AS VARCHAR(10))
DECLARE @ParmDefinition nvarchar(500) SET @ParmDefinition = N'@rstring datetime, @cstring datetime'
EXECUTE sp_ExecuteSQL @SQL, @ParmDefinition, @rstring = @regdate, @cstring = @compdate
I have the values of
@regdate
and@compdate
fetched from a cursor and are of the format Jan 3 2012 2:30PM Mar 28 2012 6:03PM but these values are not reflecting in procedure output where I get the output:@regdate
感谢。^
答案 0 :(得分:1)
这与数据类型,日期时间或游标无关。这只是 sloppy 不一致的变量命名。外面你打电话给他们:
@regstring
@compstring
你打电话给他们:
@rstring
@cstring
因此,尝试以某种方式保持一致:
SET @SQL = N'update tblname set cs' + CAST (@cid AS VARCHAR(10))
+ '_begin = @rstring , cs' + CAST (@cid AS VARCHAR(10))
+ '_end = @cstring where UserID = '+ CAST (@tempID AS VARCHAR(10))
DECLARE @ParamDefinition nvarchar(500)
SET @ParamDefinition = N'@rstring datetime, @cstring datetime'
EXECUTE sp_ExecuteSQL @SQL, @ParamDefinition,
@rstring = @regdate, @cstring = @compdate
^^-------------------^^
修改强>
这对我来说很好用:
DECLARE @SQL NVARCHAR(MAX);
SET @SQL = N'SELECT _begin = @rstring , _end = @cstring;';
DECLARE @ParmDefinition nvarchar(500)
SET @ParmDefinition = N'@rstring datetime, @cstring datetime'
DECLARE @regdate DATETIME = GETDATE(), @compdate DATETIME = DATEADD(DAY, 1, GETDATE());
EXECUTE sp_ExecuteSQL @SQL, @ParmDefinition,
@rstring = @regdate, @cstring = @compdate;
结果:
_begin _end
----------------------- -----------------------
2012-06-13 10:28:47.657 2012-06-14 10:28:47.657
我没有看到输出仍然包含文字“@rstring”并且没有被参数正确替换的方法,除非您仍然有拼写或其他变量名称不一致。我怀疑这个查询还有什么比你向我们展示的更多。