DECLARE
	@RECORD_ID	INTEGER,
	@DOC_ID	INTEGER,
	@ENT_ID	INTEGER,
	@Items	INTEGER,
	@Items1	INTEGER
								
	DECLARE c_REDO CURSOR FOR 
		SELECT T.RECORD_ID, T.DOC_ID, T1.FORM_ID 
		,(SELECT COUNT(GUID) FROM TBPMO_DOCSEARCH_RESULTLIST_CONFIG WHERE ENTITY_ID = T1.FORM_ID AND TYPE_ID = 3 AND CONFIG_COLUMNS = 1) as [DD_Items] 
		,(SELECT COUNT(GUID) FROM TBPMO_DOCSEARCH_RESULTLIST_CONFIG WHERE ENTITY_ID = T1.FORM_ID AND TYPE_ID <> 3 AND CONFIG_COLUMNS = 1) as [Values] FROM TBPMO_DOC_RECORD_LINK T, TBPMO_RECORD T1 WHERE T.RECORD_ID = T1.GUID
	OPEN c_REDO 
		FETCH NEXT FROM c_REDO INTO @RECORD_ID,@DOC_ID,@ENT_ID,@Items,@Items1
		WHILE @@FETCH_STATUS = 0
		BEGIN
			DECLARE @CONFID	INTEGER,@VALUE VARCHAR(100)

			IF @Items >= 1
				BEGIN
					DECLARE cursorValues CURSOR LOCAL FOR 
						SELECT T.GUID,T1.VALUE FROM TBPMO_DOCSEARCH_RESULTLIST_CONFIG T, TBPMO_DOCRESULT_DROPDOWN_ITEMS T1 WHERE T.GUID = T1.CONFIG_ID AND T1.[DEFAULT] = 1 AND T.ENTITY_ID = @ENT_ID AND T.TYPE_ID = 3
 					OPEN cursorValues 
					FETCH NEXT FROM cursorValues INTO @CONFID,@VALUE
   					WHILE @@FETCH_STATUS = 0
					BEGIN
						IF NOT EXISTS(SELECT GUID FROM TBPMO_DOC_VALUES WHERE DocID = @DOC_ID AND RECORD_ID = @RECORD_ID AND CONFIG_ID = @CONFID)
							BEGIN
								PRINT 'VALUES DROPDOWN'
								INSERT INTO TBPMO_DOC_VALUES (DocID,RECORD_ID,CONFIG_ID,VALUE,ADDED_WHO)  VALUES
								(@DOC_ID,@RECORD_ID,@CONFID,@VALUE,'PR_NEW')
							END
				
					FETCH NEXT FROM cursorValues INTO @CONFID,@VALUE
					END
					CLOSE cursorValues
					DEALLOCATE cursorValues
				END
			IF @Items1 >= 1
				BEGIN
					DECLARE cursorValues1 CURSOR LOCAL FOR 
						SELECT T.GUID FROM TBPMO_DOCSEARCH_RESULTLIST_CONFIG T WHERE T.ENTITY_ID = @ENT_ID AND T.TYPE_ID <> 3 AND T.CONFIG_COLUMNS = 1
 					OPEN cursorValues1 
					FETCH NEXT FROM cursorValues1 INTO @CONFID
   					WHILE @@FETCH_STATUS = 0
					BEGIN
						IF NOT EXISTS(SELECT GUID FROM TBPMO_DOC_VALUES WHERE DocID = @DOC_ID AND RECORD_ID = @RECORD_ID AND CONFIG_ID = @CONFID)
							BEGIN
								PRINT 'VALUES DEFAULT'
								INSERT INTO TBPMO_DOC_VALUES (DocID,RECORD_ID,CONFIG_ID,VALUE,ADDED_WHO)  VALUES
								(@DOC_ID,@RECORD_ID,@CONFID,'','PR_NEW')
							END
				
					FETCH NEXT FROM cursorValues1 INTO @CONFID
					END
					CLOSE cursorValues1
					DEALLOCATE cursorValues1
				END
		FETCH NEXT FROM c_REDO INTO @RECORD_ID,@DOC_ID,@ENT_ID,@Items,@Items1
	END
CLOSE c_REDO
DEALLOCATE c_REDO






