Create Table Name Dynamically

  • I am trying to create a table with a name that is passed to a stored procedure. It will not compile with the Create Table @tablename. Is there a way around this problem or something else I can do to get the same results.

  • You can use a dynamic SQL statement, but you will have to ensure that your user has rights to create tables.

    declare @sql varchar(1000)

    declare @tablename varchar(50)

    set @tablename = 'newtable'

    set @sql = 'create table ' + @tablename + ' (field1 int not null)'

    exec sp_executesql (@sql)

  • Thanks, worked great.

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

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