use variable as column name

  • can any one tell me about how to write the code for the above case

  • DECLARE @Column VARCHAR(100),

    @SQL NVARCHAR(MAX)

    SELECT @Column = 'name',

    @SQL = 'SELECT [' + @Column + '] FROM sys.tables'

    EXECUTE( @SQL )

    EXECUTE sp_executesql @SQL

    --Ramesh


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

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