Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Chaning a column to IDENTITY
Message
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Divers
Thread ID:
00857384
Message ID:
00858045
Vues:
20
>I was wondering if it is possible to alter an existing column through ALTER TABLE to be an identity column or can that only be done through adding a new column / creating a new table?

Hi Jon,

The ALTER TABLE command doesn't provide functionality to add or remove IDENTITY property of a column. EM does that with series of T-SQL commands/stored procedure. You can add IDENTITY property to the existing colum in EM and than select Save Change Script to see it. Here's what I got doing that on on a test table.
BEGIN TRANSACTION
SET QUOTED_IDENTIFIER ON
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
COMMIT
BEGIN TRANSACTION
CREATE TABLE dbo.Tmp_zt
	(
	iii int NOT NULL IDENTITY (1, 1),
	ccc varchar(32) NULL
	)  ON [PRIMARY]
GO
SET IDENTITY_INSERT dbo.Tmp_zt ON
GO
IF EXISTS(SELECT * FROM dbo.zt)
	 EXEC('INSERT INTO dbo.Tmp_zt (iii, ccc)
		SELECT iii, ccc FROM dbo.zt TABLOCKX')
GO
SET IDENTITY_INSERT dbo.Tmp_zt OFF
GO
DROP TABLE dbo.zt
GO
EXECUTE sp_rename N'dbo.Tmp_zt', N'zt', 'OBJECT'
GO
COMMIT
--sb--
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform