Use ATNDBTest
Go

/* 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.AccGeneralCodeBook
	DROP CONSTRAINT FK_AccGeneralCodeBook_AccGroupCodeBook
GO
ALTER TABLE dbo.AccGroupCodeBook SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.AccGeneralCodeBook
	DROP CONSTRAINT FK_AccGeneralCodeBook_Users
GO
ALTER TABLE dbo.Users SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
CREATE TABLE dbo.Tmp_AccGeneralCodeBook
	(
	serial bigint NOT NULL,
	AccGroupCodeBookRef bigint NULL,
	Code bigint NULL,
	FADetail varchar(100) NULL,
	ENDetail varchar(100) 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_AccGeneralCodeBook SET (LOCK_ESCALATION = TABLE)
GO
IF EXISTS(SELECT * FROM dbo.AccGeneralCodeBook)
	 EXEC('INSERT INTO dbo.Tmp_AccGeneralCodeBook (serial, AccGroupCodeBookRef, Code, FADetail, RecorderRef, RecordDate, EditorRef, EditDate, OfficeDetailRef)
		SELECT serial, AccGroupCodeBookRef, Code, Detail, RecorderRef, RecordDate, EditorRef, EditDate, OfficeDetailRef FROM dbo.AccGeneralCodeBook WITH (HOLDLOCK TABLOCKX)')
GO
ALTER TABLE dbo.SystemAccCodeDefaultPerOffice
	DROP CONSTRAINT FK_SystemAccCodeDefaultPerOffice_CreditAccGeneralCodeBook
GO
ALTER TABLE dbo.SystemAccCodeDefaultPerOffice
	DROP CONSTRAINT FK_SystemAccCodeDefaultPerOffice_DebitAccGeneralCodeBook
GO
ALTER TABLE dbo.SystemAccCodeDefaultPerPart
	DROP CONSTRAINT FK_SystemAccCodeDefaultPerPart_CreditAccGeneralCodeBook
GO
ALTER TABLE dbo.SystemAccCodeDefaultPerPart
	DROP CONSTRAINT FK_SystemAccCodeDefaultPerPart_DebitAccGeneralCodeBook
GO
ALTER TABLE dbo.AccAdjutantL1CodeBook
	DROP CONSTRAINT FK_AccAdjutantL1CodeBook_AccGeneralCodeBook
GO
ALTER TABLE dbo.CashGetPay
	DROP CONSTRAINT FK_CashGetPay_CreditAccGeneralCodeBook
GO
ALTER TABLE dbo.CashGetPay
	DROP CONSTRAINT FK_CashGetPay_DebitAccGeneralCodeBook
GO
ALTER TABLE dbo.AccDocumentDetail
	DROP CONSTRAINT FK_AccDocumentDetail_AccGeneralCodeBook
GO
ALTER TABLE dbo.FactorAccCode
	DROP CONSTRAINT FK_FactorAccCode_AccGeneralCodeBook_Credit
GO
ALTER TABLE dbo.FactorAccCode
	DROP CONSTRAINT FK_FactorAccCode_AccGeneralCodeBook_Debit
GO
DROP TABLE dbo.AccGeneralCodeBook
GO
EXECUTE sp_rename N'dbo.Tmp_AccGeneralCodeBook', N'AccGeneralCodeBook', 'OBJECT' 
GO
ALTER TABLE dbo.AccGeneralCodeBook ADD CONSTRAINT
	PK_AccGeneralCodeBook 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.AccGeneralCodeBook ADD CONSTRAINT
	FK_AccGeneralCodeBook_Users FOREIGN KEY
	(
	RecorderRef
	) REFERENCES dbo.Users
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.AccGeneralCodeBook ADD CONSTRAINT
	FK_AccGeneralCodeBook_AccGroupCodeBook FOREIGN KEY
	(
	AccGroupCodeBookRef
	) REFERENCES dbo.AccGroupCodeBook
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.FactorAccCode ADD CONSTRAINT
	FK_FactorAccCode_AccGeneralCodeBook_Credit FOREIGN KEY
	(
	CreditAccGeneralCodeBookRef
	) REFERENCES dbo.AccGeneralCodeBook
	(
	serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.FactorAccCode ADD CONSTRAINT
	FK_FactorAccCode_AccGeneralCodeBook_Debit FOREIGN KEY
	(
	DebitAccGeneralCodeBookRef
	) REFERENCES dbo.AccGeneralCodeBook
	(
	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_AccGeneralCodeBook FOREIGN KEY
	(
	AccGeneralCodeBookRef
	) REFERENCES dbo.AccGeneralCodeBook
	(
	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.CashGetPay ADD CONSTRAINT
	FK_CashGetPay_CreditAccGeneralCodeBook FOREIGN KEY
	(
	CreditAccGeneralCodeBookRef
	) REFERENCES dbo.AccGeneralCodeBook
	(
	serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.CashGetPay ADD CONSTRAINT
	FK_CashGetPay_DebitAccGeneralCodeBook FOREIGN KEY
	(
	DebitAccGeneralCodeBookRef
	) REFERENCES dbo.AccGeneralCodeBook
	(
	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.AccAdjutantL1CodeBook ADD CONSTRAINT
	FK_AccAdjutantL1CodeBook_AccGeneralCodeBook FOREIGN KEY
	(
	AccGeneralCodeBookRef
	) REFERENCES dbo.AccGeneralCodeBook
	(
	serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.AccAdjutantL1CodeBook SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.SystemAccCodeDefaultPerPart ADD CONSTRAINT
	FK_SystemAccCodeDefaultPerPart_CreditAccGeneralCodeBook FOREIGN KEY
	(
	CreditAccGeneralCodeBookRef
	) REFERENCES dbo.AccGeneralCodeBook
	(
	serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.SystemAccCodeDefaultPerPart ADD CONSTRAINT
	FK_SystemAccCodeDefaultPerPart_DebitAccGeneralCodeBook FOREIGN KEY
	(
	DebitAccGeneralCodeBookRef
	) REFERENCES dbo.AccGeneralCodeBook
	(
	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_CreditAccGeneralCodeBook FOREIGN KEY
	(
	CreditAccGeneralCodeBookRef
	) REFERENCES dbo.AccGeneralCodeBook
	(
	serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.SystemAccCodeDefaultPerOffice ADD CONSTRAINT
	FK_SystemAccCodeDefaultPerOffice_DebitAccGeneralCodeBook FOREIGN KEY
	(
	DebitAccGeneralCodeBookRef
	) REFERENCES dbo.AccGeneralCodeBook
	(
	serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.SystemAccCodeDefaultPerOffice SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
