UPDATE TBDD_MODULES SET PRODUCT_VERSION = '1.0.0.1' where SHORT_NAME = 'UM'
GO
BEGIN
	IF NOT EXISTS (SELECT 1 FROM TBDD_GROUPS WHERE NAME = 'UM_ADMINS')
		INSERT INTO TBDD_GROUPS (NAME,INTERNAL) VALUES ('UM_ADMINS',1);
END

ALTER TABLE [dbo].[TBDD_GROUPS_USER] DROP CONSTRAINT [FK_TBDD_GROUPS_USER_GROUP_ID]
GO
ALTER TABLE [dbo].[TBDD_GROUPS_USER]  WITH CHECK ADD  CONSTRAINT [FK_TBDD_GROUPS_USER_GROUP_ID] FOREIGN KEY([GROUP_ID])
REFERENCES [dbo].[TBDD_GROUPS] ([GUID])
GO

ALTER TABLE [dbo].[TBDD_GROUPS_USER] CHECK CONSTRAINT [FK_TBDD_GROUPS_USER_GROUP_ID]
GO

DECLARE @UM_GROUP_ID INT 
SELECT @UM_GROUP_ID = GUID FROM  TBDD_GROUPS WHERE NAME = 'UM_ADMINS'
INSERT INTO TBDD_GROUPS_USER (GROUP_ID,USER_ID)
	SELECT @UM_GROUP_ID,B.GUID FROM TBIMPORT_USER A INNER JOIN TBDD_USER B ON A.USR_NAME = B.USERNAME
GO
CREATE TABLE TBUM_AD_COMMAND
(
	GUID			INTEGER	IDENTITY(1,1),    
	AD_CMD_NAME     VARCHAR(250),
	COMMAND_STRING	VARCHAR(MAX),
	ADDED_WHO		VARCHAR(50)		DEFAULT 'DEFAULT'		NOT NULL,
	ADDED_WHEN		DATETIME 		DEFAULT GETDATE(),
	CHANGED_WHO		VARCHAR(50),
	CHANGED_WHEN	DATETIME,
	CONSTRAINT		PK_TBUM_AD_COMMAND	PRIMARY KEY (GUID),
	CONSTRAINT		UC_TBUM_AD_COMMAND UNIQUE (AD_CMD_NAME),
)
GO
CREATE TRIGGER TBUM_AD_COMMAND_AFT_UPD ON TBUM_AD_COMMAND
FOR UPDATE
AS
	UPDATE TBUM_AD_COMMAND SET CHANGED_WHEN = GETDATE() FROM INSERTED 
	WHERE TBUM_AD_COMMAND.GUID = INSERTED.GUID
GO
CREATE PROCEDURE [dbo].[PRDD_DELETE_GROUP](@pGROUP_ID INT)
AS
BEGIN TRY
	-- COMMON
	IF OBJECT_ID(N'dbo.TBDD_GROUPS_USER', N'U') IS NOT NULL 
		DELETE FROM TBDD_GROUPS_USER WHERE GROUP_ID = @pGROUP_ID
	IF OBJECT_ID(N'dbo.TBDD_GROUPS_CLIENT', N'U') IS NOT NULL 
		DELETE FROM TBDD_GROUPS_CLIENT WHERE GROUP_ID = @pGROUP_ID
	IF OBJECT_ID(N'dbo.TBDD_GROUPS_MODULES', N'U') IS NOT NULL 
		DELETE FROM TBDD_GROUPS_MODULES WHERE GROUP_ID = @pGROUP_ID

	-- FINALLY
	DELETE FROM TBDD_GROUPS WHERE GUID = @pGROUP_ID

	RETURN 0
END TRY
BEGIN CATCH   
	PRINT 'FEHLER IN PROCEDURE: ' + CONVERT(VARCHAR(30),ERROR_PROCEDURE()) + ' - ZEILE: ' + CONVERT(VARCHAR(10),ERROR_LINE())
	+ ' - FEHLER-MESSAGE: ' 
    + CONVERT(VARCHAR(500),ERROR_MESSAGE())
	RETURN -1
END CATCH

GO