Create a table using a variable

How can I create a table within a stored procedure using a variable passed in.. I keep getting an error..??

I'm using the follwing :

CREATE PROCEDURE usr_Create_ERS_HH_USAGE
@TableName nchar(50)
AS

if not exists (select * from sysobjects where id = object_id(@TableName) and OBJECTPROPERTY(id, N'IsUserTable') = 1)

begin
 set @TableName = '[dbo].' + @TableName
 print @TableName
 
 
    print 'Table does not exists'
    CREATE TABLE @TableName
(
       <Table Internals>
)
end

I get an error on the  create command
simloxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

simonsabinCommented:
Use this

CREATE PROCEDURE usr_Create_ERS_HH_USAGE
@TableName nchar(50)
AS

if not exists (select * from sysobjects where id = object_id(@TableName) and OBJECTPROPERTY(id, N'IsUserTable') = 1)

begin
  declare @SQL varchar(8000)

 set @SQL = ' CREATE TABLE ' + @TableName + '( ..... )'

 print @TableName
 
 
    print 'Table does not exists'
EXEC (@SQL)

You will need to put the column definititions where the ... are.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DredwickCommented:
doh! so simple too, you helped me out as well!
Thanks,

Justin
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.