从 Oracle 中的插入创建更新 sql

Creating an update from insert in oracle sql

我已经有一个插件可以正常工作了。但是我无法使更新工作。我正在使用 application express 和 oracle sql。以下是我想出的。然而,它似乎只是在添加新行,创建一个副本。不更新当前行数据。

  DECLARE

  l_upload_size INTEGER;
  l_upload_blob BLOB;
  l_image_id    NUMBER;
  l_image       ORDSYS.ORDImage;
  l_name        VARCHAR2(100);
  l_address        VARCHAR2(100);  
  l_postcode       VARCHAR2(100);
  l_description   VARCHAR2(100);

BEGIN

  --
  -- Get the BLOB of the new image from the APEX_APPLICATION_TEMP_FILES (synonym for WWV_FLOW_TEMP_FILES)
  -- APEX 5.0 change from APEX_APPLICATION_FILES which has been deprecated
  -- APEX_APPLICATION_TEMP_FILES has fewer columns and is missing doc_size
  --

  SELECT
    blob_content
  INTO
    l_upload_blob
  FROM
    apex_application_temp_files
  WHERE
    name = :P3_filename;
  --
  -- Insert row into the table, initialising the image and
  -- returning the newly allocated image_id for later use
  --
  INSERT
  INTO
    bars
    (
      filename,
      image,
      name,
      address,
      postcode,
      description

    )
    VALUES
    (
     :P3_filename,
      ORDSYS.ORDImage(),
     :P3_NAME,
     :P3_ADDRESS,
     :P3_POSTCODE,
     :P3_DESCRIPTION
    )
  RETURNING
    image_id, image
  INTO
    l_image_id, l_image;

  -- find the size of BLOB (get doc_size)
  l_upload_size := dbms_lob.getlength(l_upload_blob);
  -- copy the blob into the ORDImage BLOB container
  DBMS_LOB.COPY( l_image.SOURCE.localData, l_upload_blob, l_upload_size );

  -- set the image properties
  l_image.setProperties(); 
  create_blob_thumbnail(l_image_id);

  UPDATE
    bars
  SET
    image     = l_image -- original ORDImage image
  WHERE
    image_id = l_image_id;

END;

您似乎在寻找 MERGE 命令。尝试这样的事情:

MERGE INTO bars DEST_TABLE
USING (select :P3_filename as filename from dual) SOURCE_TABLE
ON (DEST_TABLE.name = SOURCE_TABLE.filename)
WHEN MATCHED THEN 
 UPDATE SET  image = ORDSYS.ORDImage()
WHEN NOT MATCHED THEN 
INSERT (
      filename,
      image,
      name,
      address,
      postcode,
      description)
      VALUES (:P3_filename,
      ORDSYS.ORDImage(),
     :P3_NAME,
     :P3_ADDRESS,
     :P3_POSTCODE,
     :P3_DESCRIPTION);