
/* 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.AccDetail
	DROP CONSTRAINT DF_AccDetail_UpdateDate
GO
ALTER TABLE dbo.AccDetail
	DROP CONSTRAINT DF_AccDetail_Updator
GO
ALTER TABLE dbo.AccDetail
	DROP CONSTRAINT DF_AccDetail_Active
GO
CREATE TABLE dbo.Tmp_AccDetail
	(
	ID bigint NOT NULL IDENTITY (1, 1),
	Code char(10) NOT NULL,
	Title nvarchar(1000) NOT NULL,
	Type int NOT NULL,
	CreditType int NOT NULL,
	Description nvarchar(2000) NULL,
	Credit decimal(19, 2) NULL,
	Debit decimal(19, 2) NULL,
	UpdateDate datetime NOT NULL,
	Updater bigint NULL,
	Active bit NULL,
	AttachPath nvarchar(1000) NULL,
	TitleNoSpace nvarchar(1000) NULL
	)  ON [PRIMARY]
GO
ALTER TABLE dbo.Tmp_AccDetail ADD CONSTRAINT
	DF_AccDetail_UpdateDate DEFAULT (getdate()) FOR UpdateDate
GO
ALTER TABLE dbo.Tmp_AccDetail ADD CONSTRAINT
	DF_AccDetail_Updator DEFAULT ((0)) FOR Updater
GO
ALTER TABLE dbo.Tmp_AccDetail ADD CONSTRAINT
	DF_AccDetail_Active DEFAULT ((1)) FOR Active
GO
SET IDENTITY_INSERT dbo.Tmp_AccDetail ON
GO
IF EXISTS(SELECT * FROM dbo.AccDetail)
	 EXEC('INSERT INTO dbo.Tmp_AccDetail (ID, Code, Title, Type, CreditType, Description, Credit, Debit, UpdateDate, Updater, Active, AttachPath, TitleNoSpace)
		SELECT ID, Code, Title, Type, CreditType, Description, Credit, Debit, UpdateDate, Updater, Active, AttachPath, TitleNoSpace FROM dbo.AccDetail WITH (HOLDLOCK TABLOCKX)')
GO
SET IDENTITY_INSERT dbo.Tmp_AccDetail OFF
GO
ALTER TABLE dbo.AccDocItem
	DROP CONSTRAINT FK_AccDocItem_AccDetail
GO
ALTER TABLE dbo.AccDocItem
	DROP CONSTRAINT FK_AccDocItem_AccDetail1
GO
ALTER TABLE dbo.AccDetailDetail
	DROP CONSTRAINT FK_AccDetailDetail_AccDetail1
GO
ALTER TABLE dbo.AccDetailDetail
	DROP CONSTRAINT FK_AccDetailDetail_AccDetail2
GO
ALTER TABLE dbo.AccControlDetail
	DROP CONSTRAINT FK_AccControlDetail_AccDetail
GO
ALTER TABLE dbo.AccPersonInfo
	DROP CONSTRAINT FK_AccPersonInfo_AccDetail
GO
DROP TABLE dbo.AccDetail
GO
EXECUTE sp_rename N'dbo.Tmp_AccDetail', N'AccDetail', 'OBJECT' 
GO
ALTER TABLE dbo.AccDetail ADD CONSTRAINT
	PK_AccDetailID PRIMARY KEY CLUSTERED 
	(
	ID
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

GO
CREATE TRIGGER AccDetail_LastUpdate
ON dbo.AccDetail FOR UPDATE 
	AS
	if not (UPDATE(UpdateDate))
		update AccDetail set UpdateDate=getdate() where id in (select id from inserted)
        
	update AppSetting set TheValue = dbo.StrDate(getdate()) where [Name]='AccDetailLastUpdate'

	return
GO
CREATE TRIGGER AccDetail_Delete
ON dbo.AccDetail for DELETE 
	AS
		Insert into dbo.AccDetailTemp select * from deleted
		update AppSetting set TheValue = dbo.StrDate(getdate()) where [Name]='AccDetailLastUpdate'
	return
GO
CREATE TRIGGER AccDetail_Insert
ON dbo.AccDetail FOR Insert
	AS
		update AppSetting set TheValue = dbo.StrDate(getdate()) where [Name]='AccDetailLastUpdate'
   return
GO
COMMIT
BEGIN TRANSACTION
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
COMMIT
 BEGIN TRANSACTION
GO
ALTER TABLE dbo.AccControlDetail WITH NOCHECK ADD CONSTRAINT
	FK_AccControlDetail_AccDetail FOREIGN KEY
	(
	DetailID
	) REFERENCES dbo.AccDetail
	(
	ID
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
COMMIT
 BEGIN TRANSACTION
GO
ALTER TABLE dbo.AccDetailDetail WITH NOCHECK ADD CONSTRAINT
	FK_AccDetailDetail_AccDetail1 FOREIGN KEY
	(
	DetailID1
	) REFERENCES dbo.AccDetail
	(
	ID
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.AccDetailDetail WITH NOCHECK ADD CONSTRAINT
	FK_AccDetailDetail_AccDetail2 FOREIGN KEY
	(
	DetailID2
	) REFERENCES dbo.AccDetail
	(
	ID
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
COMMIT
 BEGIN TRANSACTION
GO
ALTER TABLE dbo.AccDocItem WITH NOCHECK ADD CONSTRAINT
	FK_AccDocItem_AccDetail FOREIGN KEY
	(
	DetailID1
	) REFERENCES dbo.AccDetail
	(
	ID
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.AccDocItem
	NOCHECK CONSTRAINT FK_AccDocItem_AccDetail
GO
ALTER TABLE dbo.AccDocItem WITH NOCHECK ADD CONSTRAINT
	FK_AccDocItem_AccDetail1 FOREIGN KEY
	(
	DetailID2
	) REFERENCES dbo.AccDetail
	(
	ID
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.AccDocItem
	NOCHECK CONSTRAINT FK_AccDocItem_AccDetail1
GO
COMMIT
