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.AccDocumentDetail
	DROP CONSTRAINT FK_AccDocumentDetail_AccGroupCodeBook
GO
ALTER TABLE dbo.AccGroupCodeBook SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.AccDocumentDetail
	DROP CONSTRAINT FK_AccDocumentDetail_AccGeneralCodeBook
GO
ALTER TABLE dbo.AccGeneralCodeBook SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.AccDocumentDetail
	DROP CONSTRAINT FK_AccDocumentDetail_AccEntityL2CodeBook
GO
ALTER TABLE dbo.AccEntityL2CodeBook SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.AccDocumentDetail
	DROP CONSTRAINT FK_AccDocumentDetail_AccEntityL1CodeBook
GO
ALTER TABLE dbo.AccEntityL1CodeBook SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.AccDocumentDetail
	DROP CONSTRAINT FK_AccDocumentDetail_AccAdjutantL1CodeBook
GO
ALTER TABLE dbo.AccAdjutantL1CodeBook SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.AccDocumentDetail
	DROP CONSTRAINT FK_AccDocumentDetail_AccDocumentHeader
GO
ALTER TABLE dbo.AccDocumentHeader SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
CREATE TABLE dbo.Tmp_AccDocumentDetail
	(
	Serial bigint NOT NULL,
	AccDocumentHeaderRef bigint NULL,
	Detail varchar(200) NULL,
	ENDes varchar(200) NULL,
	Debit bigint NULL,
	Credit bigint NULL,
	DebitInForeignCurrency money NULL,
	CreditInForeignCurrency money NULL,
	AccGroupCodeBookRef bigint NOT NULL,
	AccGeneralCodeBookRef bigint NOT NULL,
	AccAdjutantL1CodeBookRef bigint NOT NULL,
	AccEntityL1CodeBookRef bigint NULL,
	AccEntityL2CodeBookRef bigint NULL
	)  ON [PRIMARY]
GO
ALTER TABLE dbo.Tmp_AccDocumentDetail SET (LOCK_ESCALATION = TABLE)
GO
IF EXISTS(SELECT * FROM dbo.AccDocumentDetail)
	 EXEC('INSERT INTO dbo.Tmp_AccDocumentDetail (Serial, AccDocumentHeaderRef, Detail, Debit, Credit, DebitInForeignCurrency, CreditInForeignCurrency, AccGroupCodeBookRef, AccGeneralCodeBookRef, AccAdjutantL1CodeBookRef, AccEntityL1CodeBookRef, AccEntityL2CodeBookRef)
		SELECT Serial, AccDocumentHeaderRef, Detail, Debit, Credit, DebitInForeignCurrency, CreditInForeignCurrency, AccGroupCodeBookRef, AccGeneralCodeBookRef, AccAdjutantL1CodeBookRef, AccEntityL1CodeBookRef, AccEntityL2CodeBookRef FROM dbo.AccDocumentDetail WITH (HOLDLOCK TABLOCKX)')
GO
DROP TABLE dbo.AccDocumentDetail
GO
EXECUTE sp_rename N'dbo.Tmp_AccDocumentDetail', N'AccDocumentDetail', 'OBJECT' 
GO
ALTER TABLE dbo.AccDocumentDetail ADD CONSTRAINT
	PK_AccDocumentDetail 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.AccDocumentDetail ADD CONSTRAINT
	FK_AccDocumentDetail_AccDocumentHeader FOREIGN KEY
	(
	AccDocumentHeaderRef
	) REFERENCES dbo.AccDocumentHeader
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.AccDocumentDetail ADD CONSTRAINT
	FK_AccDocumentDetail_AccAdjutantL1CodeBook FOREIGN KEY
	(
	AccAdjutantL1CodeBookRef
	) REFERENCES dbo.AccAdjutantL1CodeBook
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
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 ADD CONSTRAINT
	FK_AccDocumentDetail_AccEntityL2CodeBook FOREIGN KEY
	(
	AccEntityL2CodeBookRef
	) REFERENCES dbo.AccEntityL2CodeBook
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
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 ADD CONSTRAINT
	FK_AccDocumentDetail_AccGroupCodeBook FOREIGN KEY
	(
	AccGroupCodeBookRef
	) REFERENCES dbo.AccGroupCodeBook
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
COMMIT
