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
CREATE TABLE dbo.Tmp_Warehouse
	(
	Serial bigint NOT NULL,
	Code bigint NULL,
	Detail varchar(100) NULL,
	SelectedInReport bit NULL,
	RecorderRef int NULL,
	RecordDate char(10) NULL,
	EditorRef int NULL,
	EditDate char(10) NULL,
	OfficeDetailRef int NULL
	)  ON [PRIMARY]
GO
ALTER TABLE dbo.Tmp_Warehouse SET (LOCK_ESCALATION = TABLE)
GO
IF EXISTS(SELECT * FROM dbo.Warehouse)
	 EXEC('INSERT INTO dbo.Tmp_Warehouse (Serial, Code, Detail, RecorderRef, RecordDate, EditorRef, EditDate, OfficeDetailRef)
		SELECT Serial, Code, Detail, RecorderRef, RecordDate, EditorRef, EditDate, OfficeDetailRef FROM dbo.Warehouse WITH (HOLDLOCK TABLOCKX)')
GO
ALTER TABLE dbo.StorageCountHeader
	DROP CONSTRAINT FK_StorageCountHeader_Warehouse
GO
ALTER TABLE dbo.PartWarehouseBridge
	DROP CONSTRAINT FK_PartWarehouseBridge_Warehouse
GO
ALTER TABLE dbo.FactorHeader
	DROP CONSTRAINT FK_FactorHeader_Warehouse
GO
DROP TABLE dbo.Warehouse
GO
EXECUTE sp_rename N'dbo.Tmp_Warehouse', N'Warehouse', 'OBJECT' 
GO
ALTER TABLE dbo.Warehouse ADD CONSTRAINT
	PK_Warehouse PRIMARY KEY CLUSTERED 
	(
	Serial
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

GO
COMMIT
BEGIN TRANSACTION
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
ALTER TABLE dbo.FactorHeader SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.PartWarehouseBridge ADD CONSTRAINT
	FK_PartWarehouseBridge_Warehouse FOREIGN KEY
	(
	WarehouseRef
	) REFERENCES dbo.Warehouse
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.PartWarehouseBridge SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.StorageCountHeader ADD CONSTRAINT
	FK_StorageCountHeader_Warehouse FOREIGN KEY
	(
	WarehouseRef
	) REFERENCES dbo.Warehouse
	(
	Serial
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.StorageCountHeader SET (LOCK_ESCALATION = TABLE)
GO
COMMIT




-- تعیین مقدار پیش فرض برای انبارها
Update Warehouse Set SelectedInReport = 1

