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.AccGroupCodeBook
	DROP CONSTRAINT FK_AccGroupCodeBook_Users
GO
ALTER TABLE dbo.Users SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.AccGroupCodeBook
	DROP CONSTRAINT FK_AccGroupCodeBook_AccGroupKind
GO
ALTER TABLE dbo.AccGroupKind SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
CREATE TABLE dbo.Tmp_AccGroupCodeBook
	(
	Serial bigint NOT NULL,
	Code bigint NULL,
	FADetail varchar(100) NULL,
	ENDetail varchar(100) NULL,
	AccGroupKindRef smallint 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_AccGroupCodeBook SET (LOCK_ESCALATION = TABLE)
GO
IF EXISTS(SELECT * FROM dbo.AccGroupCodeBook)
	 EXEC('INSERT INTO dbo.Tmp_AccGroupCodeBook (Serial, Code, FADetail, AccGroupKindRef, RecorderRef, RecordDate, EditorRef, EditDate, OfficeDetailRef)
		SELECT Serial, Code, Detail, AccGroupKindRef, RecorderRef, RecordDate, EditorRef, EditDate, OfficeDetailRef FROM dbo.AccGroupCodeBook WITH (HOLDLOCK TABLOCKX)')
GO
ALTER TABLE dbo.SystemAccCodeDefaultPerOffice
	DROP CONSTRAINT FK_SystemAccCodeDefaultPerOffice_CreditAccGroupCodeBook
GO
ALTER TABLE dbo.SystemAccCodeDefaultPerOffice
	DROP CONSTRAINT FK_SystemAccCodeDefaultPerOffice_DebitAccGroupCodeBook
GO
ALTER TABLE dbo.SystemAccCodeDefaultPerPart
	DROP CONSTRAINT FK_SystemAccCodeDefaultPerPart_CreditAccGroupCodeBook
GO
ALTER TABLE dbo.AccGeneralCodeBook
	DROP CONSTRAINT FK_AccGeneralCodeBook_AccGroupCodeBook
GO
ALTER TABLE dbo.SystemAccCodeDefaultPerPart
	DROP CONSTRAINT FK_SystemAccCodeDefaultPerPart_DebitAccGroupCodeBook
GO
ALTER TABLE dbo.CashGetPay
	DROP CONSTRAINT FK_CashGetPay_CreditAccGroupCodeBook
GO
ALTER TABLE dbo.CashGetPay
	DROP CONSTRAINT FK_CashGetPay_DebitAccGroupCodeBook
GO
ALTER TABLE dbo.AccDocumentDetail
	DROP CONSTRAINT FK_AccDocumentDetail_AccGroupCodeBook
GO
ALTER TABLE dbo.FactorAccCode
	DROP CONSTRAINT FK_FactorAccCode_AccGroupCodeBook_Credit
GO
ALTER TABLE dbo.FactorAccCode
	DROP CONSTRAINT FK_FactorAccCode_AccGroupCodeBook_Debit
GO
DROP TABLE dbo.AccGroupCodeBook
GO
EXECUTE sp_rename N'dbo.Tmp_AccGroupCodeBook', N'AccGroupCodeBook', 'OBJECT' 
GO
ALTER TABLE dbo.AccGroupCodeBook ADD CONSTRAINT
	PK_AccGroupCodeBook 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.AccGroupCodeBook ADD CONSTRAINT
	FK_AccGroupCodeBook_AccGroupKind FOREIGN KEY
	(
	AccGroupKindRef
	) REFERENCES dbo.AccGroupKind
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.AccGroupCodeBook ADD CONSTRAINT
	FK_AccGroupCodeBook_Users FOREIGN KEY
	(
	RecorderRef
	) REFERENCES dbo.Users
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.FactorAccCode ADD CONSTRAINT
	FK_FactorAccCode_AccGroupCodeBook_Credit FOREIGN KEY
	(
	CreditAccGroupCodeBookRef
	) REFERENCES dbo.AccGroupCodeBook
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.FactorAccCode ADD CONSTRAINT
	FK_FactorAccCode_AccGroupCodeBook_Debit FOREIGN KEY
	(
	DebitAccGroupCodeBookRef
	) REFERENCES dbo.AccGroupCodeBook
	(
	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_AccGroupCodeBook FOREIGN KEY
	(
	AccGroupCodeBookRef
	) REFERENCES dbo.AccGroupCodeBook
	(
	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_CreditAccGroupCodeBook FOREIGN KEY
	(
	CreditAccGroupCodeBookRef
	) REFERENCES dbo.AccGroupCodeBook
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.CashGetPay ADD CONSTRAINT
	FK_CashGetPay_DebitAccGroupCodeBook FOREIGN KEY
	(
	DebitAccGroupCodeBookRef
	) REFERENCES dbo.AccGroupCodeBook
	(
	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.AccGeneralCodeBook ADD CONSTRAINT
	FK_AccGeneralCodeBook_AccGroupCodeBook FOREIGN KEY
	(
	AccGroupCodeBookRef
	) REFERENCES dbo.AccGroupCodeBook
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.AccGeneralCodeBook SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.SystemAccCodeDefaultPerPart ADD CONSTRAINT
	FK_SystemAccCodeDefaultPerPart_CreditAccGroupCodeBook FOREIGN KEY
	(
	CreditAccGroupCodeBookRef
	) REFERENCES dbo.AccGroupCodeBook
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.SystemAccCodeDefaultPerPart ADD CONSTRAINT
	FK_SystemAccCodeDefaultPerPart_DebitAccGroupCodeBook FOREIGN KEY
	(
	DebitAccGroupCodeBookRef
	) REFERENCES dbo.AccGroupCodeBook
	(
	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_CreditAccGroupCodeBook FOREIGN KEY
	(
	CreditAccGroupCodeBookRef
	) REFERENCES dbo.AccGroupCodeBook
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.SystemAccCodeDefaultPerOffice ADD CONSTRAINT
	FK_SystemAccCodeDefaultPerOffice_DebitAccGroupCodeBook FOREIGN KEY
	(
	DebitAccGroupCodeBookRef
	) REFERENCES dbo.AccGroupCodeBook
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.SystemAccCodeDefaultPerOffice SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
