/****
This SQL script was generated by the Configure Data Synchronization
dialog box. This script contains statements that create the
change-tracking columns, deleted-items table, and triggers on the
server database. These database objects are required by Synchronization
Services to successfully synchronize data between client and server
databases. For more information, see the
‘How to: Configure a Database Server for Synchronization’ topic in Help.
****/


IF @@TRANCOUNT > 0
set ANSI_NULLS ON 
set QUOTED_IDENTIFIER ON 

GO
BEGIN TRANSACTION;


IF @@TRANCOUNT > 0
ALTER TABLE [dbo].[tblMoamelehType] 
ADD [LastEditDate] DateTime NULL CONSTRAINT [DF_tblMoamelehType_LastEditDate] DEFAULT (GETUTCDATE()) WITH VALUES
GO
IF @@ERROR <> 0 
     ROLLBACK TRANSACTION;


IF @@TRANCOUNT > 0
ALTER TABLE [dbo].[tblMoamelehType] 
ADD [CreationDate] DateTime NULL CONSTRAINT [DF_tblMoamelehType_CreationDate] DEFAULT (GETUTCDATE()) WITH VALUES
GO
IF @@ERROR <> 0 
     ROLLBACK TRANSACTION;


IF @@TRANCOUNT > 0
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tblMoamelehType_Tombstone]')) 
BEGIN 
CREATE TABLE [dbo].[tblMoamelehType_Tombstone]( 
    [MoamelehTypeID] Int NOT NULL,
    [DeletionDate] DateTime NULL
)END 

GO
IF @@ERROR <> 0 
     ROLLBACK TRANSACTION;


IF @@TRANCOUNT > 0
ALTER TABLE [dbo].[tblMoamelehType_Tombstone] ADD CONSTRAINT [PKDEL_tblMoamelehType_Tombstone_MoamelehTypeID]
   PRIMARY KEY CLUSTERED
    ([MoamelehTypeID])
GO
IF @@ERROR <> 0 
     ROLLBACK TRANSACTION;


IF @@TRANCOUNT > 0
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tblMoamelehType_DeletionTrigger]') AND type = 'TR') 
   DROP TRIGGER [dbo].[tblMoamelehType_DeletionTrigger] 

GO
CREATE TRIGGER [dbo].[tblMoamelehType_DeletionTrigger] 
    ON [tblMoamelehType] 
    AFTER DELETE 
AS 
SET NOCOUNT ON 
UPDATE [dbo].[tblMoamelehType_Tombstone] 
    SET [DeletionDate] = GETUTCDATE() 
    FROM deleted 
    WHERE deleted.[MoamelehTypeID] = [tblMoamelehType_Tombstone].[MoamelehTypeID] 
IF @@ROWCOUNT = 0 
BEGIN 
    INSERT INTO [dbo].[tblMoamelehType_Tombstone] 
    ([MoamelehTypeID], DeletionDate)
    SELECT [MoamelehTypeID], GETUTCDATE()
    FROM deleted 
END 

GO
IF @@ERROR <> 0 
     ROLLBACK TRANSACTION;


IF @@TRANCOUNT > 0
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tblMoamelehType_UpdateTrigger]') AND type = 'TR') 
   DROP TRIGGER [dbo].[tblMoamelehType_UpdateTrigger] 

GO
CREATE TRIGGER [dbo].[tblMoamelehType_UpdateTrigger] 
    ON [dbo].[tblMoamelehType] 
    AFTER UPDATE 
AS 
BEGIN 
    SET NOCOUNT ON 
    UPDATE [dbo].[tblMoamelehType] 
    SET [LastEditDate] = GETUTCDATE() 
    FROM inserted 
    WHERE inserted.[MoamelehTypeID] = [tblMoamelehType].[MoamelehTypeID] 
END;
GO
IF @@ERROR <> 0 
     ROLLBACK TRANSACTION;


IF @@TRANCOUNT > 0
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tblMoamelehType_InsertTrigger]') AND type = 'TR') 
   DROP TRIGGER [dbo].[tblMoamelehType_InsertTrigger] 

GO
CREATE TRIGGER [dbo].[tblMoamelehType_InsertTrigger] 
    ON [dbo].[tblMoamelehType] 
    AFTER INSERT 
AS 
BEGIN 
    SET NOCOUNT ON 
    UPDATE [dbo].[tblMoamelehType] 
    SET [CreationDate] = GETUTCDATE() 
    FROM inserted 
    WHERE inserted.[MoamelehTypeID] = [tblMoamelehType].[MoamelehTypeID] 
END;
GO
IF @@ERROR <> 0 
     ROLLBACK TRANSACTION;
COMMIT TRANSACTION;
