How to give aliase name to column dynamically

  • Hi,

    I want to give the aliase name to column,

    below is the test code.

    Create table #Temp

    (

    Number Int,

    Name Varchar(100)

    )

    iNSERT iNTO #Temp

    values (1,'Test')

    Declare @STR As varchar(100) = 'UserName'

    select Number,Name As @STR from #Temp

    Please help me.

    Thanks in Advance!

  • EXEC ('select Number, Name As ' + @STR + ' from #Temp')

    ---------------------------------------------------------

    It takes a minimal capacity for rational thought to see that the corporate 'free press' is a structurally irrational and biased, and extremely violent, system of elite propaganda.
    David Edwards - Media lens[/url]

    Society has varying and conflicting interests; what is called objectivity is the disguise of one of these interests - that of neutrality. But neutrality is a fiction in an unneutral world. There are victims, there are executioners, and there are bystanders... and the 'objectivity' of the bystander calls for inaction while other heads fall.
    Howard Zinn

  • Hi thanks Abu,

    Is any other alternative way for this.

  • Afraid not... you have to use dynamic SQL for this kind of thing.

    ---------------------------------------------------------

    It takes a minimal capacity for rational thought to see that the corporate 'free press' is a structurally irrational and biased, and extremely violent, system of elite propaganda.
    David Edwards - Media lens[/url]

    Society has varying and conflicting interests; what is called objectivity is the disguise of one of these interests - that of neutrality. But neutrality is a fiction in an unneutral world. There are victims, there are executioners, and there are bystanders... and the 'objectivity' of the bystander calls for inaction while other heads fall.
    Howard Zinn

  • What's the purpose here? Why do you want a column dynamically named (I pity the front end app which uses that)

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass

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

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