DB2 SQL delete to purge Header/Detail records

pts.
Tags:
IBM DB2
SQL
I have a Header file with Detail records that belong to it. I need to copy the detail data off to a backup file, delete the detail records where the Header record entered date is greater than xx days from the current date, copy the header data off to a backup file and finally delete the header record. I came up with this: BEGIN SET @days = 30; SET nDate = integer( substring(char(date(now()),ISO),1,4) || substring(char(date(now()),ISO),6,2) || substring(char(date(now()),ISO),9,2) ); SET NewDate = (nDate - @days); INSERT INTO EKDBK -- new backup file ( BDDATE , BDWHS , BDDEPT , BDVNDR , BDJOB , BDDTS ) SELECT KDDATE , KDWHS , KDDEPT , KDVNDR , KDITEM FROM EKDL01 d2 -- detail file WHERE EXISTS ( SELECT h2.KHWHS , h2.KHDEPT , h2.KHVNDR , h2.KHDATE FROM EKH h2 -- Header file WHERE h2.KHDATE <= NewDate AND h2.KHWHS = d2.KDWHS AND h2.KHDEPT = d2.KDDEPT AND h2.KHVNDR = d2.KDVNDR ) ;

Answer Wiki

Thanks. We'll let you know when a new response is added.

Recommend you use TYPE date instead of TYPE integer for nDate. In your SQL, a current date of 15 Nov 2004 gives:

20041115 – 30 = 20041085 (not a valid date)

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following