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 you fix this query?
You can use the CAST function.
The error happened because you try to join a
CHARwith aINT. TheCHARin this case is the ‘SELECT ‘ string.