﻿
BEGIN TRANSACTION
GO
PRINT N'Altering [dbo].[MessageHeader]...';


GO
ALTER TABLE [dbo].[MessageHeader]
    ADD [BSMDStatus] TINYINT NULL;

GO

ALTER TABLE [dbo].[MessageHeader]
    ADD [HIS] TINYINT NULL;

GO

PRINT N'Altering [dbo].[MessageCore]...';

GO
ALTER TABLE [dbo].[MessageCore]
    ADD [Wetris_zz_56_datensatz_id] INT NULL;

GO

ALTER TABLE [dbo].[MessageCore]
    ADD [BSMDStatus] TINYINT NULL;

GO

ALTER TABLE MessageCore ADD [Created] [smalldatetime] NULL
ALTER TABLE MessageCore ADD [Changed] [smalldatetime] NULL

ALTER TABLE [dbo].[MessageCore] ADD [InitialHIS] TINYINT NULL;

ALTER TABLE MessageHeader ADD [Created] [smalldatetime] NULL
ALTER TABLE MessageHeader ADD [Changed] [smalldatetime] NULL


ALTER TABLE MessageCore Add
	CONSTRAINT [DF_MessageCore_Created] DEFAULT (getdate()) FOR [Created]
GO

CREATE TRIGGER MessageCore_Trigger_Change_Log
ON
	dbo.MessageCore
	FOR UPDATE
	AS
		SET NOCOUNT ON
		IF NOT UPDATE([Changed])
		UPDATE MessageCore SET [Changed] = GETDATE() WHERE Id IN (SELECT Id FROM [inserted])
GO

ALTER TABLE MessageHeader Add
	CONSTRAINT [DF_MessageHeader_Created] DEFAULT (getdate()) FOR [Created]
GO

CREATE TRIGGER MessageHeader_Trigger_Change_Log
ON
	dbo.MessageHeader
	FOR UPDATE
	AS
		SET NOCOUNT ON
		IF NOT UPDATE([Changed])
		UPDATE MessageHeader SET [Changed] = GETDATE() WHERE Id IN (SELECT Id FROM [inserted])
GO


IF @@ERROR <> 0
   AND @@TRANCOUNT > 0
    BEGIN
        ROLLBACK;
    END

IF @@TRANCOUNT = 0
    BEGIN
        INSERT  INTO #tmpErrors (Error)
        VALUES                 (1);
        BEGIN TRANSACTION;
    END


GO

IF EXISTS (SELECT * FROM #tmpErrors) ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT>0 BEGIN
PRINT N'The transacted portion of the database update succeeded.'
COMMIT TRANSACTION
END
ELSE PRINT N'The transacted portion of the database update failed.'
GO
DROP TABLE #tmpErrors
GO
PRINT N'Update complete.';


GO
