Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Creating a table with a dynamic name.
Message
 
 
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Stored procedures, Triggers, UDFs
Divers
Thread ID:
00725229
Message ID:
00725269
Vues:
10
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--
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform