Create Table ATNDBTest.dbo.Broker(
	Serial bigint Not Null,
	Code bigint Not Null,
	Name varchar(50) Null,
	Active bit Null,
	Manager varchar(50) Null,
	Address varchar(150) Null,
	PostCode varchar(20) Null,
	Tel1 varchar(20) Null,
	Tel2 varchar(20) Null,
	Fax varchar(20) Null,
	Mobile varchar(20) Null,
	SSN varchar(50) Null,
	AccEntityL1CodeBookRef bigint Null,
	AccEntityL2CodeBookRef bigint Null,
	SaleBrokerPercent decimal(6,3) Null,
	RecorderRef int Null,
	RecordDate char(10) Null,
	Description varchar(300) Null,
	OfficeDetailRef int Null)
GO


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.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
ALTER TABLE dbo.FactorHeader
	DROP CONSTRAINT FK_FactorHeader_AccDocumentHeader_OnVerification
GO
ALTER TABLE dbo.FactorHeader
	DROP CONSTRAINT FK_FactorHeader_AccDocumentHeader_OnUpdating
GO
ALTER TABLE dbo.AccDocumentHeader SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO

If Exists(Select * From sys.objects Where name = 'FactorHeaderAndDetailAbstractList') 
Begin
	DROP VIEW dbo.FactorHeaderAndDetailAbstractList
End

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,
	RebateRound bigint NULL,
	BrokerRef bigint NULL,
	SaleBrokerPercent decimal(6, 3) 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, AccDocumentHeaderRefOnUpdating, GetterOrPayerFactorRef, ControlNo, WarehouseRef, EffectiveDate, RecorderRef, RecordDate, EditorRef, EditDate, VerifierRef, VerificationDate, UpdatingUserRef, UpdatingDate, Cost1th, Cost2th, Rebate, RebateRound, TotalPaid, ForeignCurrencyFactor, PayDate, Mechanize, Description, OfficeDetailRef, OfficePeriodRef)
		SELECT Serial, Code, FactorKindRef, AccDocumentHeaderRefOnVerification, AccDocumentHeaderRefOnUpdating, GetterOrPayerFactorRef, ControlNo, WarehouseRef, EffectiveDate, RecorderRef, RecordDate, EditorRef, EditDate, VerifierRef, VerificationDate, UpdatingUserRef, UpdatingDate, Cost1th, Cost2th, Rebate, RebateRound, TotalPaid, ForeignCurrencyFactor, PayDate, Mechanize, Description, OfficeDetailRef, OfficePeriodRef FROM dbo.FactorHeader WITH (HOLDLOCK TABLOCKX)')
GO
ALTER TABLE dbo.FactorCashSettlementCheckBridge
	DROP CONSTRAINT FK_FactorCashSettlementCheckBridge_FactorHeader
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_AccDocumentHeader_OnVerification FOREIGN KEY
	(
	AccDocumentHeaderRefOnVerification
	) REFERENCES dbo.AccDocumentHeader
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.FactorHeader ADD CONSTRAINT
	FK_FactorHeader_AccDocumentHeader_OnUpdating FOREIGN KEY
	(
	AccDocumentHeaderRefOnUpdating
	) REFERENCES dbo.AccDocumentHeader
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
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
BEGIN TRANSACTION
GO
ALTER TABLE dbo.FactorCashSettlementCheckBridge ADD CONSTRAINT
	FK_FactorCashSettlementCheckBridge_FactorHeader FOREIGN KEY
	(
	FactorHeaderRef
	) REFERENCES dbo.FactorHeader
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.FactorCashSettlementCheckBridge SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
