/*
   دوشنبه, 9 اسفند 140007:50:25 ب.ظ
   User: 
   Server: TAHERIAN
   Database: ATNDB
   Application: 
*/

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.GritFactor
	DROP CONSTRAINT FK_GritFactor_OfficePeriod
GO
ALTER TABLE dbo.OfficePeriod SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.GritFactor
	DROP CONSTRAINT FK_GritFactor_OfficeDetail
GO
ALTER TABLE dbo.OfficeDetail SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.GritFactor
	DROP CONSTRAINT FK_GritFactor_Recorder
GO
ALTER TABLE dbo.GritFactor
	DROP CONSTRAINT FK_GritFactor_Editor
GO
ALTER TABLE dbo.GritFactor
	DROP CONSTRAINT FK_GritFactor_Verifier
GO
ALTER TABLE dbo.Users SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.GritFactor
	DROP CONSTRAINT FK_GritFactor_GritProduct
GO
ALTER TABLE dbo.GritProduct SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.GritFactor
	DROP CONSTRAINT FK_GritFactor_GritFactorKind
GO
ALTER TABLE dbo.GritFactorKind SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.GritFactor
	DROP CONSTRAINT FK_GritFactor_GritCashAndCredit
GO
ALTER TABLE dbo.GritCashAndCredit SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.GritFactor
	DROP CONSTRAINT FK_GritFactor_GetterOrPayerFactor
GO
ALTER TABLE dbo.GetterOrPayerFactor SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.GritFactor
	DROP CONSTRAINT FK_GritFactor_AccDocumentHeader_OnVerification
GO
ALTER TABLE dbo.AccDocumentHeader SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
CREATE TABLE dbo.Tmp_GritFactor
	(
	Serial bigint NOT NULL,
	Code bigint NULL,
	AccDocumentHeaderRefOnVerification bigint NULL,
	AccDocumentHeaderRefOnUpdating bigint NULL,
	GetterOrPayerFactorRef bigint NULL,
	GritFactorKindRef smallint NOT NULL,
	FactorHeaderRef bigint NULL,
	ControlNo varchar(20) NULL,
	EffectiveDate char(10) NULL,
	EffectiveTime char(8) NULL,
	GritCashAndCreditRef smallint NULL,
	LicensePlate varchar(15) NULL,
	EmptyWeight float(53) NULL,
	GrossWeight float(53) NULL,
	GritProductRef bigint NULL,
	Quantity float(53) NULL,
	UnitPrice bigint NULL,
	ApprovedUnitPrice bigint NULL,
	TotalPrice bigint NULL,
	BuyUnitPriceInProductTable bigint NULL,
	VAT bigint NULL,
	Cost bigint NULL,
	Rebate bigint NULL,
	TotalPaid bigint NULL,
	PayDate 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,
	Description varchar(300) NULL,
	OfficeDetailRef int NULL,
	OfficePeriodRef int NULL,
	RowVersion datetime NULL,
	Tag varchar(50) NULL
	)  ON [PRIMARY]
GO
ALTER TABLE dbo.Tmp_GritFactor SET (LOCK_ESCALATION = TABLE)
GO
IF EXISTS(SELECT * FROM dbo.GritFactor)
	 EXEC('INSERT INTO dbo.Tmp_GritFactor (Serial, Code, AccDocumentHeaderRefOnVerification, AccDocumentHeaderRefOnUpdating, GetterOrPayerFactorRef, GritFactorKindRef, ControlNo, EffectiveDate, EffectiveTime, GritCashAndCreditRef, LicensePlate, EmptyWeight, GrossWeight, GritProductRef, Quantity, UnitPrice, ApprovedUnitPrice, TotalPrice, BuyUnitPriceInProductTable, VAT, Cost, Rebate, TotalPaid, PayDate, RecorderRef, RecordDate, EditorRef, EditDate, VerifierRef, VerificationDate, UpdatingUserRef, UpdatingDate, Description, OfficeDetailRef, OfficePeriodRef, RowVersion, Tag)
		SELECT Serial, Code, AccDocumentHeaderRefOnVerification, AccDocumentHeaderRefOnUpdating, GetterOrPayerFactorRef, GritFactorKindRef, ControlNo, EffectiveDate, EffectiveTime, GritCashAndCreditRef, LicensePlate, EmptyWeight, GrossWeight, GritProductRef, Quantity, UnitPrice, ApprovedUnitPrice, TotalPrice, BuyUnitPriceInProductTable, VAT, Cost, Rebate, TotalPaid, PayDate, RecorderRef, RecordDate, EditorRef, EditDate, VerifierRef, VerificationDate, UpdatingUserRef, UpdatingDate, Description, OfficeDetailRef, OfficePeriodRef, RowVersion, Tag FROM dbo.GritFactor WITH (HOLDLOCK TABLOCKX)')
GO
ALTER TABLE dbo.GritFactorAccCode
	DROP CONSTRAINT FK_GritFactorAccCode_GritFactor
GO
ALTER TABLE dbo.FactorCashSettlementCheckBridge
	DROP CONSTRAINT FK_FactorCashSettlementCheckBridge_GritFactor
GO
DROP TABLE dbo.GritFactor
GO
EXECUTE sp_rename N'dbo.Tmp_GritFactor', N'GritFactor', 'OBJECT' 
GO
ALTER TABLE dbo.GritFactor ADD CONSTRAINT
	PK_GritFactor 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_GritFactor_GetterOrPayerFactorRef ON dbo.GritFactor
	(
	GetterOrPayerFactorRef
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX IDX_GritFactor_EffectiveDate ON dbo.GritFactor
	(
	EffectiveDate
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX IDX_GritFactor_GritProductRef ON dbo.GritFactor
	(
	GritProductRef
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX IDX_GritFactor_GritFactorKindRef ON dbo.GritFactor
	(
	GritFactorKindRef
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX IDX_GritFactor_GritCashAndCreditRef ON dbo.GritFactor
	(
	GritCashAndCreditRef
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX IDX_GritFactor_RecorderRef ON dbo.GritFactor
	(
	RecorderRef
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX IDX_GritFactor_EditorRef ON dbo.GritFactor
	(
	EditorRef
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX IDX_GritFactor_VerifierRef ON dbo.GritFactor
	(
	VerifierRef
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX IDX_GritFactor_UpdatingUserRef ON dbo.GritFactor
	(
	UpdatingUserRef
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE dbo.GritFactor ADD CONSTRAINT
	FK_GritFactor_AccDocumentHeader_OnVerification FOREIGN KEY
	(
	AccDocumentHeaderRefOnVerification
	) REFERENCES dbo.AccDocumentHeader
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.GritFactor ADD CONSTRAINT
	FK_GritFactor_GetterOrPayerFactor FOREIGN KEY
	(
	GetterOrPayerFactorRef
	) REFERENCES dbo.GetterOrPayerFactor
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.GritFactor ADD CONSTRAINT
	FK_GritFactor_GritCashAndCredit FOREIGN KEY
	(
	GritCashAndCreditRef
	) REFERENCES dbo.GritCashAndCredit
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.GritFactor ADD CONSTRAINT
	FK_GritFactor_GritFactorKind FOREIGN KEY
	(
	GritFactorKindRef
	) REFERENCES dbo.GritFactorKind
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
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 ADD CONSTRAINT
	FK_GritFactor_Recorder FOREIGN KEY
	(
	RecorderRef
	) REFERENCES dbo.Users
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.GritFactor ADD CONSTRAINT
	FK_GritFactor_Editor FOREIGN KEY
	(
	EditorRef
	) REFERENCES dbo.Users
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.GritFactor ADD CONSTRAINT
	FK_GritFactor_Verifier FOREIGN KEY
	(
	VerifierRef
	) REFERENCES dbo.Users
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.GritFactor ADD CONSTRAINT
	FK_GritFactor_OfficeDetail FOREIGN KEY
	(
	OfficeDetailRef
	) REFERENCES dbo.OfficeDetail
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.GritFactor ADD CONSTRAINT
	FK_GritFactor_OfficePeriod FOREIGN KEY
	(
	OfficePeriodRef
	) REFERENCES dbo.OfficePeriod
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.FactorCashSettlementCheckBridge ADD CONSTRAINT
	FK_FactorCashSettlementCheckBridge_GritFactor FOREIGN KEY
	(
	GritFactorRef
	) REFERENCES dbo.GritFactor
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.FactorCashSettlementCheckBridge SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.GritFactorAccCode ADD CONSTRAINT
	FK_GritFactorAccCode_GritFactor FOREIGN KEY
	(
	GritFactorRef
	) REFERENCES dbo.GritFactor
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.GritFactorAccCode SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
