Why is this error of convertion type SQL Server 2008
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.
精彩评论