Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
albear
Starting Member
2 Posts |
Posted - 2006-10-30 : 12:07:39
|
i have a rather intresting problem i need to change all the identity colums from Yes, to Yes, NOT FOR REPLICATION, in quite a few databasesnow the only way i can find of doing this is the say SQL server does it its self see code below BEGIN TRANSACTIONSET QUOTED_IDENTIFIER ONSET TRANSACTION ISOLATION LEVEL SERIALIZABLESET ARITHABORT ONSET NUMERIC_ROUNDABORT OFFSET CONCAT_NULL_YIELDS_NULL ONSET ANSI_NULLS ONSET ANSI_PADDING ONSET ANSI_WARNINGS ONCOMMITBEGIN TRANSACTIONALTER TABLE dbo.AllowedTemplates DROP CONSTRAINT FK_AllowedTemplates_ContentGOCOMMITBEGIN TRANSACTIONALTER TABLE dbo.AllowedTemplates DROP CONSTRAINT DF_AllowedTemplates_AT_DisplayOrderGOCREATE TABLE dbo.Tmp_AllowedTemplates ( ATID int NOT NULL IDENTITY (1, 1) NOT FOR REPLICATION, PTID int NOT NULL, CID int NOT NULL, AT_DisplayOrder int NOT NULL, ParentPTID int NULL ) ON [PRIMARY]GOALTER TABLE dbo.Tmp_AllowedTemplates ADD CONSTRAINT DF_AllowedTemplates_AT_DisplayOrder DEFAULT (0) FOR AT_DisplayOrderGOSET IDENTITY_INSERT dbo.Tmp_AllowedTemplates ONGOIF EXISTS(SELECT * FROM dbo.AllowedTemplates) EXEC('INSERT INTO dbo.Tmp_AllowedTemplates (ATID, PTID, CID, AT_DisplayOrder, ParentPTID) SELECT ATID, PTID, CID, AT_DisplayOrder, ParentPTID FROM dbo.AllowedTemplates TABLOCKX')GOSET IDENTITY_INSERT dbo.Tmp_AllowedTemplates OFFGODROP TABLE dbo.AllowedTemplatesGOEXECUTE sp_rename N'dbo.Tmp_AllowedTemplates', N'AllowedTemplates', 'OBJECT'GOALTER TABLE dbo.AllowedTemplates ADD CONSTRAINT PK_AllowedTemplates PRIMARY KEY CLUSTERED ( ATID ) WITH FILLFACTOR = 90 ON [PRIMARY]GOALTER TABLE dbo.AllowedTemplates WITH NOCHECK ADD CONSTRAINT FK_AllowedTemplates_Content FOREIGN KEY ( CID ) REFERENCES dbo.Content ( CID )GOCOMMITnow i either need another way to do this (which i think is impossable) or i need a way to script all the tables within a database into this format, just wondering if anyone had come across this before and was able to help in any way. |
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2006-10-30 : 12:20:21
|
how aboutalter table AllowedTemplates drop column ATID alter table AllowedTemplates add ATID int NOT NULL IDENTITY (1, 1) NOT FOR REPLICATIONGo with the flow & have fun! Else fight the flow blog thingie: http://weblogs.sqlteam.com/mladenp |
 |
|
albear
Starting Member
2 Posts |
Posted - 2006-10-30 : 12:30:14
|
Yep i can see how that would work for a new database. But all the current databases are filled with data and the identity colum is used as the primary key, so if i drop it none of the data will match up, as the identity will be reset. |
 |
|
|
|
|
|
|