Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Sep 2006
    Location
    India Mumbai
    Posts
    248
    Thanks
    13
    Thanked 1 Time in 1 Post

    problem in passing dynamic values to MSSQL query

    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
    Code:
    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

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    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......
    I am the luckiest man in the world


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •