UPDATE TBDD_MODULES SET PRODUCT_VERSION = '2.4.6.6' where NAME = 'Record-Organizer' 
GO
ALTER PROCEDURE [dbo].[PRPMO_DELETE_ENTITY](@pENTITY_ID INT)
AS
BEGIN TRY
	DELETE FROM TBPMO_FORM_SQL WHERE FORM_ID = @pENTITY_ID
    DELETE FROM TBPMO_CLIENT_ENTITY WHERE ENTITY_ID = @pENTITY_ID
	DELETE FROM TBPMO_WORKFLOW_ENTITY_STATE WHERE ENTITY_ID = @pENTITY_ID
	DELETE FROM TBPMO_TEMPLATE_ENTITY WHERE ENTITY_ID = @pENTITY_ID
	DELETE FROM TBPMO_FORM_CONSTRUCTOR_DETAIL WHERE FORM_ID = @pENTITY_ID
	DELETE FROM TBPMO_CONTROL_LANGUAGE WHERE CONTROL_SCREEN_ID IN (SELECT GUID FROM TBPMO_CONTROL_SCREEN WHERE CONTROL_ID IN (SELECT GUID FROM TBPMO_CONTROL WHERE FORM_ID = @pENTITY_ID))
	DELETE FROM TBPMO_CONTROL_SCREEN WHERE CONTROL_ID IN (SELECT GUID FROM TBPMO_CONTROL WHERE FORM_ID = @pENTITY_ID)
	DELETE FROM TBPMO_CONTROL WHERE FORM_ID = @pENTITY_ID
	DELETE FROM TBPMO_ENTITY_SUPERVISOR WHERE ENTITY_ID = @pENTITY_ID
	DELETE FROM TBPMO_DOCSEARCH_RESULTLIST_CONFIG WHERE [ENTITY_ID]	 = @pENTITY_ID
	DELETE FROM TBPMO_RECORD_CONNECT WHERE RECORD1_ID in (SELECT GUID FROM TBPMO_RECORD WHERE FORM_ID = @pENTITY_ID)
	DELETE FROM TBPMO_RECORD_CONNECT WHERE RECORD2_ID in (SELECT GUID FROM TBPMO_RECORD WHERE FORM_ID = @pENTITY_ID)
	DELETE FROM TBPMO_RECORD_LOG WHERE RECORD_ID in (SELECT GUID FROM TBPMO_RECORD WHERE FORM_ID = @pENTITY_ID)
	DELETE FROM TBPMO_RECORD WHERE FORM_ID = @pENTITY_ID
	DELETE FROM TBPMO_ENTITY_VARIANT_REASONS WHERE ENTITY_ID = @pENTITY_ID
	DELETE FROM TBPMO_FORM_VIEW WHERE FORM_ID = @pENTITY_ID
	DELETE FROM TBPMO_FORM WHERE GUID = @pENTITY_ID
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())
END CATCH
GO
ALTER PROCEDURE [dbo].[PRPMO_COPY_ENTITY](@pFORM_ID INT, @pADDEDWHO VARCHAR(50))
AS
DECLARE	
		@NEW_FORM_ID INT,
		@PARENT_ID INT,
		@NAME		VARCHAR(100),
		@LEVEL		VARCHAR(10),
		@SHOW_FORM_CONSTR	BIT,
		@FORM_TYPE_ID INT,
		@CONTROL_GUID INTEGER,
		@CONTROL_TYPE_ID INT,
		@CONTROL_NAME VARCHAR(100)
		,@COL_NAME VARCHAR(100)
		,@REQUIRED BIT
		,@DEFAULT_VALUE VARCHAR(100)
		,@SQL_COMMAND_1 VARCHAR(2000)
		,@SQL_COMMAND_2 VARCHAR(2000)
		,@CONNECTION_ID_1 SMALLINT
		,@CONNECTION_ID_2 SMALLINT
		,@READ_ONLY BIT
		,@VALIDATION BIT
		,@MULTILINE BIT
		,@PARENT_CONTROL_ID INT
		,@MASTER_DATA_ID INT
		,@FORMAT_TYPE VARCHAR(50)
		,@SHOW_COLUMN BIT
		,@STATIC_LIST VARCHAR(2000)

		SELECT @PARENT_ID = PARENT_ID, @NAME = 'COPY_' + NAME,@LEVEL = [LEVEL],@SHOW_FORM_CONSTR =  SHOW_FORM_CONSTRUCT, @FORM_TYPE_ID = FORM_TYPE_ID FROM TBPMO_FORM WHERE GUID = @pFORM_ID
		
		INSERT INTO TBPMO_FORM (
			PARENT_ID,NAME,LEVEL,ADDED_WHO,SHOW_FORM_CONSTRUCT,FORM_TYPE_ID) VALUES (
			@PARENT_ID,@NAME,@LEVEL,@pADDEDWHO,@SHOW_FORM_CONSTR,@FORM_TYPE_ID)

		SELECT @NEW_FORM_ID =  MAX(GUID) FROM TBPMO_FORM
		INSERT INTO TBPMO_FORM_VIEW (FORM_ID, SCREEN_ID, FORM_TITLE, DESCRIPTION, ADDED_WHO) VALUES 
			(@NEW_FORM_ID, 1, 'UNDEFINED_FORMTITLE', '', @pADDEDWHO)

DECLARE	c_CONTROL	CURSOR FOR SELECT GUID,[CONTROL_TYPE_ID]
							  ,'COPY_' + [NAME]
							  ,[COL_NAME]
							  ,[REQUIRED]
							  ,[DEFAULT_VALUE]
							  ,[SQL_COMMAND_1]
							  ,[SQL_COMMAND_2]
							  ,[CONNECTION_ID_1]
							  ,[CONNECTION_ID_2]
							  ,[READ_ONLY]
							  ,[VALIDATION]
							  ,[MULTILINE]
							  ,[PARENT_CONTROL_ID]
							  ,[MASTER_DATA_ID]
							  ,[FORMAT_TYPE]
							  ,[SHOW_COLUMN],STATIC_LIST FROM TBPMO_CONTROL WHERE FORM_ID = @pFORM_ID ORDER BY PARENT_CONTROL_ID


BEGIN
	OPEN c_CONTROL 
    FETCH NEXT FROM c_CONTROL INTO @CONTROL_GUID,@CONTROL_TYPE_ID,
		@CONTROL_NAME 
		,@COL_NAME
		,@REQUIRED
		,@DEFAULT_VALUE
		,@SQL_COMMAND_1
		,@SQL_COMMAND_2
		,@CONNECTION_ID_1
		,@CONNECTION_ID_2
		,@READ_ONLY
		,@VALIDATION
		,@MULTILINE
		,@PARENT_CONTROL_ID
		,@MASTER_DATA_ID
		,@FORMAT_TYPE
		,@SHOW_COLUMN
		,@STATIC_LIST
    
	WHILE @@FETCH_STATUS = 0
		BEGIN
		IF @PARENT_CONTROL_ID > 0
			BEGIN
				DECLARE @CTRLNAME VARCHAR(100)
				SELECT @CTRLNAME = 'COPY_' + [NAME] FROM TBPMO_CONTROL WHERE GUID = @PARENT_CONTROL_ID
				PRINT 'PCTRID = ' + CONVERT(VARCHAR(10),@PARENT_CONTROL_ID)
				PRINT '@CTRLNAME = ' + @CTRLNAME
				SELECT @PARENT_CONTROL_ID = GUID FROM TBPMO_CONTROL WHERE NAME = @CTRLNAME AND FORM_ID = @NEW_FORM_ID
				PRINT 'NEW PCTRID = ' + CONVERT(VARCHAR(10),@PARENT_CONTROL_ID)
			END
			
			INSERT INTO TBPMO_CONTROL (
				FORM_ID
				,[CONTROL_TYPE_ID]
				,[NAME]
				,[COL_NAME]
				,[REQUIRED]
				,[DEFAULT_VALUE]
				,[SQL_COMMAND_1]
				,[SQL_COMMAND_2]
				,[CONNECTION_ID_1]
				,[CONNECTION_ID_2]
				,[READ_ONLY]
				,[VALIDATION]
				,[MULTILINE]
				,[PARENT_CONTROL_ID]
				,[MASTER_DATA_ID]
				,[FORMAT_TYPE]
				,[SHOW_COLUMN]
				,[STATIC_LIST]
				,ADDED_WHO
			) VALUES (
				@NEW_FORM_ID
				,@CONTROL_TYPE_ID
				,@CONTROL_NAME 
				,@COL_NAME
				,@REQUIRED
				,@DEFAULT_VALUE
				,@SQL_COMMAND_1
				,@SQL_COMMAND_2
				,@CONNECTION_ID_1
				,@CONNECTION_ID_2
				,@READ_ONLY
				,@VALIDATION
				,@MULTILINE
				,@PARENT_CONTROL_ID
				,@MASTER_DATA_ID
				,@FORMAT_TYPE
				,@SHOW_COLUMN
				,@STATIC_LIST
				,@pADDEDWHO
			)
				DECLARE @NEW_CONTROLID INTEGER
				SELECT @NEW_CONTROLID = MAX(GUID) FROM TBPMO_CONTROL

				DECLARE
				   @SCREEN_ID	INTEGER
				  ,@CONTROL_TEXT VARCHAR(100)
				  ,@X_LOC	FLOAT
				  ,@Y_LOC	FLOAT
				  ,@HEIGHT INTEGER
				  ,@WIDTH INTEGER
				  ,@FONT_FAMILY VARCHAR(50)
				  ,@FONT_COLOR INTEGER
				  ,@FONT_SIZE INTEGER
				  ,@FONT_STYLE INTEGER
				  ,@TAB_INDEX INTEGER
				  ,@TAB_STOP BIT
				  ,@BACK_COLOR INTEGER

				 SELECT @SCREEN_ID = [SCREEN_ID], @CONTROL_TEXT = [CONTROL_TEXT],@X_LOC = [X_LOC],
					@Y_LOC = [Y_LOC],@HEIGHT = [HEIGHT], @WIDTH = [WIDTH],@FONT_FAMILY = [FONT_FAMILY],
					@FONT_COLOR = [FONT_COLOR], @FONT_SIZE = [FONT_SIZE],@FONT_STYLE = [FONT_STYLE],
					@TAB_INDEX = [TAB_INDEX], @TAB_STOP = [TAB_STOP], @BACK_COLOR = [BACK_COLOR] FROM TBPMO_CONTROL_SCREEN WHERE CONTROL_ID = @CONTROL_GUID
				INSERT INTO TBPMO_CONTROL_SCREEN (
					CONTROL_ID,
					SCREEN_ID
					,CONTROL_TEXT
					,X_LOC
					,Y_LOC
					,HEIGHT
					,WIDTH
					,FONT_FAMILY
					,FONT_COLOR
					,FONT_SIZE
					,FONT_STYLE
					,TAB_INDEX 
					,TAB_STOP
					,BACK_COLOR
					,ADDED_WHO) 
				VALUES(
					@NEW_CONTROLID
					,@SCREEN_ID
					,@CONTROL_TEXT
					,@X_LOC
					,@Y_LOC
					,@HEIGHT
					,@WIDTH
					,@FONT_FAMILY
					,@FONT_COLOR
					,@FONT_SIZE
					,@FONT_STYLE
					,@TAB_INDEX
					,@TAB_STOP
					,@BACK_COLOR
					,@pADDEDWHO
				)
				DECLARE @NEW_ID INT
				SELECT @NEW_ID = MAX(GUID) FROM TBPMO_CONTROL_SCREEN WHERE CONTROL_TEXT = @CONTROL_TEXT
				
				INSERT INTO TBPMO_CONTROL_LANGUAGE ([LANGUAGE_TYPE] ,[CONTROL_SCREEN_ID],[CAPTION],[ADDED_WHO]) VALUES (
				'de-DE',@NEW_ID,@CONTROL_TEXT,@pADDEDWHO)
				INSERT INTO TBPMO_CONTROL_LANGUAGE ([LANGUAGE_TYPE] ,[CONTROL_SCREEN_ID],[CAPTION],[ADDED_WHO]) VALUES (
				'en-US',@NEW_ID,@CONTROL_TEXT,@pADDEDWHO)
			
			FETCH NEXT FROM c_CONTROL INTO @CONTROL_GUID,@CONTROL_TYPE_ID,
					@CONTROL_NAME 
					,@COL_NAME
					,@REQUIRED
					,@DEFAULT_VALUE
					,@SQL_COMMAND_1
					,@SQL_COMMAND_2
					,@CONNECTION_ID_1
					,@CONNECTION_ID_2
					,@READ_ONLY
					,@VALIDATION
					,@MULTILINE
					,@PARENT_CONTROL_ID
					,@MASTER_DATA_ID
					,@FORMAT_TYPE
					,@SHOW_COLUMN
					,@STATIC_LIST
		END

	CLOSE c_CONTROL
    DEALLOCATE c_CONTROL
END
GO

CREATE TABLE TBPMO_STRUCTURE_NODES_USER_TEMP(
	USER_ID			int					NOT NULL,
	RECORD_ID		int					NOT NULL,
	ADDED_WHEN		datetime			DEFAULT getdate(),
CONSTRAINT 	   	[PK_TBPMO_STRUCTURE_NODES_USER_TEMP] 	PRIMARY KEY(USER_ID,RECORD_ID))
GO
CREATE PROCEDURE PRPMO_NODES_GET_CHILD_RECORDS (@USER_ID INTEGER, @NODE_ID INTEGER)
AS
BEGIN
	DELETE FROM TBPMO_STRUCTURE_NODES_USER_TEMP WHERE USER_ID = @USER_ID;
	WITH tblChild AS
	(
    SELECT GUID,RECORD_ID FROM
        TBPMO_STRUCTURE_NODES WHERE GUID = @NODE_ID
    UNION ALL
    SELECT TBPMO_STRUCTURE_NODES.GUID,TBPMO_STRUCTURE_NODES.RECORD_ID FROM TBPMO_STRUCTURE_NODES  JOIN tblChild  ON TBPMO_STRUCTURE_NODES.PARENT_GUID = tblChild.GUID
	)
	INSERT INTO TBPMO_STRUCTURE_NODES_USER_TEMP (USER_ID,RECORD_ID) 
	SELECT @USER_ID,RECORD_ID FROM tblChild where RECORD_ID is not null
	OPTION(MAXRECURSION 32767)
END
GO

CREATE FUNCTION FNPMO_GET_WINDREAM_FOLDER_PATH (@pDOCID BIGINT)
RETURNS VARCHAR(MAX)
AS 
BEGIN
	DECLARE	
		@RESULT					VARCHAR(MAX),
		@DOCID					BIGINT,
		@a						TINYINT = 1,
		@i						TINYINT = 0,
		@PARENT_ID				BIGINT,
		@DATEINAME				NVARCHAR(255),
		@PARENTNAME				NVARCHAR(255),
		@FSFLAGS				INT,
		@WM_LAUFWERKBUCHSTABE	CHAR(1)		
	
	SELECT @WM_LAUFWERKBUCHSTABE = WM_LAUFWERKBUCHSTABE	FROM TBPMO_KONFIGURATION WHERE GUID = 1
	
	IF EXISTS(SELECT dwDocID FROM [172.24.12.41\BRAINPOOL].windream60.dbo.BaseAttributes WHERE dwDocID = @pDOCID)
		BEGIN
			--SELECT dwFSFlags FROM [172.24.12.41\BRAINPOOL].windream60.dbo.BaseAttributes WHERE dwDocID = 220733
			SELECT @FSFLAGS = dwFSFlags FROM [172.24.12.41\BRAINPOOL].windream60.dbo.BaseAttributes WHERE dwDocID = @pDOCID
			IF @FSFLAGS <> 16
				BEGIN
					SET @RESULT = 'THIS IS NOT A FOLDER (dwFSFlags <> 16)!'
					SET @DATEINAME = ''
				END
			ELSE
				BEGIN
					SELECT @DATEINAME = szLongName,@PARENT_ID = dwParentID FROM [172.24.12.41\brainpool].windream60.dbo.BaseAttributes WHERE dwDocID = @pDOCID
					WHILE @a > 0
						BEGIN
							IF @i = 0
								BEGIN
									IF @PARENT_ID = 1
										BEGIN
											SET @RESULT = '\'
											BREAK
										END
									ELSE
										BEGIN
											SET @DOCID = @PARENT_ID
											SET @RESULT = '\'
										END
								END
							ELSE
								BEGIN
									IF @PARENT_ID = 1
										BEGIN
											SET @RESULT = '\' + @PARENTNAME + @RESULT
											BREAK
										END
									ELSE
										BEGIN
											SET @DOCID = @PARENT_ID
											SET @RESULT = '\' + @PARENTNAME + @RESULT
										END
								END	
					
							SELECT @PARENTNAME = szLongName,@PARENT_ID = dwParentID FROM [172.24.12.41\brainpool].windream60.dbo.BaseAttributes WHERE dwDocID = @DOCID
							SET @i = @i + 1
						END
				END
		END
	ELSE
		BEGIN
			SET @RESULT = 'THIS DOC-ID DOES NOT EXIST!'
			SET @DATEINAME = ''
		END
	
	SET @RESULT = @WM_LAUFWERKBUCHSTABE +':' +  @RESULT + @DATEINAME
	--PRINT @RESULT
	RETURN @RESULT
END

GO
ALTER VIEW [dbo].[VWPMO_CONTROL_SCREEN] AS
SELECT TOP 100 PERCENT
	T.GUID			CONTROL_ID,
	T.FORM_ID		CONTROL_ENTITY_ID,
	T.NAME			CONTROL_NAME,
	T.REQUIRED		CONTROL_REQUIRED,
	T.DEFAULT_VALUE	CONTROL_DEF_VALUE,
	T.READ_ONLY		CONTROL_READ_ONLY,
	T.MULTILINE		CONTROL_MULTILINE,
	T.PARENT_CONTROL_ID	CONTROL_PARENT_ID,
	T.COL_NAME		CONTROL_COL_NAME,
	T.SHOW_COLUMN   CONTROL_SHOW_COLUMN,
	T.CONNECTION_ID_1	CONTROL_CONNID_1,
	T.SQL_COMMAND_1		CONTROL_SQLCOMMAND_1,
	T.SQL_COMMAND_2		CONTROL_SQLCOMMAND_2,
	T.VISIBLE		CONTROL_VISIBLE,
	T.TREE_VIEW		CONTROL_TREE_VIEW,
	T.STATIC_LIST	CONTROL_STATIC_LIST,
	T4.GUID			CTRLTYPE_ID,
	T4.TYPE_NAME	CTRLTYPE_NAME,
	T.FORMAT_TYPE	CONTROL_FORMAT_TYPE,
	T.FORM_ID,
	T3.NAME			FORM_NAME,
	T3.LEVEL		FORM_LEVEL,
	T1.SCREEN_ID	SCREEN_ID,
	T2.NAME			SCREEN_NAME,
	T1.GUID			CTRLSCR_ID,
	T1.CONTROL_TEXT	CTRLSCR_CAPTION,
	T1.X_LOC		CTRLSCR_X_LOC,
	T1.Y_LOC		CTRLSCR_Y_LOC,
	T1.HEIGHT		CTRLSCR_HEIGHT,
	T1.WIDTH		CTRLSCR_WIDTH,
	T1.FONT_FAMILY	CTRLSCR_FONT_FAMILY,
	T1.FONT_COLOR	CTRLSCR_FONT_COLOR,
	T1.FONT_SIZE	CTRLSCR_FONT_SIZE,
	T1.FONT_STYLE	CTRLSCR_FONT_STYLE,
	T1.TAB_INDEX	CTRLSCR_TAB_INDEX,
	T1.TAB_STOP		CTRLSCR_TAB_STOP,
	T1.BACK_COLOR   CTRLSCR_BACK_COLOR,
	T.MASTER_DATA_ID CTRLSCR_MASTER_DATA_ID
FROM
	TBPMO_CONTROL T,
	TBPMO_CONTROL_SCREEN T1,
	TBPMO_SCREEN T2,
	TBPMO_FORM T3,
	TBPMO_CONTROL_TYPE T4
WHERE
	T.FORM_ID = T3.GUID AND
	T.GUID = T1.CONTROL_ID AND
	T1.SCREEN_ID = T2.GUID AND
	T.CONTROL_TYPE_ID = T4.GUID
ORDER BY 
	T1.SCREEN_ID,
	T.FORM_ID,
	T1.TAB_STOP


GO
--CREATE PROCEDURE PRPMO_RIGHTS_SERVICE_CHECK_USER (@pUSER_ID INT,@pCHECK_WHO VARCHAR(50) = 'DD-Admin')
ALTER PROCEDURE [dbo].[PRPMO_RIGHTS_SERVICE_CHECK_USER] (@pUSER_ID INT,@pCHECK_WHO VARCHAR(50) = 'DD-Admin')
AS
BEGIN TRY
	DECLARE @CONSTR_DET_ID		INT,
			@ENTITY_ID			INT,
			@RECORD_ID			INT,
			@RECORD_SELECT		NVARCHAR(MAX),
			@RECORD_WHERE		NVARCHAR(MAX),
			@CREATE_VIEW		NVARCHAR(MAX),
			@USER_ID_ECM		INT,
			@WD_RIGHT			INT,
			@CONFIG				INT,
			@DOC_ID				INT,
			@DEL_SQL			NVARCHAR(30)

	DECLARE Cursor_constructors	CURSOR FOR	SELECT	T1.GUID, T1.FORM_ID FROM TBPMO_CONSTRUCTOR_USER T,TBPMO_FORM_CONSTRUCTOR_DETAIL T1,(SELECT DISTINCT TR.FORM_ID FROM TBPMO_DOC_RECORD_LINK T, TBPMO_RECORD TR WHERE T.RECORD_ID = TR.GUID) T2
											WHERE	T.CONSTRUCT_ID = T1.CONSTRUCT_ID AND T1.FORM_ID = T2.FORM_ID AND T.USER_ID = @pUSER_ID

	SELECT @USER_ID_ECM = USERID_FK_INT_ECM FROM TBDD_USER WHERE GUID = @pUSER_ID
	
	DELETE FROM TBPMO_RIGHTS_WORK_SERVICE WHERE USER_ID = @pUSER_ID
	
	OPEN Cursor_constructors 
	FETCH NEXT FROM Cursor_constructors INTO @CONSTR_DET_ID,@ENTITY_ID
	WHILE @@FETCH_STATUS = 0
		BEGIN
			IF EXISTS(select * FROM sys.views where name = 'VWTEMP_REC_USER')
				DROP VIEW VWTEMP_REC_USER
					
			SET @WD_RIGHT = 3
			IF EXISTS(SELECT GUID FROM TBPMO_ENTITY_SUPERVISOR WHERE USER_ID = @pUSER_ID AND ENTITY_ID = @ENTITY_ID)
				SELECT @WD_RIGHT = RIGHT1 FROM TBPMO_ENTITY_SUPERVISOR WHERE USER_ID = @pUSER_ID AND ENTITY_ID = @ENTITY_ID
			ELSE
				BEGIN
					IF EXISTS(SELECT GUID FROM TBPMO_ENTITY_USER_RIGHTS WHERE USER_ID = @pUSER_ID AND ENTITY_ID = @ENTITY_ID)
						BEGIN
							SELECT @CONFIG = [READ_ONLY] FROM TBPMO_ENTITY_USER_RIGHTS WHERE USER_ID = @pUSER_ID AND ENTITY_ID = @ENTITY_ID
							IF @CONFIG = 1
								BEGIN
									SET @WD_RIGHT = 1
									--Print 'User shall only read'
								END
						END
				END
			
			IF EXISTS(SELECT GUID FROM TBPMO_CONSTRUCTOR_USER_SQL WHERE CONSTR_DET_ID = @CONSTR_DET_ID AND USER_ID = @pUSER_ID)
				BEGIN
					SELECT @RECORD_WHERE = ISNULL(SQL_COMMAND1,'') FROM TBPMO_CONSTRUCTOR_USER_SQL WHERE CONSTR_DET_ID = @CONSTR_DET_ID AND USER_ID = @pUSER_ID
					IF LEN(@RECORD_WHERE) > 0
						BEGIN
							SET @RECORD_WHERE = REPLACE(@RECORD_WHERE,'@USER_ID',CONVERT(NVARCHAR(10),@pUSER_ID))
							SET @RECORD_WHERE = REPLACE(@RECORD_WHERE,'@NUMBER',CONVERT(NVARCHAR(3),@ENTITY_ID))
							SET @RECORD_SELECT = @RECORD_WHERE
						END
					ELSE
						BEGIN
							SET @RECORD_SELECT = N'SELECT [Record-ID] FROM VWTEMP_PMO_FORM' + CONVERT(NVARCHAR(3),@ENTITY_ID)
						END
				END
			ELSE
				BEGIN
					SET @RECORD_SELECT = N'SELECT [Record-ID] FROM VWTEMP_PMO_FORM' + CONVERT(NVARCHAR(3),@ENTITY_ID)
				END	
	
			SET @CREATE_VIEW = N'CREATE VIEW VWTEMP_REC_USER AS ' + @RECORD_SELECT
			EXEC (@CREATE_VIEW)
			
			DECLARE Cursor_Records CURSOR FOR SELECT [Record-ID] FROM VWTEMP_REC_USER 
						
			OPEN Cursor_Records 
			FETCH NEXT FROM Cursor_Records INTO @RECORD_ID
			WHILE @@FETCH_STATUS = 0
			BEGIN
				DECLARE Cursor_Docs CURSOR FOR	SELECT	T.DOC_ID FROM TBPMO_DOC_RECORD_LINK T,TBPMO_RECORD TR,(SELECT dwObjectID FROM VWPMO_WD_OBJECT_RIGHTS WHERE [dwUserOrGroupID] = @USER_ID_ECM) T1
												WHERE	T.RECORD_ID = TR.GUID AND T.DOC_ID = T1.dwObjectID AND T.RECORD_ID = @RECORD_ID AND TR.FORM_ID = @ENTITY_ID
							
				OPEN Cursor_Docs 
				FETCH NEXT FROM Cursor_Docs INTO @DOC_ID
				WHILE @@FETCH_STATUS = 0
				BEGIN
					--PRINT 'INSERTING ENT_ID: '  + CONVERT(VARCHAR(10),@ENTITY_ID) +' - DOC_ID: '  + CONVERT(VARCHAR(10),@DOC_ID) + ' - RIGHT: '+ CONVERT(VARCHAR(10),@WD_RIGHT)
					INSERT INTO TBPMO_RIGHTS_WORK_SERVICE(DOC_ID,USER_ID,RIGHT_MODIFY,FILE_RIGHT,REASON,ADDED_WHO) VALUES(@DOC_ID,@pUSER_ID,'ADD RIGHT', @WD_RIGHT,'USER_RIGHTS_RENEW',@pCHECK_WHO)
					FETCH NEXT FROM Cursor_Docs INTO @DOC_ID
				END
				CLOSE Cursor_Docs
				DEALLOCATE Cursor_Docs

				FETCH NEXT FROM Cursor_Records INTO @RECORD_ID
			END
			CLOSE Cursor_Records
			DEALLOCATE Cursor_Records
			
			SET @DEL_SQL = CONVERT(NVARCHAR(30),N'DROP VIEW VWTEMP_REC_USER')
			EXEC sp_executesql @DEL_SQL

			FETCH NEXT FROM Cursor_constructors INTO @CONSTR_DET_ID,@ENTITY_ID
		END
		CLOSE Cursor_constructors
		DEALLOCATE Cursor_constructors
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())
END CATCH

GO





