PDA

View Full Version : problem in passing dynamic values to MSSQL query


bhakti_thakkar
05-20-2010, 08:00 AM
DECLARE @t NVARCHAR(MAX) , @ColName NVARCHAR(MAX) , @Value int , @tablename varchar(255)
set @tablename = 'Relation_T'
set @ColName = 'Relation_ID'
set @Value = '1'

SET @t = 'SELECT * FROM '+quotename(@tablename)+' WHERE ' + @ColName + ' = ' + @Value
print @t
--EXECUTE sp_Executesql @t

I am getting error

Msg 245, Level 16, State 1, Line 6
Conversion failed when converting the nvarchar value 'SELECT * FROM [Relation_T] WHERE Relation_ID = ' to data type int.


please help

Roelf
06-15-2010, 05:15 PM
you could try to CONVERT the @Value variable to NVARCHAR before concatenate it to the rest of the string.

Does this line work:
set @Value = '1'
because the @Value variable is defined as int......