在oracle sql中从insert创建更新

时间:2015-12-31 14:46:16

标签: sql oracle oracle-apex

我已经有一个完全正常工作的插件。但是我无法使更新正常工作。我正在使用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;

1 个答案:

答案 0 :(得分:0)

您似乎正在寻找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);