UPDATE TBDD_MODULES SET PRODUCT_VERSION = '2.4.7.6' where NAME = 'Record-Organizer' 
GO 
ALTER PROCEDURE [dbo].[PRPMO_CREATE_ENTITY_TABLE_VIEW](@pENTITY_ID INT)
AS
DECLARE	@COL_NAME		VARCHAR(50),
		@SQL_BEGIN		NVARCHAR(4000),
		@SQL_END		NVARCHAR(4000),
		@SQL			NVARCHAR(MAX),
		@CONTROL_ID		INT,
		@RESULT			NVARCHAR(MAX),
		@TYPEID			INT,
		@TABINDEX		INT,
		@SQL_LBL		VARCHAR(4000),
		@ENTITY_TABLENAME	VARCHAR(50),
		@VWPMO_ENTITY_TABLE	VARCHAR(100)

SELECT @ENTITY_TABLENAME = ENTITY_TABLENAME FROM TBPMO_KONFIGURATION WHERE GUID = 1	
	
DECLARE	c_SPALTENNAMEN	CURSOR FOR	SELECT		T.GUID,'[' + T.COL_NAME + ']' AS 'COL_NAME',T2.GUID,T1.TAB_INDEX, '' FROM TBPMO_CONTROL T,TBPMO_CONTROL_SCREEN T1,TBPMO_CONTROL_TYPE T2
									WHERE		T.SHOW_COLUMN = 1 AND T.[COL_NAME] <> '' AND T.GUID = T1.CONTROL_ID AND T.CONTROL_TYPE_ID = T2.GUID AND T.FORM_ID = @pENTITY_ID AND LEN(ISNULL(T.COL_NAME,'')) > 0 AND T.CONTROL_TYPE_ID NOT IN (1,5)
									UNION
									SELECT		T.GUID,'[' + T.NAME + ']' AS 'COL_NAME',T2.GUID,T1.TAB_INDEX, T.SQL_COMMAND_1 FROM TBPMO_CONTROL T,TBPMO_CONTROL_SCREEN T1,TBPMO_CONTROL_TYPE T2
									WHERE		T.GUID = T1.CONTROL_ID AND T.CONTROL_TYPE_ID = T2.GUID AND T.FORM_ID = @pENTITY_ID AND T.CONTROL_TYPE_ID = 1 AND LEN(ISNULL(T.SQL_COMMAND_1,'')) > 0
									ORDER BY	TAB_INDEX
BEGIN
	SET @VWPMO_ENTITY_TABLE = 'VWPMO_ENTITY_TABLE@pENTITY_ID'
	SET @SQL_BEGIN = 'CREATE VIEW @VIEW AS SELECT T.[Record-ID]' + CHAR(13) + ',T.ROW_COLOR' + CHAR(13) + ',@pENTITY_ID AS [Form-ID]' + CHAR(13) + ','
	SET @SQL_BEGIN = REPLACE(@SQL_BEGIN,'@VIEW',@VWPMO_ENTITY_TABLE)
	SET @SQL_BEGIN = REPLACE(@SQL_BEGIN,'@pENTITY_ID',CONVERT(VARCHAR(3),@pENTITY_ID))


	SET @VWPMO_ENTITY_TABLE = REPLACE(@VWPMO_ENTITY_TABLE,'@pENTITY_ID',CONVERT(VARCHAR(3),@pENTITY_ID))
	
	SET @SQL_END = 'T.file_exists as [files?],' + CHAR(13) + 'T.file_count as [file count],' + CHAR(13) + 'T.AddedWho,' + CHAR(13) + 'T.AddedWhen,' + CHAR(13) + 'T.ChangedWho,' + CHAR(13) + 'T.ChangedWhen FROM @TABLE@pENTITY_ID T' 
	SET @SQL_END = REPLACE(@SQL_END,'@pENTITY_ID',CONVERT(VARCHAR(3),@pENTITY_ID))
	SET @SQL_END = REPLACE(@SQL_END,'@TABLE',@ENTITY_TABLENAME)
	SET @SQL = ''
	OPEN c_SPALTENNAMEN 
    FETCH NEXT FROM c_SPALTENNAMEN INTO @CONTROL_ID,@COL_NAME,@TYPEID,@TABINDEX,@SQL_LBL
    
	WHILE @@FETCH_STATUS = 0
		BEGIN
			SET @COL_NAME = REPLACE(@COL_NAME,'lbl','')
			--PRINT @COL_NAME
			IF @TYPEID IN(10,11) -- CheckBox,RadioBox
				SET @SQL = @SQL + 'CONVERT(BIT,T.@COL_NAME) AS ' + @COL_NAME + ',' + CHAR(13)
			ELSE IF @TYPEID = 6 --PictureBox
				SET @SQL = @SQL + 'T.@COL_NAME AS ' + @COL_NAME + ',' + CHAR(13)
			ELSE IF @TYPEID = 1 --Label mit SQL
				BEGIN
					SET @SQL_LBL = REPLACE(UPPER(@SQL_LBL),'@RECORD_ID','T.[Record-ID]')
					SET @SQL = @SQL + '(' + @SQL_LBL + ') AS ' + @COL_NAME + ','  + CHAR(13)
				END
			--ELSE IF @TYPEID IN(7,12,14) --DGV,CheckedListBox,ListBox
			--	SET @SQL = @SQL + 'dbo.FNPMO_GET_CONTROL_VALUES(' + CONVERT(VARCHAR(20),@CONTROL_ID) + ', T.[Record-ID]) AS ' + @COL_NAME + ','   + CHAR(13)
			ELSE IF @TYPEID = 4 --Datepicker
				SET @SQL = @SQL + 'T.@COL_NAME AS ' + @COL_NAME + ','  + CHAR(13)
			ELSE
				SET @SQL = @SQL + 'ISNULL(T.@COL_NAME,'''') AS ' + @COL_NAME + ','  + CHAR(13)
			
			SET @SQL = REPLACE(@SQL,'@COL_NAME',@COL_NAME)
			FETCH NEXT FROM c_SPALTENNAMEN INTO @CONTROL_ID,@COL_NAME,@TYPEID,@TABINDEX,@SQL_LBL
		END

	CLOSE c_SPALTENNAMEN
    DEALLOCATE c_SPALTENNAMEN
	SET @RESULT = @SQL_BEGIN + @SQL + @SQL_END
	PRINT @RESULT
	
	IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME = @VWPMO_ENTITY_TABLE)
		BEGIN
			SET @SQL = 'DROP VIEW [dbo].[@VIEW]'
			SET @SQL = REPLACE(@SQL,'@VIEW',@VWPMO_ENTITY_TABLE)
			PRINT @SQL
			EXEC sp_executesql @SQL
		END
	EXEC sp_executesql @RESULT
	UPDATE TBPMO_FORM_SQL SET SQL_COMMAND1 = @RESULT WHERE FORM_ID = @pENTITY_ID
END

