/*
   Friday, March 30, 20122:12:01 PM
   User: 
   Server: .
   Database: ATNDBTest
   Application: 
*/

/* To prevent any potential data loss issues, you should review this script in detail before running it outside the context of the database designer.*/
BEGIN TRANSACTION
SET QUOTED_IDENTIFIER ON
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
GO
ALTER TABLE dbo.SystemsOperationDefaultAccCode
	DROP CONSTRAINT FK_SystemsOperationDefaultAccCode_SystemsOperationDefault
GO
ALTER TABLE dbo.SystemsOperationDefault SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.SystemsOperationDefaultAccCode
	DROP CONSTRAINT FK_SystemsOperationDefaultAccCode_CreditAccGroupCodeBook
GO
ALTER TABLE dbo.SystemsOperationDefaultAccCode
	DROP CONSTRAINT FK_SystemsOperationDefaultAccCode_DebitAccGroupCodeBook
GO
ALTER TABLE dbo.AccGroupCodeBook SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.SystemsOperationDefaultAccCode
	DROP CONSTRAINT FK_SystemsOperationDefaultAccCode_CreditAccGeneralCodeBook
GO
ALTER TABLE dbo.SystemsOperationDefaultAccCode
	DROP CONSTRAINT FK_SystemsOperationDefaultAccCode_DebitAccGeneralCodeBook
GO
ALTER TABLE dbo.AccGeneralCodeBook SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.SystemsOperationDefaultAccCode
	DROP CONSTRAINT FK_SystemsOperationDefaultAccCode_CreditAccEntityL2CodeBook
GO
ALTER TABLE dbo.SystemsOperationDefaultAccCode
	DROP CONSTRAINT FK_SystemsOperationDefaultAccCode_DebitAccEntityL2CodeBook
GO
ALTER TABLE dbo.AccEntityL2CodeBook SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.SystemsOperationDefaultAccCode
	DROP CONSTRAINT FK_SystemsOperationDefaultAccCode_CreditAccEntityL1CodeBook
GO
ALTER TABLE dbo.SystemsOperationDefaultAccCode
	DROP CONSTRAINT FK_SystemsOperationDefaultAccCode_DebitAccEntityL1CodeBook
GO
ALTER TABLE dbo.AccEntityL1CodeBook SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.SystemsOperationDefaultAccCode
	DROP CONSTRAINT FK_SystemsOperationDefaultAccCode_CreditAccAdjutantL1CodeBook
GO
ALTER TABLE dbo.SystemsOperationDefaultAccCode
	DROP CONSTRAINT FK_SystemsOperationDefaultAccCode_DebitAccAdjutantL1CodeBook
GO
ALTER TABLE dbo.AccAdjutantL1CodeBook SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
CREATE TABLE dbo.Tmp_SystemsOperationDefaultAccCode
	(
	Serial bigint NOT NULL,
	OfficeDetailRef int NOT NULL,
	SystemsOperationDefaultRef int NOT NULL,
	DebitAccGroupCodeBookRef bigint NULL,
	DebitAccGeneralCodeBookRef bigint NULL,
	DebitAccAdjutantL1CodeBookRef bigint NULL,
	DebitAccEntityL1CodeBookRef bigint NULL,
	DebitAccEntityL2CodeBookRef bigint NULL,
	CreditAccGroupCodeBookRef bigint NULL,
	CreditAccGeneralCodeBookRef bigint NULL,
	CreditAccAdjutantL1CodeBookRef bigint NULL,
	CreditAccEntityL1CodeBookRef bigint NULL,
	CreditAccEntityL2CodeBookRef bigint NULL
	)  ON [PRIMARY]
GO
ALTER TABLE dbo.Tmp_SystemsOperationDefaultAccCode SET (LOCK_ESCALATION = TABLE)
GO
IF EXISTS(SELECT * FROM dbo.SystemsOperationDefaultAccCode)
	 EXEC('INSERT INTO dbo.Tmp_SystemsOperationDefaultAccCode (Serial, OfficeDetailRef, SystemsOperationDefaultRef, DebitAccGroupCodeBookRef, DebitAccGeneralCodeBookRef, DebitAccAdjutantL1CodeBookRef, DebitAccEntityL1CodeBookRef, DebitAccEntityL2CodeBookRef, CreditAccGroupCodeBookRef, CreditAccGeneralCodeBookRef, CreditAccAdjutantL1CodeBookRef, CreditAccEntityL1CodeBookRef, CreditAccEntityL2CodeBookRef)
		SELECT Serial, OfficeDetailRef, SystemsOperationDefaultRef, DebitAccGroupCodeBookRef, DebitAccGeneralCodeBookRef, DebitAccAdjutantL1CodeBookRef, DebitAccEntityL1CodeBookRef, DebitAccEntityL2CodeBookRef, CreditAccGroupCodeBookRef, CreditAccGeneralCodeBookRef, CreditAccAdjutantL1CodeBookRef, CreditAccEntityL1CodeBookRef, CreditAccEntityL2CodeBookRef FROM dbo.SystemsOperationDefaultAccCode WITH (HOLDLOCK TABLOCKX)')
GO
DROP TABLE dbo.SystemsOperationDefaultAccCode
GO
EXECUTE sp_rename N'dbo.Tmp_SystemsOperationDefaultAccCode', N'SystemsOperationDefaultAccCode', 'OBJECT' 
GO
ALTER TABLE dbo.SystemsOperationDefaultAccCode ADD CONSTRAINT
	PK_DefaultSystemsOperationAccCode PRIMARY KEY CLUSTERED 
	(
	Serial
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

GO
ALTER TABLE dbo.SystemsOperationDefaultAccCode ADD CONSTRAINT
	FK_SystemsOperationDefaultAccCode_CreditAccAdjutantL1CodeBook FOREIGN KEY
	(
	CreditAccAdjutantL1CodeBookRef
	) REFERENCES dbo.AccAdjutantL1CodeBook
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.SystemsOperationDefaultAccCode ADD CONSTRAINT
	FK_SystemsOperationDefaultAccCode_CreditAccEntityL1CodeBook FOREIGN KEY
	(
	CreditAccEntityL1CodeBookRef
	) REFERENCES dbo.AccEntityL1CodeBook
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.SystemsOperationDefaultAccCode ADD CONSTRAINT
	FK_SystemsOperationDefaultAccCode_CreditAccEntityL2CodeBook FOREIGN KEY
	(
	CreditAccEntityL2CodeBookRef
	) REFERENCES dbo.AccEntityL2CodeBook
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.SystemsOperationDefaultAccCode ADD CONSTRAINT
	FK_SystemsOperationDefaultAccCode_CreditAccGeneralCodeBook FOREIGN KEY
	(
	CreditAccGeneralCodeBookRef
	) REFERENCES dbo.AccGeneralCodeBook
	(
	serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.SystemsOperationDefaultAccCode ADD CONSTRAINT
	FK_SystemsOperationDefaultAccCode_CreditAccGroupCodeBook FOREIGN KEY
	(
	CreditAccGroupCodeBookRef
	) REFERENCES dbo.AccGroupCodeBook
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.SystemsOperationDefaultAccCode ADD CONSTRAINT
	FK_SystemsOperationDefaultAccCode_DebitAccAdjutantL1CodeBook FOREIGN KEY
	(
	DebitAccAdjutantL1CodeBookRef
	) REFERENCES dbo.AccAdjutantL1CodeBook
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.SystemsOperationDefaultAccCode ADD CONSTRAINT
	FK_SystemsOperationDefaultAccCode_DebitAccEntityL1CodeBook FOREIGN KEY
	(
	DebitAccEntityL1CodeBookRef
	) REFERENCES dbo.AccEntityL1CodeBook
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.SystemsOperationDefaultAccCode ADD CONSTRAINT
	FK_SystemsOperationDefaultAccCode_DebitAccEntityL2CodeBook FOREIGN KEY
	(
	DebitAccEntityL2CodeBookRef
	) REFERENCES dbo.AccEntityL2CodeBook
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.SystemsOperationDefaultAccCode ADD CONSTRAINT
	FK_SystemsOperationDefaultAccCode_DebitAccGeneralCodeBook FOREIGN KEY
	(
	DebitAccGeneralCodeBookRef
	) REFERENCES dbo.AccGeneralCodeBook
	(
	serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.SystemsOperationDefaultAccCode ADD CONSTRAINT
	FK_SystemsOperationDefaultAccCode_DebitAccGroupCodeBook FOREIGN KEY
	(
	DebitAccGroupCodeBookRef
	) REFERENCES dbo.AccGroupCodeBook
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.SystemsOperationDefaultAccCode ADD CONSTRAINT
	FK_SystemsOperationDefaultAccCode_SystemsOperationDefault FOREIGN KEY
	(
	SystemsOperationDefaultRef
	) REFERENCES dbo.SystemsOperationDefault
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
COMMIT
