edgarmtze
edgarmtze

Reputation: 25058

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 you fix this query?

Upvotes: 0

Views: 34

Answers (1)

mamcx
mamcx

Reputation: 16196

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.

Upvotes: 1

Related Questions