/*
   Sunday, March 10, 20137:58:23 PM
   User: 
   Server: .
   Database: ATNDBTest
   Application: 
*/

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.FactorHeader
	DROP CONSTRAINT FK_FactorHeader_Warehouse
GO
ALTER TABLE dbo.Warehouse SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.FactorHeader
	DROP CONSTRAINT FK_FactorHeader_GetterOrPayerFactor
GO
ALTER TABLE dbo.GetterOrPayerFactor SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.FactorHeader
	DROP CONSTRAINT FK_FactorHeader_FactorKind
GO
ALTER TABLE dbo.FactorKind SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
If Exists(Select * From sys.Views Where name = 'FactorHeaderAndDetailAbstractList') Drop View dbo.FactorHeaderAndDetailAbstractList
GO
CREATE TABLE dbo.Tmp_FactorHeader
	(
	Serial bigint NOT NULL,
	Code bigint NULL,
	FactorKindRef int NOT NULL,
	AccDocumentHeaderRefOnVerification bigint NULL,
	AccDocumentHeaderRefOnUpdating bigint NULL,
	GetterOrPayerFactorRef bigint NULL,
	ControlNo varchar(20) NULL,
	WarehouseRef bigint NULL,
	EffectiveDate char(10) NULL,
	RecorderRef int NULL,
	RecordDate char(10) NULL,
	EditorRef int NULL,
	EditDate char(10) NULL,
	VerifierRef int NULL,
	VerificationDate char(10) NULL,
	UpdatingUserRef int NULL,
	UpdatingDate char(10) NULL,
	Cost1th bigint NULL,
	Cost2th bigint NULL,
	Rebate bigint NULL,
	TotalPaid bigint NULL,
	ForeignCurrencyFactor float(53) NULL,
	PayDate char(10) NULL,
	Mechanize bit NULL,
	Description varchar(300) NULL,
	OfficeDetailRef int NULL,
	OfficePeriodRef int NULL
	)  ON [PRIMARY]
GO
ALTER TABLE dbo.Tmp_FactorHeader SET (LOCK_ESCALATION = TABLE)
GO
IF EXISTS(SELECT * FROM dbo.FactorHeader)
	 EXEC('INSERT INTO dbo.Tmp_FactorHeader (Serial, Code, FactorKindRef, AccDocumentHeaderRefOnVerification, GetterOrPayerFactorRef, ControlNo, WarehouseRef, EffectiveDate, RecorderRef, RecordDate, EditorRef, EditDate, VerifierRef, VerificationDate, UpdatingUserRef, UpdatingDate, Cost1th, Cost2th, Rebate, TotalPaid, ForeignCurrencyFactor, PayDate, Mechanize, Description, OfficeDetailRef, OfficePeriodRef)
		SELECT Serial, Code, FactorKindRef, AccDocumentHeaderRef, GetterOrPayerFactorRef, ControlNo, WarehouseRef, EffectiveDate, RecorderRef, RecordDate, EditorRef, EditDate, VerifierRef, VerificationDate, UpdatingUserRef, UpdatingDate, Cost1th, Cost2th, Rebate, TotalPaid, ForeignCurrencyFactor, PayDate, Mechanize, Description, OfficeDetailRef, OfficePeriodRef FROM dbo.FactorHeader WITH (HOLDLOCK TABLOCKX)')
GO
ALTER TABLE dbo.FactorDetail
	DROP CONSTRAINT FK_FactorDetail_FactorHeader
GO
ALTER TABLE dbo.FactorAccCode
	DROP CONSTRAINT FK_FactorAccCode_FactorHeader
GO
DROP TABLE dbo.FactorHeader
GO
EXECUTE sp_rename N'dbo.Tmp_FactorHeader', N'FactorHeader', 'OBJECT' 
GO
ALTER TABLE dbo.FactorHeader ADD CONSTRAINT
	PK_FactorHeader PRIMARY KEY CLUSTERED 
	(
	Serial
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

GO
CREATE NONCLUSTERED INDEX IDX_FactorHeader_GetterOrPayerFactorRef ON dbo.FactorHeader
	(
	GetterOrPayerFactorRef
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX IDX_FactorHeader_EffectiveDate ON dbo.FactorHeader
	(
	EffectiveDate
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE dbo.FactorHeader ADD CONSTRAINT
	FK_FactorHeader_FactorKind FOREIGN KEY
	(
	FactorKindRef
	) REFERENCES dbo.FactorKind
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.FactorHeader ADD CONSTRAINT
	FK_FactorHeader_GetterOrPayerFactor FOREIGN KEY
	(
	GetterOrPayerFactorRef
	) REFERENCES dbo.GetterOrPayerFactor
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.FactorHeader ADD CONSTRAINT
	FK_FactorHeader_Warehouse FOREIGN KEY
	(
	WarehouseRef
	) REFERENCES dbo.Warehouse
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.FactorAccCode ADD CONSTRAINT
	FK_FactorAccCode_FactorHeader FOREIGN KEY
	(
	FactorHeaderRef
	) REFERENCES dbo.FactorHeader
	(
	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.FactorDetail ADD CONSTRAINT
	FK_FactorDetail_FactorHeader FOREIGN KEY
	(
	FactorHeaderRef
	) REFERENCES dbo.FactorHeader
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.FactorDetail SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
