--select PRODUCT_VERSION FROM TBDD_MODULES where NAME = 'Record-Organizer'
UPDATE TBDD_MODULES SET PRODUCT_VERSION = '2.4.8.5' where NAME = 'Record-Organizer' 
GO 
CREATE VIEW VWPMO_DOC_CHANGES_5MIN
AS
SELECT DocID, [Filename], FULL_FILENAME,
	Doctype, [Version] as [Version], Objecttype,
	Creation_DateTime, Change_DateTime,'CHANGE GENERAL' as CHANGE_TYPE 
FROM VWPMO_DOC_SYNC 
WHERE 
	Change_DateTime >=  DATEADD(MINUTE , -5,  GetDate())
UNION
SELECT T.DocID, T.[Filename], T.FULL_FILENAME,
	T.Doctype, T.[Version] as [Version], T.Objecttype,
	T.Creation_DateTime, T.Change_DateTime,'CHANGE PATHorFILE' as CHANGE_TYPE 
FROM VWPMO_DOC_SYNC T, TBPMO_DOCRESULT_LIST T1 
WHERE 
	T.DocID = T1.DocID AND 
	T.Change_DateTime >=  DATEADD(MINUTE , -5,  GetDate())
	AND UPPER(T.FULL_FILENAME) <> UPPER(T1.FULL_FILENAME) 
GO

CREATE PROCEDURE [dbo].[PRPMO_DAILY_JOB_06_12_18]
AS
BEGIN
	--PROCEDURE1
	PRINT 'PROCEDURE1'
	--EXEC PRPMO_ZSG_IN_COMOS_SAP;
	
END
GO
CREATE PROCEDURE [dbo].[PRPMO_DOCSYNC_5min]
AS
BEGIN TRY
DECLARE @DocID	INTEGER,
		@DISPLAY_NAME	VARCHAR(250),
	@FULL_FILENAME	VARCHAR(5000),
	@Filename		VARCHAR(500),
	@Doctype		VARCHAR(250),
	@Version		SMALLINT,
	@Objecttype	VARCHAR(250),
	@Creation_DateTime DATETIME,
	@Change_DateTime DATETIME
DECLARE c_Values CURSOR LOCAL FOR 
	select DocID, [Filename], FULL_FILENAME,
	Doctype, [Version] as [Version], Objecttype,
	Creation_DateTime, Change_DateTime from VWPMO_DOC_CHANGES_5MIN
 	OPEN c_Values 
   	FETCH NEXT FROM c_Values INTO @DocID,@Filename,@FULL_FILENAME,
	@Doctype,@Version,@Objecttype,
	@Creation_DateTime,@Change_DateTime
   	WHILE @@FETCH_STATUS = 0
	BEGIN
		IF NOT EXISTS(SELECT [DocID] FROM TBPMO_DOCRESULT_LIST WHERE DocID = @DocID)
			BEGIN
			PRINT 'INSERT NECESSARY'
			INSERT INTO TBPMO_DOCRESULT_LIST (
				[DocID],
				[DISPLAY_NAME],
				[FULL_FILENAME],
				[Filename],
				[Doctype],
				[Version],
				[Objecttype],
				Creation_DateTime,
				Change_DateTime) 
			VALUES (
				@DocID,
				@Filename,
				@FULL_FILENAME,
				@Filename,
				@Doctype,
				@Version,
				@Objecttype,
				@Creation_DateTime,
				@Change_DateTime
				)
			END
		ELSE
			UPDATE TBPMO_DOCRESULT_LIST SET 
				[FULL_FILENAME] = @FULL_FILENAME,
				[Filename] = @Filename,
				[Doctype] = @Doctype,
				[Version] = @Version,
				[Objecttype] = @Objecttype,
				Change_DateTime = @Change_DateTime
			WHERE DocID = @DocID
				
		--#####
		FETCH NEXT FROM c_Values INTO @DocID,@Filename,@FULL_FILENAME,
			@Doctype,@Version,@Objecttype,@Creation_DateTime,@Change_DateTime
	END
CLOSE c_Values
DEALLOCATE c_Values
END TRY
BEGIN CATCH   
	PRINT 'ERROR IN PROCEDURE: ' + CONVERT(VARCHAR(30),ERROR_PROCEDURE()) + ' - ROW: ' + CONVERT(VARCHAR(10),ERROR_LINE())
	+ ' - ERROR-MESSAGE: ' 
    + CONVERT(VARCHAR(500),ERROR_MESSAGE())
END CATCH

GO

CREATE PROCEDURE [dbo].[PRPMO_DAILY_JOB_5MIN]
AS
BEGIN
	EXEC PRPMO_DOCSYNC_5min
END
GO
