Solved

Create table dynamically while passing the table name

Posted on 2016-10-03
2
398 Views
Last Modified: 2016-10-03
Hi Experts,

I want to pass the table name dynamically to the following query and get it created:

DECLARE @SQL varchar(MAX);
	DECLARE @TableName varchar(255);

	SET @SQL = '"CREATE TABLE +  @TableName+ (
		[ID] [int] IDENTITY(1,1) NOT NULL,
		[AgencyName] [nvarchar](255) NULL,
		[ServiceID] [nvarchar](255) NULL,
	
		) ON [PRIMARY]"';

execute (@SQL);

Open in new window


And usually, I got an error with the single quotation. So, could you please tell me what the correct way to get it working?

Thanks a lot in advance.
Harreni
0
Comment
Question by:Harreni
2 Comments
 
LVL 45

Accepted Solution

by:
Vitor Montalvão earned 500 total points
ID: 41826159
Try this and see the difference:
SET @SQL = 'CREATE TABLE ' +  @TableName+ ' (
		[ID] [int] IDENTITY(1,1) NOT NULL,
		[AgencyName] [nvarchar](255) NULL,
		[ServiceID] [nvarchar](255) NULL,
		) ON [PRIMARY]'

Open in new window

0
 

Author Closing Comment

by:Harreni
ID: 41826200
Thanks a lot Vitor, It works 100%
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now