[英]Adding parameters to a dynamic sql query
My sql procedure is 我的SQL过程是
SET @query='UPDATE '+@tbl+' SET auth_Date='''+@authDate+'''
,authorized_By_ID=@AuthID
,authorized=1 WHERE '+@col+'=@Id;
IF(@@ERROR=0) SET @reVal=1 ELSE SET @reVal=-1'
EXECUTE sp_executesql @query, N'@reVal int OUTPUT',
@reVal,N'@AuthID int',@AuthID,N'@Id int',@Id
Here @AuthID is Datetime, @AuthID and @Id is int 这里@AuthID是Datetime,@ AuthID和@Id是int
1) How can I pass arguments . 1)如何传递参数。 2) There is an error is coming Conversion failed when converting date and/or time from character string.
2) Conversion failed when converting date and/or time from character string.
出现错误, Conversion failed when converting date and/or time from character string.
You pass argument definition in the second parameter, followed by the arguments, in the order declared: 您在第二个参数中传递参数定义,然后按声明的顺序传递参数:
EXECUTE sp_executesql @query,
N'@AuthID int, @Id int, @reVal int OUTPUT',
@AuthID, @Id, @reVal OUTPUT;
Any reason why you don't pass @authDate also as a parameter? 为何不将@authDate也作为参数传递的任何原因? specially since datetime types are notoriously error prone when converted to strings, due to locale settings. 特别是因为日期时间类型由于语言环境设置而在转换为字符串时极易出错。
Last, you should use TRY/CATCH blocks instead of @@ERROR checks . 最后,您应该使用TRY / CATCH块,而不是@@ ERROR检查 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.