TheLazyPig
Programmer
Hi!
I get an error "No table is open in the current work area" in highlighted part.
Region and RegionBK have the same columns but RegionBK has another column named "deleted" it contains the date when the program ran.
The program deletes the duplicate old records in the region table and then inserts them into a backup table (regionbk).
Thank you!
I get an error "No table is open in the current work area" in highlighted part.
Code:
SET DELETED ON
SET SAFETY OFF
LOCAL pcCurrDir,pcScrPath,pcBckPath,pcRegion,pcAsOf,pcBkDate,pnCtr,lnRegCnt,lnDupCnt,lnDelCnt,lnDupCnt,lnTotCnt
*----------------------------------
pcCurrDir = "C:\BACKUP_FILES\Region"
pcScrPath = pcCurrDir+"\230308\region.dbf"
pcBckPath = pcCurrDir+"\230308\regionbk.dbf"
pcRegion = 'NCR' &®ION
pcAsOf = GOMONTH(DATE(2023,02,01),1)-.1 &&ASOF
pcBkDate = DATE(2023,02,28)
pnCtr = 0
*----------------------------------
IF ATC("REGION.DBF",pcScrPath) = 0
MESSAGEBOX("Invalid File Name!",48)
ELSE
USE (pcScrPath) IN 0 SHARED
SELECT region
COUNT TO lnRegCnt
WAIT WINDOW NOWAIT "Checking for duplicate records....."
**To Check Duplicate Records by Region
SELECT * ;
FROM region ;
WHERE RG+DIV+STA+EMPNO+FNAME+MI+LNAME IN ( ;
SELECT RG+DIV+STA+EMPNO+FNAME+MI+LNAME ;
FROM region ;
WHERE RG = pcRegion ;
GROUP BY 1 ;
HAVING COUNT(*)>1 ;
) ORDER BY EMPNO ;
INTO CURSOR curDup READWRITE
llWithDup = reccount("curDup") > 0
IF llWithDup
MESSAGEBOX("With Duplicate Record Found!")
**Duplicates that are less than asof date
IF asof < pcAsOf
**get count of duplicate
SELECT curDup
COUNT TO lnDupCnt
WAIT WINDOW NOWAIT "No. of Duplicate Records : " + TRANSFORM(lnDupCnt,"###,###,###.##")
**to insert duplicate record to regionbk for history
WAIT WINDOW NOWAIT "....Backup Records...."
=regBackUp()
MESSAGEBOX("Successfully Inserted Records!")
**delete duplicate records where asof less than pcAsOf
DELETE FROM region WHERE RG+DIV+STA+EMPNO IN (SELECT RG+DIV+STA+EMPNO FROM curDup WHERE asof < pcAsOf)
MESSAGEBOX("Successfully Deleted!")
ENDIF
ENDIF
**count of deleted records
SELECT region
COUNT TO lnDelCnt
WAIT WINDOW NOWAIT "No. of Records Deleted : " + TRANSFORM(lnDupCnt-lnDelCnt,"###,###,###.##")
**check if there's still duplicate records
SELECT * ;
FROM region ;
WHERE RG+DIV+STA+EMPNO+FNAME+MI+LNAME IN ( ;
SELECT RG+DIV+STA+EMPNO+FNAME+MI+LNAME ;
FROM region ;
WHERE RG = pcRegion ;
GROUP BY 1 ;
HAVING COUNT(*)>1 ;
) ORDER BY EMPNO ;
INTO CURSOR curDups
llWithDups = reccount("curDups") > 0
IF llWithDups
MESSAGEBOX("WITH DUPLICATE")
IF asof < pcAsOf
SELECT curDups
COUNT TO lnDupCnt
WAIT WINDOW NOWAIT "No. of Duplicate Records : " + TRANSFORM(lnDupCnt,"###,###,###.##")
ENDIF
ENDIF
SELECT region
COUNT TO lnTotCnt
PACK
MESSAGEBOX("Total Count : " + TRANSFORM(lnTotCnt,"###,###,###.##"))
ENDIF
**To backup deleted records from region to regionbk
PROCEDURE regBackUp
LOCAL lnCurDup
CLOSE ALL
USE (pcBckPath) ALIAS RegionBk
SELECT 0
USE (pcScrPath) IN 0 SHARED ALIAS Region
SELECT * ;
FROM region ;
WHERE RG+DIV+STA+EMPNO IN ( ;
SELECT RG+DIV+STA+EMPNO ;
FROM region ;
WHERE RG = pcRegion ;
GROUP BY 1 ;
HAVING COUNT(*)>1 ;
) AND asof < pcBkDate ;
INTO CURSOR curDup
COUNT TO lnCurDup
WAIT WINDOW NOWAIT "No. of Records Backup : " + TRANSFORM(lnCurDup,"###,###,###.##")
**Insert Duplicate Records in RegionBK (old records)
SELECT Regionbk
APPEND FROM DBF('curDup')
**Replace blank deleted date to SYSDATE
USE (pcBckPath) ALIAS RegionBk
SELECT 0
[highlight #FCE94F]REPLACE RegionBK.Deleted WITH DATE() FOR EMPTY(RegionBK.Deleted) IN (pcBckPath)[/highlight]
END PROC
Region and RegionBK have the same columns but RegionBK has another column named "deleted" it contains the date when the program ran.
The program deletes the duplicate old records in the region table and then inserts them into a backup table (regionbk).
Thank you!