Daniel Sh.
Daniel Sh.

Reputation: 2074

Int Variable inside Nvarchar Statement

The following code sends error.

@resp2 is INT, it's the result of a sum preiously done. So now I want to update some row in another table using a dinamic statement.

SET @SQL = 'update TelepromTableNamesInfo set [Resp] = '+@RESP2+'
                where nombre = ''' + @TableWBraq + ''''
EXEC (@SQL)

First thing I've tried is '''+@resp2+''' But I don't want it be

' variable value '

since it's an INT value and there's no need for ''

The error makes sence. I can't put some INT value into a string. I'd use cast or convert but how can I do it inside the statement? Or maybe I'm approaching the update from the wrong perspective?

Thanks.

EDIT

Solved.

'+ cast(@RESP2 as nvarchar(7))+'

It was easier than I thought, thanks.

Upvotes: 0

Views: 1965

Answers (2)

Jamiec
Jamiec

Reputation: 136114

SET @SQL = 'update TelepromTableNamesInfo set [Resp] = '+ CAST(@RESP2 AS VARCHAR(50)) +'
                where nombre = ''' + @TableWBraq + ''''
EXEC (@SQL)

Upvotes: 2

sarwar026
sarwar026

Reputation: 3821

May be single quote is creating the problem. Please give a try the following:

SET @SQL = 'update TelepromTableNamesInfo set [Resp] = '+@RESP2+
                'where nombre = '"' + @TableWBraq + '"'
EXEC (@SQL)

Upvotes: 0

Related Questions