/*
   Mohon dilihat & dibaca & dipahami terlebih dahulu setiap baris query dibawah ini, sebelum di-execute.
   Terima kasih
*/

/* 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.m_eform_document
	DROP CONSTRAINT FK_m_eform_document_status
GO
ALTER TABLE dbo.m_status SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.m_eform_document
	DROP CONSTRAINT FK_m_eform_document_eform
GO
ALTER TABLE dbo.m_eform SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.m_eform_document
	DROP CONSTRAINT FK_m_eform_document_document
GO
ALTER TABLE dbo.m_document SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
CREATE TABLE dbo.Tmp_m_eform_document
	(
	eform_document_id int NOT NULL IDENTITY (1, 1),
	eform_id int NOT NULL,
	document_id int NOT NULL,
	minimum_files numeric(18, 0) NOT NULL,
	maximum_files numeric(18, 0) NOT NULL,
	applies_to char(1) NOT NULL,
	seq_number numeric(18, 0) NOT NULL,
	status_id int NOT NULL,
	created_by varchar(8) NOT NULL,
	created_date datetime NOT NULL,
	modified_by varchar(8) NOT NULL,
	modified_date datetime NOT NULL,
	is_delete char(1) NOT NULL
	)  ON [PRIMARY]
GO
ALTER TABLE dbo.Tmp_m_eform_document SET (LOCK_ESCALATION = TABLE)
GO
ALTER TABLE dbo.Tmp_m_eform_document ADD CONSTRAINT
	DF_m_eform_document_applies_to DEFAULT 'a' FOR applies_to
GO
SET IDENTITY_INSERT dbo.Tmp_m_eform_document ON
GO
IF EXISTS(SELECT * FROM dbo.m_eform_document)
	 EXEC('INSERT INTO dbo.Tmp_m_eform_document (eform_document_id, eform_id, document_id, minimum_files, maximum_files, seq_number, status_id, created_by, created_date, modified_by, modified_date, is_delete)
		SELECT eform_document_id, eform_id, document_id, minimum_files, maximum_files, seq_number, status_id, created_by, created_date, modified_by, modified_date, is_delete FROM dbo.m_eform_document WITH (HOLDLOCK TABLOCKX)')
GO
SET IDENTITY_INSERT dbo.Tmp_m_eform_document OFF
GO
-- sebelum didrop, cek dulu SELECT * FROM dbo.m_eform_document ada isinya atau kosong. Jika ada isinya, lanjutkan drop.
DROP TABLE dbo.m_eform_document
GO
EXECUTE sp_rename N'dbo.Tmp_m_eform_document', N'm_eform_document', 'OBJECT' 
GO
ALTER TABLE dbo.m_eform_document ADD CONSTRAINT
	PK__m_eform___BF27DE2600EA0E6F PRIMARY KEY CLUSTERED 
	(
	eform_document_id
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

GO
ALTER TABLE dbo.m_eform_document ADD CONSTRAINT
	FK_m_eform_document_document FOREIGN KEY
	(
	document_id
	) REFERENCES dbo.m_document
	(
	document_id
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.m_eform_document ADD CONSTRAINT
	FK_m_eform_document_eform FOREIGN KEY
	(
	eform_id
	) REFERENCES dbo.m_eform
	(
	eform_id
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
ALTER TABLE dbo.m_eform_document ADD CONSTRAINT
	FK_m_eform_document_status FOREIGN KEY
	(
	status_id
	) REFERENCES dbo.m_status
	(
	status_id
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 
	
GO
COMMIT
