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.CostDetail
	DROP CONSTRAINT FK_CostDetail_CreditAccEntityL2CodeBook
GO
ALTER TABLE dbo.CostDetail
	DROP CONSTRAINT FK_CostDetail_DebitAccEntityL2CodeBook
GO
ALTER TABLE dbo.AccEntityL2CodeBook SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.CostDetail
	DROP CONSTRAINT FK_CostDetail_CreditAccEntityL1CodeBook
GO
ALTER TABLE dbo.CostDetail
	DROP CONSTRAINT FK_CostDetail_DebitAccEntityL1CodeBook
GO
ALTER TABLE dbo.AccEntityL1CodeBook SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.CostDetail
	DROP CONSTRAINT FK_CostDetail_CreditAccAdjutantL1CodeBook
GO
ALTER TABLE dbo.CostDetail
	DROP CONSTRAINT FK_CostDetail_DebitAccAdjutantL1CodeBook
GO
ALTER TABLE dbo.AccAdjutantL1CodeBook SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.CostDetail
	DROP CONSTRAINT FK_CostDetail_CreditAccGeneralCodeBook
GO
ALTER TABLE dbo.CostDetail
	DROP CONSTRAINT FK_CostDetail_DebitAccGeneralCodeBook
GO
ALTER TABLE dbo.AccGeneralCodeBook SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.CostDetail
	DROP CONSTRAINT FK_CostDetail_CreditAccGroupCodeBook
GO
ALTER TABLE dbo.CostDetail
	DROP CONSTRAINT FK_CostDetail_DebitAccGroupCodeBook
GO
ALTER TABLE dbo.AccGroupCodeBook SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.CostDetail
	DROP CONSTRAINT FK_CostDetail_CostPayKind
GO
ALTER TABLE dbo.CostPayKind SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.CostDetail
	DROP CONSTRAINT FK_CostDetail_CostHeader
GO
ALTER TABLE dbo.CostHeader SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
CREATE TABLE dbo.Tmp_CostDetail
	(
	Serial bigint NOT NULL,
	CostHeaderRef bigint NOT NULL,
	CostBaseRef bigint NULL,
	CostCenterRef bigint NULL,
	CostPayKindRef smallint NOT NULL,
	CostPayRef smallint NULL,
	Amount bigint NULL,
	AmountInForeignCurrency money NULL,
	ExtendedOption varchar(200) 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_CostDetail SET (LOCK_ESCALATION = TABLE)
GO
IF EXISTS(SELECT * FROM dbo.CostDetail)
	 EXEC('INSERT INTO dbo.Tmp_CostDetail (Serial, CostHeaderRef, CostPayKindRef, CostPayRef, Amount, AmountInForeignCurrency, ExtendedOption, DebitAccGroupCodeBookRef, DebitAccGeneralCodeBookRef, DebitAccAdjutantL1CodeBookRef, DebitAccEntityL1CodeBookRef, DebitAccEntityL2CodeBookRef, CreditAccGroupCodeBookRef, CreditAccGeneralCodeBookRef, CreditAccAdjutantL1CodeBookRef, CreditAccEntityL1CodeBookRef, CreditAccEntityL2CodeBookRef)
		SELECT Serial, CostHeaderRef, CostPayKindRef, CostPayRef, Amount, AmountInForeignCurrency, ExtendedOption, DebitAccGroupCodeBookRef, DebitAccGeneralCodeBookRef, DebitAccAdjutantL1CodeBookRef, DebitAccEntityL1CodeBookRef, DebitAccEntityL2CodeBookRef, CreditAccGroupCodeBookRef, CreditAccGeneralCodeBookRef, CreditAccAdjutantL1CodeBookRef, CreditAccEntityL1CodeBookRef, CreditAccEntityL2CodeBookRef FROM dbo.CostDetail WITH (HOLDLOCK TABLOCKX)')
GO
DROP TABLE dbo.CostDetail
GO
EXECUTE sp_rename N'dbo.Tmp_CostDetail', N'CostDetail', 'OBJECT' 
GO
ALTER TABLE dbo.CostDetail ADD CONSTRAINT
	PK_CostDetail 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.CostDetail ADD CONSTRAINT
	FK_CostDetail_CostHeader FOREIGN KEY
	(
	CostHeaderRef
	) REFERENCES dbo.CostHeader
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.CostDetail ADD CONSTRAINT
	FK_CostDetail_CostPayKind FOREIGN KEY
	(
	CostPayKindRef
	) REFERENCES dbo.CostPayKind
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.CostDetail ADD CONSTRAINT
	FK_CostDetail_CreditAccGroupCodeBook FOREIGN KEY
	(
	CreditAccGroupCodeBookRef
	) REFERENCES dbo.AccGroupCodeBook
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.CostDetail ADD CONSTRAINT
	FK_CostDetail_CreditAccGeneralCodeBook FOREIGN KEY
	(
	CreditAccGeneralCodeBookRef
	) REFERENCES dbo.AccGeneralCodeBook
	(
	serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.CostDetail ADD CONSTRAINT
	FK_CostDetail_CreditAccAdjutantL1CodeBook FOREIGN KEY
	(
	CreditAccAdjutantL1CodeBookRef
	) REFERENCES dbo.AccAdjutantL1CodeBook
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.CostDetail ADD CONSTRAINT
	FK_CostDetail_CreditAccEntityL1CodeBook FOREIGN KEY
	(
	CreditAccEntityL1CodeBookRef
	) REFERENCES dbo.AccEntityL1CodeBook
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.CostDetail ADD CONSTRAINT
	FK_CostDetail_CreditAccEntityL2CodeBook FOREIGN KEY
	(
	CreditAccEntityL2CodeBookRef
	) REFERENCES dbo.AccEntityL2CodeBook
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.CostDetail ADD CONSTRAINT
	FK_CostDetail_DebitAccGroupCodeBook FOREIGN KEY
	(
	DebitAccGroupCodeBookRef
	) REFERENCES dbo.AccGroupCodeBook
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.CostDetail ADD CONSTRAINT
	FK_CostDetail_DebitAccGeneralCodeBook FOREIGN KEY
	(
	DebitAccGeneralCodeBookRef
	) REFERENCES dbo.AccGeneralCodeBook
	(
	serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.CostDetail ADD CONSTRAINT
	FK_CostDetail_DebitAccAdjutantL1CodeBook FOREIGN KEY
	(
	DebitAccAdjutantL1CodeBookRef
	) REFERENCES dbo.AccAdjutantL1CodeBook
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.CostDetail ADD CONSTRAINT
	FK_CostDetail_DebitAccEntityL1CodeBook FOREIGN KEY
	(
	DebitAccEntityL1CodeBookRef
	) REFERENCES dbo.AccEntityL1CodeBook
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.CostDetail ADD CONSTRAINT
	FK_CostDetail_DebitAccEntityL2CodeBook FOREIGN KEY
	(
	DebitAccEntityL2CodeBookRef
	) REFERENCES dbo.AccEntityL2CodeBook
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
COMMIT

