ALTER PROCEDURE [dbo].[PRPMO_CREATE_TEMP_VIEWS] 
AS
BEGIN
	DECLARE
		@FORM_ID		INTEGER,
		@SQL_COMMAND	nvarchar(MAX),
		@VIEW_NAME		nvarchar(30),
		@DEL_SQL		NVARCHAR(100),
		@CREATE_VIEW	NVARCHAR(MAX)							
	DECLARE c_TASK CURSOR FOR 
		SELECT FORM_ID,SQL_COMMAND
		FROM TBPMO_FORM_SQL where SQL_COMMAND is not null
  	OPEN c_TASK 
    	FETCH NEXT FROM c_TASK INTO @FORM_ID,@SQL_COMMAND
    	WHILE @@FETCH_STATUS = 0
		BEGIN
			SET @VIEW_NAME = 'VWTEMP_PMO_FORM' + CONVERT(VARCHAR(3),@FORM_ID)
			IF EXISTS(select * FROM sys.views where name = @VIEW_NAME)
				BEGIN
					SET @DEL_SQL = 'DROP VIEW dbo.' + QUOTENAME(@VIEW_NAME) + ''
					EXEC sp_executesql @DEL_SQL
				END
			SELECT @SQL_COMMAND =  SQL_COMMAND FROM TBPMO_FORM_SQL where FORM_ID = @FORM_ID
			SET @CREATE_VIEW = 'CREATE VIEW ' + @VIEW_NAME + ' AS ' + @SQL_COMMAND
			
			EXEC sp_executesql @CREATE_VIEW
									        	
			FETCH NEXT FROM c_TASK INTO @FORM_ID,@SQL_COMMAND
		END
	CLOSE c_TASK
	DEALLOCATE c_TASK
  END

GO


