using varchar variable to query a table

  • Hello All

    this query doesnot work.

    column employeeid has datatype char ()

    declare @ab varchar (100)

    set @ab='''483503'''

    select * from tm_temp_empmastall where employeeid=@ab

  • You only need to wrap the value in single quotes:

    declare @ab varchar (100)

    set @ab='483503'

    select * from tm_temp_empmastall where employeeid=@ab

  • i want multiple employeeids in @ab

    set @ab='483503,483504'

    and then

    select * from tm_temp_empmastall where employeeid in (@ab)

  • Use dynamic SQL to complete your query, and you can send the amount of employee you want:

    Here's an example:

    DECLARE @sql NVARCHAR(1000)

    DECLARE @ab NVARCHAR(100)

    set @ab = '''483503'',''483504'''

    SET @sql = 'select * from tm_temp_empmastall where employeeid in ( ' + @ab

    + ' )'

    PRINT @sql

    EXEC sp_executeSQL @sql

    Hope this helps,

    J-F

    Cheers,

    J-F

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply