简体   繁体   English

SQL Server“varchar值的转换'99101500728'溢出了一个int列。”

[英]SQL Server “The conversion of the varchar value '99101500728' overflowed an int column.”

This is my issue: when I run this query, there is no problem and returns the results as expected 这是我的问题:当我运行此查询时,没有问题并按预期返回结果

SELECT * 
FROM a 
INNER JOIN b ON a.OpId = b.OpId
INNER JOIN c ON b.AdId = c.AdId
INNER JOIN d ON c.AdId = d.AdId
INNER JOIN e ON a.CId = e.CId
INNER JOIN f ON b.OpId = f.OpId
INNER JOIN g ON a.AdRId = g.AdRId
INNER JOIN s ON c.AdSId = s.AdSId 
WHERE f.document = '52147896'

However, when I try to execute it using the EXEC command, it does not work!! 但是,当我尝试使用EXEC命令执行它时,它不起作用!!

Here is the problematic code 这是有问题的代码

declare @document varchar(20) = '52147896' --This is the correct type for this attribute since I took it directly from the table type
DECLARE @SELECT2 nvarchar(max) = 'SELECT * FROM a inner join  b ON 
a.OpId = b.OpId
INNER JOIN c ON b.AdId = c.AdId
INNER JOIN d ON c.AdId = d.AdId
INNER JOIN e ON a.CId = e.CId
INNER JOIN f ON b.OpId = f.OpId
INNER JOIN g ON a.AdRId = g.AdRId
INNER JOIN s ON c.AdSId = s.AdSId WHERE 
f.document = '+@document

And this is the error I get: 这是我得到的错误:

Msg 248, Level 16, State 1, Line 1 Msg 248,Level 16,State 1,Line 1
The conversion of the varchar value '99101500728' overflowed an int column. varchar值'99101500728'的转换溢出了一个int列。

Can someone help me with this issue? 有人可以帮我解决这个问题吗?

Thanks in advance 提前致谢

If the type is a string, then inclose the value in single quotes: 如果类型是字符串,则将值括在单引号中:

. . . 
f.document = '''+ @document + ''''

Better yet, use parameters and pass the value in using sp_executesql . 更好的是,使用参数并使用sp_executesql传递值。

The difference that's causing the error lies on this line: 造成错误的区别在于这一行:

f.document = '52147896' -- Expression without EXEC

versus

f.document = 52147896 -- Expression with EXEC

On the second case, since you are comparing against an integer value, the SQL enging will convert the column f.document to integer to do the comparison, as stated by the data type precedence . 在第二种情况下,由于您要与整数值进行比较,因此SQL f.document会将列f.document转换为整数以进行比较,如数据类型优先级所述 When it tries to convert, the varchar value '99101500728' is too big for an int a fails. 当它尝试转换时,varchar值'99101500728'对于int失败来说太大了。

To solve it, write your value as string literal by adding single quotes before and after: 要解决此问题,请通过在前后添加单引号将您的值写为字符串文字:

declare @document varchar(20) = '52147896' --This is the correct type for this attribute since I took it directly from the table type
DECLARE @SELECT2 nvarchar(max) = 'SELECT * FROM a inner join  b ON 
a.OpId = b.OpId
INNER JOIN c ON b.AdId = c.AdId
INNER JOIN d ON c.AdId = d.AdId
INNER JOIN e ON a.CId = e.CId
INNER JOIN f ON b.OpId = f.OpId
INNER JOIN g ON a.AdRId = g.AdRId
INNER JOIN s ON c.AdSId = s.AdSId WHERE 
f.document = '''+@document + ''''

Your where clause is passing an integer value. 您的where子句传递一个整数值。

WHERE f.document = 52147896

This is causing an implicit conversion of f.document to an INT data type. 这导致f.document隐式转换为INT数据类型。 One of your ID values has a character string of 99101500728, but this will not convert to an INT data type. 您的一个ID值的字符串为99101500728,但这不会转换为INT数据类型。

You can fix your dynamic SQL by escaping the single quotes so that your WHERE clause passes the value as a string 您可以通过转义单引号来修复动态SQL,以便WHERE子句将值作为字符串传递

'WHERE f.document = ''' + @document + ''''

which will generate 这将产生

WHERE f.document = '52147896'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM