
/* To prevent any potential data loss issues, you should review this script in detail before running it outside the context of the database designer.*/
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.AccWarehouseInfo
	DROP CONSTRAINT FK_AccWarehouseInfo_AccDetail
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.AccWarehouseInfo
	DROP CONSTRAINT FK_AccWarehouseInfo_AccControl
GO
COMMIT
GO
ALTER TABLE dbo.AccWarehouseInfo
	DROP CONSTRAINT DF_AccWarehouseInfo_Type
GO
ALTER TABLE dbo.AccWarehouseInfo
	DROP CONSTRAINT DF_AccWarehouseInfo_UpdateDate
GO
ALTER TABLE dbo.AccWarehouseInfo
	DROP CONSTRAINT DF_AccWarehouseInfo_Updater
GO
CREATE TABLE dbo.Tmp_AccWarehouseInfo
	(
	ID bigint NOT NULL IDENTITY (1, 1),
	Type int NOT NULL,
	AccID bigint NOT NULL,
	Price decimal(28, 5) NULL,
	Unit bigint NULL,
	Description nvarchar(3000) NULL,
	MinimumIn decimal(28, 5) NULL,
	UpdateDate datetime NOT NULL,
	Updater bigint NULL,
	OrderPoint decimal(28, 5) NULL,
	TotalID bigint NULL,
	ControlID bigint NULL,
	DetailID1 bigint NULL,
	DetailID2 bigint NULL,
	PriceSell decimal(28, 5) NULL,
	IranCode nchar(20) NULL,
	BarCode nvarchar(500) NULL,
	StandardCode nchar(20) NULL,
	Avarez float(53) NULL,
	WareAddress nvarchar(500) NULL,
	Oft int NULL,
	RotubatPayeh int NULL,
	RotubatFasli int NULL,
	StandardFormula bit NULL,
	UseLocallRotubatSetting bit NULL,
	Name nvarchar(50) NULL,
	Code char(20) NULL,
	Base bigint NULL,
	Pakaging bigint NULL,
	ProdType bigint NULL,
	Phisics bigint NULL,
	UnitSecondary bigint NULL
	)  ON [PRIMARY]
GO
ALTER TABLE dbo.Tmp_AccWarehouseInfo ADD CONSTRAINT
	DF_AccWarehouseInfo_Type DEFAULT ((0)) FOR Type
GO
ALTER TABLE dbo.Tmp_AccWarehouseInfo ADD CONSTRAINT
	DF_AccWarehouseInfo_UpdateDate DEFAULT (getdate()) FOR UpdateDate
GO
ALTER TABLE dbo.Tmp_AccWarehouseInfo ADD CONSTRAINT
	DF_AccWarehouseInfo_Updater DEFAULT ((0)) FOR Updater
GO
SET IDENTITY_INSERT dbo.Tmp_AccWarehouseInfo ON
GO
IF EXISTS(SELECT * FROM dbo.AccWarehouseInfo)
	 EXEC('INSERT INTO dbo.Tmp_AccWarehouseInfo (ID, Type, AccID, Price, Unit, Description, MinimumIn, UpdateDate, Updater, OrderPoint, TotalID, ControlID, DetailID1, DetailID2, PriceSell, IranCode, BarCode, StandardCode, Avarez, WareAddress, Oft, RotubatPayeh, RotubatFasli, StandardFormula, UseLocallRotubatSetting, Name, Code, Base, Pakaging, ProdType, Phisics, UnitSecondary)
		SELECT ID, Type, AccID, Price, Unit, Description, MinimumIn, UpdateDate, Updater, OrderPoint, TotalID, ControlID, DetailID1, DetailID2, PriceSell, IranCode, CONVERT(nvarchar(500), BarCode), StandardCode, Avarez, WareAddress, Oft, RotubatPayeh, RotubatFasli, StandardFormula, UseLocallRotubatSetting, Name, Code, Base, Pakaging, ProdType, Phisics, UnitSecondary FROM dbo.AccWarehouseInfo WITH (HOLDLOCK TABLOCKX)')
GO
SET IDENTITY_INSERT dbo.Tmp_AccWarehouseInfo OFF
GO
DROP TABLE dbo.AccWarehouseInfo
GO
EXECUTE sp_rename N'dbo.Tmp_AccWarehouseInfo', N'AccWarehouseInfo', 'OBJECT' 
GO
ALTER TABLE dbo.AccWarehouseInfo ADD CONSTRAINT
	PK_AccWarehouseInfo PRIMARY KEY CLUSTERED 
	(
	ID
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

GO
ALTER TABLE dbo.AccWarehouseInfo WITH NOCHECK ADD CONSTRAINT
	FK_AccWarehouseInfo_AccControl FOREIGN KEY
	(
	AccID
	) REFERENCES dbo.AccControl
	(
	ID
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.AccWarehouseInfo
	NOCHECK CONSTRAINT FK_AccWarehouseInfo_AccControl
GO
ALTER TABLE dbo.AccWarehouseInfo WITH NOCHECK ADD CONSTRAINT
	FK_AccWarehouseInfo_AccDetail FOREIGN KEY
	(
	AccID
	) REFERENCES dbo.AccDetail
	(
	ID
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.AccWarehouseInfo
	NOCHECK CONSTRAINT FK_AccWarehouseInfo_AccDetail
GO
CREATE TRIGGER AccWarehouseInfo_Delete
ON dbo.AccWarehouseInfo for DELETE 
	AS
		INSERT INTO AccWarehouseInfoTemp select * from deleted

     	--update AppSetting set TheValue = dbo.StrDate(getdate()) where [Name]='AccWarehouseInfoLastUpdate'
	return
GO
CREATE TRIGGER AccWarehouseInfo_Insert
ON dbo.AccWarehouseInfo FOR Insert
	AS
     	--update AppSetting set TheValue = dbo.StrDate(getdate()) where [Name]='AccWarehouseInfoLastUpdate'
	return
GO
CREATE TRIGGER AccWarehouseInfo_LastUpdate
ON dbo.AccWarehouseInfo FOR UPDATE 
	AS
	if not (UPDATE(UpdateDate))
		update AccWarehouseInfo set UpdateDate=getdate() where id in (select id from inserted)

     	--update AppSetting set TheValue = dbo.StrDate(getdate()) where [Name]='AccWarehouseInfoLastUpdate'
	return
GO
COMMIT
