How to write a stored procedure to create a table using a parameter

Hi,

I have a stored procedure, that i want to use to create a table. I want to take in a parameter and use this parameter as the table name.

=========================
CREATE PROC proc1
@myTblName nvarchar(15)
AS
BEGIN

CREATE TABLE [@myTblName] (

[id] [bigint] NOT NULL,

[col1] [nvarchar](50) NULL,

[col2] [int] NULL,

[col3] [nchar](10) NULL,

[col4] [money] NULL,

[col5] [money] NULL,

[col6] [bigint] NULL,)

END
GO
===============================

This stored procedure runs, but not take in the parameter. it will only create a table with the name of "@myTblName". can anyone help? thanks
CRIITAsked:
Who is Participating?
 
Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:
CREATE PROC proc1
@myTblName nvarchar(15)
AS
BEGIN

exec ('
CREATE TABLE [' + @myTblName + '] (

[id] [bigint] NOT NULL,

[col1] [nvarchar](50) NULL,

[col2] [int] NULL,

[col3] [nchar](10) NULL,

[col4] [money] NULL,

[col5] [money] NULL,

[col6] [bigint] NULL)
')

END
0
 
CRIITAuthor Commented:
cool, this is it, thanks a lot
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.