2
\$\begingroup\$

I have written below SQL in an RPGLE program. Intent is to update the header file (TC400F) if no corresponding records exist in detail file (TC401F). Are there better ways of doing this?

Exec SQL UPDATE  TC400F                     
            SET  T40STS = '05',             
                 T40OFL = '1'               
          WHERE  T40SID = :K#T41SID AND     
                 T40PID = :K#T41PID AND     
     NOT EXISTS  (SELECT * FROM TC401F WHERE
                 T41SID = :K#T41SID AND     
                 T41PID = :K#T41PID );      
\$\endgroup\$
1

1 Answer 1

1
\$\begingroup\$

Not really but you can simplify your query like this (variable are not replaced 2x)

Exec SQL UPDATE  TC400F f1                     
         SET  (f1.T40STS, f1.T40OFL) = ('05', '1')             
         WHERE  (f1.T40SID, f1.T40PID) = (:K#T41SID, :K#T41PID) AND    
         NOT EXISTS
         (SELECT * FROM TC401F f2 WHERE (f1.T40SID, f1.T40PID)=(f2.T41SID, f2.T41PID));
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Did you mean (f2.T40SID, f2.T40PID) or (f2.T41SID, f2.T41PID) in the subselect? \$\endgroup\$ Commented Oct 30, 2017 at 14:17
  • \$\begingroup\$ i have change my query, ty \$\endgroup\$ Commented Oct 30, 2017 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.