在 SP 内执行动态查询的最佳方法
我在 SP 内执行动态查询时遇到一些问题,我想寻求一些帮助,因为无论我尝试什么,我都无法正确执行它:
我已经尝试过:
SET @subWorksQuery =
'UPDATE JK_SubscriberWorks SET ' +
'update_date = convert(datetime, ''' + @dateNow + ''', 103), ' +
'challenge_' + convert(nvarchar(2), @challengeDay) + '_q = ''' + @challengeQuestion + ''', ' +
'challenge_' + convert(nvarchar(2), @challengeDay) + '_a = ''' + @challengeAnswer + ''' ' +
'WHERE subscriberwork_id = '' + convert(nvarchar(10), @subscriberWorksId) + '';';
execute @execReturn = @subWorksQuery
但我总是得到:
消息 203,级别 16,状态 2,过程 sp_InsertChallengeResponse_test, 112路
名称 'UPDATE JK_SubscriberWorks SET update_date = Convert(datetime, '23-12-2011 23:35:17', 103),challenge_23_q = 'Hvilket år blev Klasseloteriet omdannet til et aktieselskab?
Få hjælp til svaret。', Challenge_23_a = '1992' 其中subscriberwork_id = ' + 转换(nvarchar(10),@subscriberWorksId)+';'不是有效的 标识符。
从该错误中删除 UPDATE 语句并独立运行它,它会运行并执行更新
如果我使用 sp_executesql
就像
SET @subWorksQuery =
N'UPDATE JK_SubscriberWorks SET ' +
'update_date = @a, ' +
'challenge_' + convert(nvarchar(2), @challengeDay) + '_q = @b, ' +
'challenge_' + convert(nvarchar(2), @challengeDay) + '_a = @c ' +
'WHERE subscriberwork_id = @d;';
SET @parmDefinition = N'@a datetime, @b nvarchar(250), @c nvarchar(500), @d decimal';
execute sp_executesql
@subWorksQuery,
@parmDefinition,
@a = @CreateDate, @b = @challengeQuestion, @c = @challengeAnswer, @d = @subscriberWorksId;
它从不执行 UPDATE,但不会抛出任何错误。
我在这里缺少什么?
I'm having some trouble executing a dynamic query inside my SP, and I thought asking for some help as I can't execute it correctly no matter what I try:
I have tried:
SET @subWorksQuery =
'UPDATE JK_SubscriberWorks SET ' +
'update_date = convert(datetime, ''' + @dateNow + ''', 103), ' +
'challenge_' + convert(nvarchar(2), @challengeDay) + '_q = ''' + @challengeQuestion + ''', ' +
'challenge_' + convert(nvarchar(2), @challengeDay) + '_a = ''' + @challengeAnswer + ''' ' +
'WHERE subscriberwork_id = '' + convert(nvarchar(10), @subscriberWorksId) + '';';
execute @execReturn = @subWorksQuery
but I always get:
Msg 203, Level 16, State 2, Procedure sp_InsertChallengeResponse_test,
Line 112
The name 'UPDATE JK_SubscriberWorks SET update_date = convert(datetime, '23-12-2011 23:35:17', 103), challenge_23_q =
'Hvilket år blev Klasselotteriet omdannet til et aktieselskab?
Få hjælp til svaret.',
challenge_23_a = '1992' WHERE subscriberwork_id = ' +
convert(nvarchar(10), @subscriberWorksId) + ';' is not a valid
identifier.
Removing the UPDATE statement from that error and run it independently, it runs and performs the update
If I use sp_executesql
like
SET @subWorksQuery =
N'UPDATE JK_SubscriberWorks SET ' +
'update_date = @a, ' +
'challenge_' + convert(nvarchar(2), @challengeDay) + '_q = @b, ' +
'challenge_' + convert(nvarchar(2), @challengeDay) + '_a = @c ' +
'WHERE subscriberwork_id = @d;';
SET @parmDefinition = N'@a datetime, @b nvarchar(250), @c nvarchar(500), @d decimal';
execute sp_executesql
@subWorksQuery,
@parmDefinition,
@a = @CreateDate, @b = @challengeQuestion, @c = @challengeAnswer, @d = @subscriberWorksId;
It never performs the UPDATE, but does not throw any error.
What am I missing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
像这样运行它:
[你不会从变量中的更新语句中得到任何返回,并且你不能像这样运行
execute (@execReturn = @subWorksQuery)
]没有括号看起来开始解析,假设它是一个存储过程名称,但当它达到最大长度时失败。
也就是说,最好使用
sp_executesql
带参数。Run it like this:
[you won't be getting anything back from the update statement in the variable, and you can't run like this
execute (@execReturn = @subWorksQuery)
]Without parentheses it seems to be starting parsing, assuming it is a stored procedure name, but failing when it hits the max length for one.
In saying that, it is better to use
sp_executesql
with parameters.我不确定您在返回值中寻找什么,但如果您只需要受影响的行数,那么应该很容易获得。
更改:
至:
I am not sure what you are looking for in the return value, but if you just need the count of rows affected, that should be easy to obtain.
Change:
to:
只是一个想法...您的 @d 参数是一个十进制值。你的id是int吗?是否存在可能的数据类型冲突?
你的 sp 输入参数是如何定义的?你能发布完整的sp吗?
戴夫
just a thought...your @d parameter is a decimal value. Is your id an int? is there a possible data type conflict?
how are your sp input parameters defined? Could you post the full sp?
Dave