Solved

Create table dynamically while passing the table name

Posted on 2016-10-03
2
420 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 49

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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.

740 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