Dynamic SQL column names

  • Is there a way to create a table with columns based on sql variables (i.e. create table #table1 (@strColumnName varchar(20) null)

  • Yep something like:

    declare @c char(10)

    declare @cmd char(100)

    set @c = 'test_column'

    set @cmd = 'create table tempdb.dbo.test (' + @c + ' int)'

    exec(@cmd)

    insert into tempdb.dbo.test values (1)

    select * from tempdb.dbo.test

    drop table tempdb.dbo.test

    Gregory Larsen, DBA

    If you looking for SQL Server Examples check out my website at http://www.geocities.com/sqlserverexamples

    Gregory A. Larsen, MVP

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

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