Using a Returned Value As A Table Name

  • Hi All

    I am trying to use a function to build the name of a table I have in a database. I want to use that returned value in a Join. Not sure if I am doing something wrong or if this is even possible.

    SELECT * FROM Coverages c

    LEFT JOIN (SELECT dbo.fn_Build_ADM_Table_Name(@ReinsYear, 'Dates')) AS adm

    ON adm.State_Code = c.State_Code

    Thanks in advance.

    Craig

  • you can definetly use a function in a join but without variables.. in case you want to use variables try using Dynamic SQL..

    DECLARE @sql NVARCHAR(3000)

    SET @sql = N 'SELECT * FROM Coverages c

    LEFT JOIN (SELECT dbo.fn_Build_ADM_Table_Name('+@ReinsYear+', 'Dates')) AS adm

    ON adm.State_Code = c.State_Code '

    EXECUTE(@SQL)

Viewing 2 posts - 1 through 1 (of 1 total)

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