/*
   Friday, March 30, 20121:31:31 PM
   User: 
   Server: .
   Database: ATNDBTest
   Application: 
*/

/* 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.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,
	Debit bigint NULL,
	Credit bigint NULL,
	DebitInForeignCurrency money NULL,
	CreditInForeignCurrency money NULL,
	AccGroupCodeBookRef bigint NOT NULL,
	AccGeneralCodeBookRef bigint NOT NULL,
	AccAdjutantL1CodeBookRef bigint 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, AccGroupCodeBookRef, AccGeneralCodeBookRef, AccAdjutantL1CodeBookRef, AccEntityL1CodeBookRef, AccEntityL2CodeBookRef)
		SELECT Serial, AccDocumentHeaderRef, Detail, Debit, Credit, 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_AccDtlDoc 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 WITH NOCHECK ADD CONSTRAINT
	FK_AccDocumentDetail_AccDocumentHeader FOREIGN KEY
	(
	AccDocumentHeaderRef
	) REFERENCES dbo.AccDocumentHeader
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
COMMIT
