Avatar of Harreni
Harreni
Flag for Saudi Arabia asked on

Create table dynamically while passing the table name

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
Microsoft SQL Server

Avatar of undefined
Last Comment
Harreni

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Vitor Montalvão

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Harreni

ASKER
Thanks a lot Vitor, It works 100%
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy