Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Creating a table with a dynamic name.
Message
 
 
General information
Forum:
Microsoft SQL Server
Category:
Stored procedures, Triggers, UDFs
Miscellaneous
Thread ID:
00725229
Message ID:
00725269
Views:
9
This message has been marked as the solution to the initial question of the thread.
Hi Joe,

You've to build SQL string and than execute it with EXECUTE command or sp_execute sql system stored procedure.
DECLARE @ddl varchar(1024)
SET @ddl = 'CREATE TABLE dbo.zobj' + @objname +  ' ( ' + 
	@objname + 'id numeric IDENTITY(1,1), ' +
	'zFile varchar(25) )'
EXECUTE (@ddl)
>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,
--sb--
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform