Home Forums SQL Server 2008 T-SQL (SS2K8) sp_executesql dynamic columns in select statement RE: sp_executesql dynamic columns in select statement

  • @cmd = N'select @col1 from dbo.testtable'

    I am not sure if this correct and would need to test but if you give it a try I would think something like this may work

    @cmd = N'select ' + @col1 + 'from dbo.testtable'

    The other way is which may work is

    declare @SQLString AS VARCHAR(max)

    set @SQLString = 'select ' + @col1 + 'from dbo.testtable'

    @cmd = @SQLString

    My home server is done when I get into my office I will give this a try

    Scott
    I am a Senior Data Analyst/DBA
    I work with MS SQL, Oracle & several ETL Tools

    @thewolf66[/url]