Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Creating a table with a dynamic name.
Message
From
21/11/2002 10:34:02
 
 
To
All
General information
Forum:
Microsoft SQL Server
Category:
Stored procedures, Triggers, UDFs
Title:
Creating a table with a dynamic name.
Miscellaneous
Thread ID:
00725229
Message ID:
00725229
Views:
31
How can you create a table name and field name dynamically? Given the following code I create a table named @zobjectname rather than what @zobjectname evaluates to. Additionally I the field name is literally @objname + 'id' with the plus sign and the single quotes.
CREATE PROCEDURE dbo.CreateNewObj 
		@objName as char(255) = NULL -- If we pass 'FRED'
AS
-- Snipped validation code  

   declare @zobjectname as char (50)
   set @zobjectname = 'dbo.zobj' + @objname -- this would be 'zobjFRED'

CREATE TABLE @zobjectname 
(
	@objname + 'id' numeric IDENTITY(1,1),
	zFile varchar(25) 
)

-- Snipped more
Thanks in advance,
~Joe Johnston USA

"If ye love wealth better than liberty, the tranquility of servitude better than the animated contest of freedom, go home from us in peace. We ask not your counsel or arms. Crouch down and lick the hands which feed you. May your chains set lightly upon you, and may posterity forget that ye were our countrymen."
~Samuel Adams

Next
Reply
Map
View

Click here to load this message in the networking platform