消息 8114:将数据类型 varchar 转换为数字时出错
我正在 SSMS 2008 中编写动态查询,但收到上述错误。这是什么原因呢?
我的查询:
SELECT TOP 10 *
FROM
ALL_COMPLAINTS A
JOIN
#TEMP5 B ON A.QXP_EXCEPTION_NO = B.QXP_EXCEPTION_NO
AND A.[LEVEL] = B.[LEVEL]
AND A.[QXP_REPORT_DATE] = B.[QXP_REPORT_DATE]
WHERE
A.QXP_REPORT_DATE >= CONVERT(DATETIME, '' + @FirstMonthDate + ' 00:00:00', 102)
AND
A.QXP_REPORT_DATE <= CONVERT(DATETIME, '' + @LastMonthDate + ' 23:59:59', 102)
AND
A.QXP_SHORT_DESC <> 'Design Control' AND A.LEVEL = ' + CAST(@TheLevel AS VARCHAR(5)) + '
我知道 A.Level
是数字,而且我也知道如果我只删除 A.Level
部分,我不会收到任何错误。但是,我不确定是否正确转换 @TheLevel
,因为这是动态 SQL。
I am writing a dynamic query in SSMS 2008 and I get the above error. What is the cause of this?
My query:
SELECT TOP 10 *
FROM
ALL_COMPLAINTS A
JOIN
#TEMP5 B ON A.QXP_EXCEPTION_NO = B.QXP_EXCEPTION_NO
AND A.[LEVEL] = B.[LEVEL]
AND A.[QXP_REPORT_DATE] = B.[QXP_REPORT_DATE]
WHERE
A.QXP_REPORT_DATE >= CONVERT(DATETIME, '' + @FirstMonthDate + ' 00:00:00', 102)
AND
A.QXP_REPORT_DATE <= CONVERT(DATETIME, '' + @LastMonthDate + ' 23:59:59', 102)
AND
A.QXP_SHORT_DESC <> 'Design Control' AND A.LEVEL = ' + CAST(@TheLevel AS VARCHAR(5)) + '
I know that A.Level
is numeric and I also know that I do not get any errors if I just remove the A.Level
portion. However, I am not certain I am casting @TheLevel
correctly since this is dynamic SQL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的引号在最后一行被搞乱了,你不能在同一个语句中混合动态和非动态 SQL。还假设@TheLevel是一个数字,如果@TheLevel是一个字符,您需要将其转换为数字(我假设在这种情况下为int)
如果您需要动态部分,那么预先评估它然后键入可能是一个很好的做法它进入非动态查询
Your quotes are messed up in the last line, you cant mix dynamic and non dynamic SQL in the same statement. Also assuming @TheLevel is a numeric, if @TheLevel is a char, you need to convert it to a numeric (int I assume in this case)
If you need a dynamic portion then it is probably good practice to pre evaluate it and then key it into the non dynamic query