Using a variable to rename a column

  • Does anyone know how to rename a column using a variable? 

    DECLARE  @user1 varchar(50)

    SELECT @User1 = 'New_Column_Name'

    SELECT User1 AS @user1

    The AS clause doesn't work, nor does an = sign.  The problem is that I have is that table 1 defines the labels of user defined fields in table 2 I am querying from.  I would like the output to use the from table 2 to have the proper column lables as defined in table 1.

    Thanks

    Curtis

  • DECLARE  @user1 varchar(50)

    SELECT @user1 = 'New_Column_Name'

    declare @sentencia_sql nvarchar(1000)

    declare @Sal int

     

    select @sentencia_sql='SelECT User1 AS ' + @user1 + '

    from _user'

    EXECUTE @Sal=sp_executesql @sentencia_sql

    --if @Sal<>0 goto manejo_error

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

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