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.GritProduct
	DROP CONSTRAINT FK_GritProduct_OfficeDetail
GO
ALTER TABLE dbo.OfficeDetail SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.GritProduct
	DROP CONSTRAINT FK_GritProduct_Recorder
GO
ALTER TABLE dbo.GritProduct
	DROP CONSTRAINT FK_GritProduct_Editor
GO
ALTER TABLE dbo.Users SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.GritProduct
	DROP CONSTRAINT FK_GritProduct_Unit
GO
ALTER TABLE dbo.Units SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.GritProduct
	DROP CONSTRAINT FK_GritProduct_GritSalePriceScope
GO
ALTER TABLE dbo.GritSalePriceScope SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
CREATE TABLE dbo.Tmp_GritProduct
	(
	Serial bigint NOT NULL,
	Code bigint NOT NULL,
	FAName varchar(50) NULL,
	ENName varchar(50) NULL,
	UnitRef bigint NULL,
	Active bit NULL,
	BuyUnitPrice bigint NULL,
	GritSalePriceScopeRef smallint NULL,
	Location varchar(15) NULL,
	RecorderRef int NULL,
	RecordDate char(10) NULL,
	EditorRef int NULL,
	EditDate char(10) NULL,
	Comment varchar(300) NULL,
	OfficeDetailRef int NULL
	)  ON [PRIMARY]
GO
ALTER TABLE dbo.Tmp_GritProduct SET (LOCK_ESCALATION = TABLE)
GO
IF EXISTS(SELECT * FROM dbo.GritProduct)
	 EXEC('INSERT INTO dbo.Tmp_GritProduct (Serial, Code, FAName, ENName, UnitRef, Active, BuyUnitPrice, GritSalePriceScopeRef, RecorderRef, RecordDate, EditorRef, EditDate, Comment, OfficeDetailRef)
		SELECT Serial, Code, FAName, ENName, UnitRef, Active, BuyUnitPrice, GritSalePriceScopeRef, RecorderRef, RecordDate, EditorRef, EditDate, Comment, OfficeDetailRef FROM dbo.GritProduct WITH (HOLDLOCK TABLOCKX)')
GO
ALTER TABLE dbo.GritSalePrice
	DROP CONSTRAINT FK_GritSalePrice_GritProduct
GO
ALTER TABLE dbo.GritFactor
	DROP CONSTRAINT FK_GritFactor_GritProduct
GO
DROP TABLE dbo.GritProduct
GO
EXECUTE sp_rename N'dbo.Tmp_GritProduct', N'GritProduct', 'OBJECT' 
GO
ALTER TABLE dbo.GritProduct ADD CONSTRAINT
	PK_GritProduct 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_GritProduct_OfficeDetailRef ON dbo.GritProduct
	(
	OfficeDetailRef
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE dbo.GritProduct ADD CONSTRAINT
	FK_GritProduct_GritSalePriceScope FOREIGN KEY
	(
	GritSalePriceScopeRef
	) REFERENCES dbo.GritSalePriceScope
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.GritProduct ADD CONSTRAINT
	FK_GritProduct_Unit FOREIGN KEY
	(
	UnitRef
	) REFERENCES dbo.Units
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.GritProduct ADD CONSTRAINT
	FK_GritProduct_Recorder FOREIGN KEY
	(
	RecorderRef
	) REFERENCES dbo.Users
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.GritProduct ADD CONSTRAINT
	FK_GritProduct_Editor FOREIGN KEY
	(
	EditorRef
	) REFERENCES dbo.Users
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.GritProduct ADD CONSTRAINT
	FK_GritProduct_OfficeDetail FOREIGN KEY
	(
	OfficeDetailRef
	) REFERENCES dbo.OfficeDetail
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.GritFactor ADD CONSTRAINT
	FK_GritFactor_GritProduct FOREIGN KEY
	(
	GritProductRef
	) REFERENCES dbo.GritProduct
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.GritFactor SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.GritSalePrice ADD CONSTRAINT
	FK_GritSalePrice_GritProduct FOREIGN KEY
	(
	GritProductRef
	) REFERENCES dbo.GritProduct
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.GritSalePrice SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
