开发者

Why is this error of convertion type SQL Server 2008

开发者 https://www.devze.com 2023-02-13 21:00 出处:网络
I create a table like CREATE TABLE #tab(ID INT,Value FLOAT) inside a loop (@n is counter)I insert elements dynamicaly:

I create a table like

CREATE TABLE #tab(ID INT,Value FLOAT)

inside a loop (@n is counter)I insert elements dynamicaly:

SET @sql = 'INSERT #tab(ID,Value) SELECT '+@n+', SUM('+@x+'*'+@y+') FROM '+ @table_name; when attempt to execute this:

Conversion failed when converting the varchar value 'INSERT #tab  (ID,Value) SELECT ' to data type int.

I don't understand why it says convertion, as id is defined as INT.

How do y开发者_如何学编程ou fix this query?


You can use the CAST function.

'SELECT '+ CAST(@n AS VARCHAR) +', SUM('+@x+'*'+@y+') FROM '

The error happened because you try to join a CHAR with a INT. The CHAR in this case is the 'SELECT ' string.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号