Friday, February 24, 2012

column name

Hi friends,

why the following does not work?

declare @.name VARCHAR(20)
set @.name = 'my_l_name'
select l_name as @.name from person

with regards,

You have to use dynamic sql for this:

DECLARE @.SQLSTRING VARCHAR(500)
declare @.name VARCHAR(20)
set @.name = 'my_l_name'
SET @.SQLSTRING = 'select l_name as ' + @.name + ' from person'
EXEC(@.SQLSTRING)

HTH, Jens Suessmeyer.

|||To add to Jens, check this out: http://www.sommarskog.se/dynamic_sql.html
--
Frank Kalis
Microsoft SQL Server MVP
http://www.insidesql.de

No comments:

Post a Comment