use variables to assign result set column names

  • I have a sp that returns a column for each month of the most recent quarter. Is there a way to return the month name as the column name?

  • You could do it dynamically by building a the SQL string in a variable object concatinating the names in the way you want and then executing the variable.

    Ex.

    DECLARE @SQLStr AS VARCHAR(500)

    SET @SQLStr = 'SELECT col1 AS ' + WHATEVERTOGETTHEMONTHNAME + ' FROM tblX'

    EXE(@SQLStr)

    However, you break the ownership chain and may have to give SELECT rights to the underlying table itself. Let me know if you need a better example.

  • cool,

    I can get it to work using

    execute sp_executesql @stmt

    is that what you mean?

    is that the most efficient way?

  • just exec( @stmt)

    Steve Jones

    steve@dkranch.net

  • And you do have to have the ()'s around your variable.

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

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