Use ATNDBTest
Go

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.AccEntityL1CodeBook
	DROP CONSTRAINT FK_AccEntityL1CodeBook_Users
GO
ALTER TABLE dbo.Users SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.AccEntityL1CodeBook
	DROP CONSTRAINT FK_AccEntityL1CodeBook_AccEntityKind
GO
ALTER TABLE dbo.AccEntityKind SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
CREATE TABLE dbo.Tmp_AccEntityL1CodeBook
	(
	Serial bigint NOT NULL,
	Code bigint NULL,
	FADetail varchar(100) NULL,
	ENDetail varchar(100) NULL,
	AccEntityKindRef smallint NULL,
	Active bit NULL,
	RecorderRef int NULL,
	RecordDate char(10) NULL,
	EditorRef int NULL,
	EditDate char(10) NULL,
	OfficeDetailRef int NULL
	)  ON [PRIMARY]
GO
ALTER TABLE dbo.Tmp_AccEntityL1CodeBook SET (LOCK_ESCALATION = TABLE)
GO
IF EXISTS(SELECT * FROM dbo.AccEntityL1CodeBook)
	 EXEC('INSERT INTO dbo.Tmp_AccEntityL1CodeBook (Serial, Code, FADetail, AccEntityKindRef, Active, RecorderRef, RecordDate, EditorRef, EditDate, OfficeDetailRef)
		SELECT Serial, Code, Detail, AccEntityKindRef, Active, RecorderRef, RecordDate, EditorRef, EditDate, OfficeDetailRef FROM dbo.AccEntityL1CodeBook WITH (HOLDLOCK TABLOCKX)')
GO
ALTER TABLE dbo.Broker
	DROP CONSTRAINT FK_Broker_AccEntityL1CodeBook
GO
ALTER TABLE dbo.SystemAccCodeDefaultPerOffice
	DROP CONSTRAINT FK_SystemAccCodeDefaultPerOffice_CreditAccEntityL1CodeBook
GO
ALTER TABLE dbo.SystemAccCodeDefaultPerOffice
	DROP CONSTRAINT FK_SystemAccCodeDefaultPerOffice_DebitAccEntityL1CodeBook
GO
ALTER TABLE dbo.SystemAccCodeDefaultPerPart
	DROP CONSTRAINT FK_SystemAccCodeDefaultPerPart_CreditAccEntityL1CodeBook
GO
ALTER TABLE dbo.SystemAccCodeDefaultPerPart
	DROP CONSTRAINT FK_SystemAccCodeDefaultPerPart_DebitAccEntityL1CodeBook
GO
ALTER TABLE dbo.CashGetPay
	DROP CONSTRAINT FK_CashGetPay_CreditAccEntityL1CodeBook
GO
ALTER TABLE dbo.AccEntityL2CodeBook
	DROP CONSTRAINT FK_AccEntityL2CodeBook_AccEntityL1CodeBook
GO
ALTER TABLE dbo.CashGetPay
	DROP CONSTRAINT FK_CashGetPay_DebitAccEntityL1CodeBook
GO
ALTER TABLE dbo.AccDocumentDetail
	DROP CONSTRAINT FK_AccDocumentDetail_AccEntityL1CodeBook
GO
ALTER TABLE dbo.FactorAccCode
	DROP CONSTRAINT FK_FactorAccCode_AccEntityL1CodeBook_Credit
GO
ALTER TABLE dbo.FactorAccCode
	DROP CONSTRAINT FK_FactorAccCode_AccEntityL1CodeBook_Debit
GO
ALTER TABLE dbo.BankAccount
	DROP CONSTRAINT FK_BankAccount_AccEntityL1CodeBook
GO
DROP TABLE dbo.AccEntityL1CodeBook
GO
EXECUTE sp_rename N'dbo.Tmp_AccEntityL1CodeBook', N'AccEntityL1CodeBook', 'OBJECT' 
GO
ALTER TABLE dbo.AccEntityL1CodeBook ADD CONSTRAINT
	PK_AccEntityL1CodeBook 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.AccEntityL1CodeBook ADD CONSTRAINT
	FK_AccEntityL1CodeBook_AccEntityKind FOREIGN KEY
	(
	AccEntityKindRef
	) REFERENCES dbo.AccEntityKind
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.AccEntityL1CodeBook ADD CONSTRAINT
	FK_AccEntityL1CodeBook_Users FOREIGN KEY
	(
	RecorderRef
	) REFERENCES dbo.Users
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.BankAccount ADD CONSTRAINT
	FK_BankAccount_AccEntityL1CodeBook FOREIGN KEY
	(
	AccEntityL1CodeBookRef
	) REFERENCES dbo.AccEntityL1CodeBook
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.BankAccount SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.FactorAccCode ADD CONSTRAINT
	FK_FactorAccCode_AccEntityL1CodeBook_Credit FOREIGN KEY
	(
	CreditAccEntityL1CodeBookRef
	) REFERENCES dbo.AccEntityL1CodeBook
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.FactorAccCode ADD CONSTRAINT
	FK_FactorAccCode_AccEntityL1CodeBook_Debit FOREIGN KEY
	(
	DebitAccEntityL1CodeBookRef
	) REFERENCES dbo.AccEntityL1CodeBook
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.FactorAccCode SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.AccDocumentDetail ADD CONSTRAINT
	FK_AccDocumentDetail_AccEntityL1CodeBook FOREIGN KEY
	(
	AccEntityL1CodeBookRef
	) REFERENCES dbo.AccEntityL1CodeBook
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.AccDocumentDetail SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.AccEntityL2CodeBook ADD CONSTRAINT
	FK_AccEntityL2CodeBook_AccEntityL1CodeBook FOREIGN KEY
	(
	AccEntityL1CodeBookRef
	) REFERENCES dbo.AccEntityL1CodeBook
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.AccEntityL2CodeBook SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.CashGetPay ADD CONSTRAINT
	FK_CashGetPay_CreditAccEntityL1CodeBook FOREIGN KEY
	(
	CreditAccEntityL1CodeBookRef
	) REFERENCES dbo.AccEntityL1CodeBook
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.CashGetPay ADD CONSTRAINT
	FK_CashGetPay_DebitAccEntityL1CodeBook FOREIGN KEY
	(
	DebitAccEntityL1CodeBookRef
	) REFERENCES dbo.AccEntityL1CodeBook
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.CashGetPay SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.SystemAccCodeDefaultPerPart ADD CONSTRAINT
	FK_SystemAccCodeDefaultPerPart_CreditAccEntityL1CodeBook FOREIGN KEY
	(
	CreditAccEntityL1CodeBookRef
	) REFERENCES dbo.AccEntityL1CodeBook
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.SystemAccCodeDefaultPerPart ADD CONSTRAINT
	FK_SystemAccCodeDefaultPerPart_DebitAccEntityL1CodeBook FOREIGN KEY
	(
	DebitAccEntityL1CodeBookRef
	) REFERENCES dbo.AccEntityL1CodeBook
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.SystemAccCodeDefaultPerPart SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.SystemAccCodeDefaultPerOffice ADD CONSTRAINT
	FK_SystemAccCodeDefaultPerOffice_CreditAccEntityL1CodeBook FOREIGN KEY
	(
	CreditAccEntityL1CodeBookRef
	) REFERENCES dbo.AccEntityL1CodeBook
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.SystemAccCodeDefaultPerOffice ADD CONSTRAINT
	FK_SystemAccCodeDefaultPerOffice_DebitAccEntityL1CodeBook FOREIGN KEY
	(
	DebitAccEntityL1CodeBookRef
	) REFERENCES dbo.AccEntityL1CodeBook
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.SystemAccCodeDefaultPerOffice SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.Broker ADD CONSTRAINT
	FK_Broker_AccEntityL1CodeBook FOREIGN KEY
	(
	AccEntityL1CodeBookRef
	) REFERENCES dbo.AccEntityL1CodeBook
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.Broker SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
