--Insert trigger
CREATE TRIGGER [dbo].[TBPMO_CONTROL_SCREEN_AFT_INS] ON [dbo].[TBPMO_CONTROL_SCREEN]
WITH EXECUTE AS CALLER
FOR INSERT
AS
BEGIN TRY
	DECLARE 
		@GUID				INTEGER,
		@CONTROL_ID			INTEGER,
		@ENTITY_ID			INTEGER,
		@SEQUENCE			INTEGER
	SELECT
  		@GUID = GUID,
		@CONTROL_ID = CONTROL_ID
	FROM INSERTED

	SELECT 
		@ENTITY_ID = FORM_ID 
	FROM TBPMO_CONTROL WHERE [GUID] = @CONTROL_ID

	SELECT @SEQUENCE = MAX(TAB_INDEX) FROM TBPMO_CONTROL_SCREEN WHERE CONTROL_ID IN (SELECT GUID FROM TBPMO_CONTROL WHERE FORM_ID = @ENTITY_ID)

	UPDATE TBPMO_CONTROL_SCREEN SET TAB_INDEX = @SEQUENCE + 1 FROM INSERTED WHERE TBPMO_CONTROL_SCREEN.GUID = INSERTED.GUID

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

