1

I want to update all the document statuses to 9. How can I do this?

I did an update statement but it did not work - I do not know why.

SELECT
    sl.sap_filename,
    ds.document_status
FROM
    tbl_document ds,
    tbl_sap_log sl
WHERE
    ds.number = sl.number
    AND sap_id IN (44343, 55555, 66666, 77777, 88888) 
    AND ds.DOCUMENT_STATUS = 8;
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Python P
  • 25
  • 6
  • Does this answer your question? [Update statement with inner join on Oracle](https://stackoverflow.com/questions/2446764/update-statement-with-inner-join-on-oracle) – Makdous Apr 26 '20 at 21:17
  • [Bad habits to kick : using old-style JOINs](https://sqlblog.org/2009/10/08/bad-habits-to-kick-using-old-style-joins) - that old-style *comma-separated list of tables* style was replaced with the *proper* ANSI `JOIN` syntax in the ANSI-**92** SQL Standard (**more than 25 years** ago) and its use is discouraged – marc_s Apr 27 '20 at 04:21

2 Answers2

4

If you want to update with 9 all the documents from your select :

UPDATE tbl_document SET tbl_document.DOCUMENT_STATUS = 9
WHERE tbl_document.DOCUMENT_STATUS = 8
AND EXISTS (SELECT 1
            FROM tbl_sap_log  
            WHERE tbl_sap_log.number  = tbl_document.number AND sap_id IN (44343,55555,66666,77777,88888) );
Gabriel Durac
  • 2,610
  • 1
  • 12
  • 13
4

You could use MERGE:

MERGE INTO tbl_document ds
USING
    (SELECT number,
            sap_id
     FROM tbl_sap_log
     WHERE sap_id IN 
       (
        44343,
        55555,
        66666,
        77777,
        88888
       )
    ) sl
ON ( ds.number = sl.number )
WHEN MATCHED THEN
UPDATE SET
    ds.document_status = 9
WHERE ds.document_status = 8;
Lalit Kumar B
  • 47,486
  • 13
  • 97
  • 124