delimiter in a variable

  • Hi All,

    In the following query,

    SELECT DISTINCT o.name, o.xtype FROM syscomments c INNER JOIN sysobjects o ON c.id=o.id WHERE c.TEXT LIKE '%TempTable%'

    I want to make TempTable as variable as in, I want to make the following query

    SET @tableName = 'TempTable'

    SET @sql = 'SELECT DISTINCT o.name, o.xtype FROM syscomments c INNER JOIN sysobjects o ON c.id = o.id WHERE c.TEXT LIKE %'+ @tableName+ '%'

    EXEC (@sql)

    I am not able to do this as I can not put delimiter in a query.

    Can anybody please help?

    Thank you!

  • are you not able to use it like this

    declare @Tablename varchar(10)

    set @tablename = 'tablename'

    SELECT DISTINCT o.name, o.xtype FROM syscomments c INNER JOIN sysobjects o ON c.id = o.id WHERE c.TEXT LIKE '%'+ @tableName+ '%'

    Jayanth Kurup[/url]

  • No. I want the delimiters before and after % like '%AOC_CompanyCode%'

  • try this

    set @tablename = CHAR(37)+'ticker'+char(37)

    select @tablename

    SELECT DISTINCT o.name, o.xtype FROM syscomments c INNER JOIN sysobjects o ON c.id = o.id WHERE c.TEXT LIKE @tableName

    Jayanth Kurup[/url]

  • I did not understand this. Could you please explain?

    and above did not work. error was "A severe error occurred on the current command."

  • I was able to execute this successfully with out any errors , could you explain how your executing the query ?

    declare @tablename varchar(20)

    --Declared a variable to store search term

    set @tablename = CHAR(37)+'ticker'+char(37)

    -- Set value for search term using char(37) which is Asci char map for "%"

    select @tablename

    SELECT DISTINCT o.name, o.xtype FROM syscomments c INNER JOIN sysobjects o ON c.id = o.id WHERE c.TEXT LIKE @tableName

    -- Above query passes value @tablename as '%ticker%'

    Jayanth Kurup[/url]

  • For me its giving the value of @tablename as %ticker%

  • I used CHAR(39) ..Ascii value for single quote

Viewing 8 posts - 1 through 7 (of 7 total)

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