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.FactorDetail
	DROP CONSTRAINT FK_FactorDetail_FactorHeader
GO
ALTER TABLE dbo.FactorHeader SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.FactorDetail
	DROP CONSTRAINT FK_FactorsDetail_Parts
GO
ALTER TABLE dbo.Part SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
CREATE TABLE dbo.Tmp_FactorDetail
	(
	Serial bigint NOT NULL,
	FactorHeaderRef bigint NOT NULL,
	PartRef bigint NOT NULL,
	Quantity float(53) NULL,
	UnitPrice bigint NULL,
	ApprovedUnitPrice bigint NULL,
	TotalPrice bigint NULL,
	BuyUnitPriceInPartTable bigint NULL,
	UnitPriceInForeignCurrency money NULL,
	ApprovedUnitPriceInForeignCurrency money NULL,
	TotalPriceInForeignCurrency money NULL,
	BuyUnitPriceInForeignCurrencyInPartTable money NULL,
	RebatePrice bigint NULL,
	RebatePercent float(53) NULL
	)  ON [PRIMARY]
GO
ALTER TABLE dbo.Tmp_FactorDetail SET (LOCK_ESCALATION = TABLE)
GO
IF EXISTS(SELECT * FROM dbo.FactorDetail)
	 EXEC('INSERT INTO dbo.Tmp_FactorDetail (Serial, FactorHeaderRef, PartRef, Quantity, UnitPrice, TotalPrice, BuyUnitPriceInPartTable, UnitPriceInForeignCurrency, TotalPriceInForeignCurrency, BuyUnitPriceInForeignCurrencyInPartTable, RebatePrice, RebatePercent)
		SELECT Serial, FactorHeaderRef, PartRef, Quantity, UnitPrice, TotalPrice, BuyUnitPriceInPartTable, UnitPriceInForeignCurrency, TotalPriceInForeignCurrency, BuyUnitPriceInForeignCurrencyInPartTable, RebatePrice, RebatePercent FROM dbo.FactorDetail WITH (HOLDLOCK TABLOCKX)')
GO
ALTER TABLE dbo.FactorAccCode
	DROP CONSTRAINT FK_FactorAccCode_FactorDetail
GO
DROP TABLE dbo.FactorDetail
GO
EXECUTE sp_rename N'dbo.Tmp_FactorDetail', N'FactorDetail', 'OBJECT' 
GO
ALTER TABLE dbo.FactorDetail ADD CONSTRAINT
	PK_FactorDetail 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_FactorDetail_FactorHeaderRef ON dbo.FactorDetail
	(
	FactorHeaderRef
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX IDX_FactorDetail_PartRef ON dbo.FactorDetail
	(
	PartRef
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE dbo.FactorDetail ADD CONSTRAINT
	FK_FactorsDetail_Parts FOREIGN KEY
	(
	PartRef
	) REFERENCES dbo.Part
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
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
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.FactorAccCode ADD CONSTRAINT
	FK_FactorAccCode_FactorDetail FOREIGN KEY
	(
	FactorDetailRef
	) REFERENCES dbo.FactorDetail
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.FactorAccCode SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
