/* 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.AccPersonInfo
	DROP CONSTRAINT FK_AccPersonInfo_AccDetail
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.AccPersonInfo
	DROP CONSTRAINT FK_AccPersonInfo_AccControl
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.AccPersonInfo
	DROP CONSTRAINT DF_AccPersonInfo_Type
GO
ALTER TABLE dbo.AccPersonInfo
	DROP CONSTRAINT DF_AccPersonInfo_UpdateDate
GO
ALTER TABLE dbo.AccPersonInfo
	DROP CONSTRAINT DF_AccPersonInfo_Updater
GO
CREATE TABLE dbo.Tmp_AccPersonInfo
	(
	ID bigint NOT NULL IDENTITY (1, 1),
	Type tinyint NOT NULL,
	AccID bigint NOT NULL,
	Address nvarchar(250) NULL,
	Tel1 nvarchar(250) NULL,
	Tel2 nvarchar(250) NULL,
	Mobile nvarchar(250) NULL,
	Fax nvarchar(250) NULL,
	Email nvarchar(250) NULL,
	MaxCredit decimal(19, 2) NULL,
	Description nvarchar(2000) NULL,
	FinancialCode nchar(20) NULL,
	UpdateDate datetime NULL,
	Updater bigint NULL,
	Web char(100) NULL,
	MelliID char(20) NULL,
	Name nvarchar(50) NULL,
	Code char(10) NULL,
	Country bigint NULL,
	City bigint NULL,
	InIran bit NULL,
	RealOrCompany bit NULL,
	CompanyType int NULL,
	GroupType int NULL,
	OtherGroupType bigint NULL,
	VizitorTotalID bigint NULL,
	VizitorControlID bigint NULL,
	VizitorDetailID1 bigint NULL,
	VizitorDetailID2 bigint NULL,
	CodePosti char(10) NULL,
	CodeSabt char(20) NULL,
	VizitorPercent float(53) NULL,
	DiscontPercent float(53) NULL
	)  ON [PRIMARY]
GO
ALTER TABLE dbo.Tmp_AccPersonInfo ADD CONSTRAINT
	DF_AccPersonInfo_Type DEFAULT ((0)) FOR Type
GO
ALTER TABLE dbo.Tmp_AccPersonInfo ADD CONSTRAINT
	DF_AccPersonInfo_UpdateDate DEFAULT (getdate()) FOR UpdateDate
GO
ALTER TABLE dbo.Tmp_AccPersonInfo ADD CONSTRAINT
	DF_AccPersonInfo_Updater DEFAULT ((0)) FOR Updater
GO
SET IDENTITY_INSERT dbo.Tmp_AccPersonInfo ON
GO
IF EXISTS(SELECT * FROM dbo.AccPersonInfo)
	 EXEC('INSERT INTO dbo.Tmp_AccPersonInfo (ID, Type, AccID, Address, Tel1, Tel2, Mobile, Fax, Email, MaxCredit, Description, FinancialCode, UpdateDate, Updater, Web, MelliID, Name, Code, Country, City, InIran, RealOrCompany, CompanyType, GroupType, OtherGroupType, VizitorTotalID, VizitorControlID, VizitorDetailID1, VizitorDetailID2, CodePosti, CodeSabt, VizitorPercent, DiscontPercent)
		SELECT ID, Type, AccID, Address, CONVERT(nvarchar(250), Tel1), CONVERT(nvarchar(250), Tel2), CONVERT(nvarchar(250), Mobile), CONVERT(nvarchar(250), Fax), CONVERT(nvarchar(250), Email), MaxCredit, Description, FinancialCode, UpdateDate, Updater, Web, MelliID, Name, Code, Country, City, InIran, RealOrCompany, CompanyType, GroupType, OtherGroupType, VizitorTotalID, VizitorControlID, VizitorDetailID1, VizitorDetailID2, CodePosti, CodeSabt, VizitorPercent, DiscontPercent FROM dbo.AccPersonInfo WITH (HOLDLOCK TABLOCKX)')
GO
SET IDENTITY_INSERT dbo.Tmp_AccPersonInfo OFF
GO
DROP TABLE dbo.AccPersonInfo
GO
EXECUTE sp_rename N'dbo.Tmp_AccPersonInfo', N'AccPersonInfo', 'OBJECT' 
GO
ALTER TABLE dbo.AccPersonInfo ADD CONSTRAINT
	PK_AccPersonInfo 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.AccPersonInfo WITH NOCHECK ADD CONSTRAINT
	FK_AccPersonInfo_AccControl FOREIGN KEY
	(
	AccID
	) REFERENCES dbo.AccControl
	(
	ID
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.AccPersonInfo
	NOCHECK CONSTRAINT FK_AccPersonInfo_AccControl
GO
ALTER TABLE dbo.AccPersonInfo WITH NOCHECK ADD CONSTRAINT
	FK_AccPersonInfo_AccDetail FOREIGN KEY
	(
	AccID
	) REFERENCES dbo.AccDetail
	(
	ID
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.AccPersonInfo
	NOCHECK CONSTRAINT FK_AccPersonInfo_AccDetail
GO
CREATE TRIGGER AccPersonInfo_LastUpdate
ON dbo.AccPersonInfo FOR UPDATE 
	AS
	if not (UPDATE(UpdateDate))
		update AccPersonInfo set UpdateDate=getdate() where id in (select id from inserted)

     	update AppSetting set TheValue = dbo.StrDate(getdate()) where [Name]='AccPersonInfoLastUpdate'
	return
GO
CREATE TRIGGER AccPersonInfo_Delete
ON dbo.AccPersonInfo FOR DELETE 
	AS
		INSERT INTO AccPersonInfoTemp select * from deleted
     	update AppSetting set TheValue = dbo.StrDate(getdate()) where [Name]='AccPersonInfoLastUpdate'
	return
GO
CREATE TRIGGER AccPersonInfo_Insert
ON dbo.AccPersonInfo FOR Insert
	AS
     	update AppSetting set TheValue = dbo.StrDate(getdate()) where [Name]='AccPersonInfoLastUpdate'
	return
GO
COMMIT
