?
Solved

Create table dynamically while passing the table name

Posted on 2016-10-03
2
Medium Priority
?
436 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 51

Accepted Solution

by:
Vitor Montalvão earned 2000 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

777 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