SQL 错误 [900] [42000]:ORA-00900:无效的 SQL 语句

问题描述 投票:0回答:1

我正在开发一个Oracle存储过程,我的oracle数据库版本是Oracle Database 11.2.0.1.0,我也非常熟悉MySQL和SQL Server,但我对Oracle真的很陌生,我尝试编写一个存储过程程序不起作用

CREATE OR REPLACE PROCEDURE update_ybm_for_exp_yb_ybjs IS

  CURSOR cur_exp_yb_ybjs IS
    SELECT setl_id, PHARMACY_ID, PATIENT_ID, prodno, batchno, created
    FROM exp_yb_ybjs_20240604_copy;

  v_setl_id      exp_yb_ybjs_20240604_copy.setl_id%TYPE;
  v_PHARMACY_ID   exp_yb_ybjs_20240604_copy.PHARMACY_ID%TYPE;
  v_PATIENT_ID   exp_yb_ybjs_20240604_copy.PATIENT_ID%TYPE;
  v_prodno       exp_yb_ybjs_20240604_copy.prodno%TYPE;
  v_batchno       exp_yb_ybjs_20240604_copy.batchno%TYPE;
  v_created    exp_yb_ybjs_20240604_copy.created%TYPE;

  myYBFL_ML yw_spxxhzb.YBFL_ML%TYPE;
BEGIN
  OPEN cur_exp_yb_ybjs;

  LOOP
    FETCH cur_exp_yb_ybjs INTO v_setl_id, v_PHARMACY_ID, v_PATIENT_ID, v_prodno, v_batchno, v_created;
    EXIT WHEN cur_exp_yb_ybjs%NOTFOUND;

    BEGIN
      -- Check if the record exists in yw_spxxhzb
      SELECT YBFL_ML
      INTO myYBFL_ML
      FROM (
       SELECT YBFL_ML, ROW_NUMBER() OVER (ORDER BY created DESC) AS rn
        FROM yw_spxxhzb
        WHERE created <= v_created AND prodno = v_prodno
      )
      WHERE rn = 1;

      -- Update the target table
      UPDATE exp_yb_ybjs_20240604_copy
      SET ITEM_ID= myYBFL_ML
      WHERE setl_id = v_setl_id
        AND PHARMACY_ID = v_PHARMACY_ID
        AND PATIENT_ID = v_PATIENT_ID
        AND prodno = v_prodno
        AND batchno = v_batchno
        AND created = v_created;

    EXCEPTION
      WHEN NO_DATA_FOUND THEN 
        
        -- Handle case where no matching record is found
        NULL; -- No action needed if no record is found
    END;
  END LOOP;

  CLOSE cur_exp_yb_ybjs;
END update_ybm_for_exp_yb_ybjs;
/

我遇到的问题是

   SQL Error [900] [42000]: ORA-00900: invalid SQL statement
   Error position: line: 8

线路代码是

   v_setl_id      exp_yb_ybjs_20240604_copy.setl_id%TYPE

知道我在这个存储过程中犯了什么错误吗?有什么建议 ?非常感谢你

我希望这个存储过程创建成功

stored-procedures plsql oracle11g
1个回答
0
投票

光标循环速度慢;如果可能的话,你想用 SQL 来完成这一切。

您似乎正在修改表的当前行,以将

ITEM_ID
设置为最新的
YBFL_ML
(不超过目标表的
created
日期)。您可以使用
MERGE
语句并关联
ROWID
伪列和相关子查询来查找最新的 ID:

CREATE OR REPLACE PROCEDURE update_ybm_for_exp_yb_ybjs
IS
BEGIN
  MERGE INTO exp_yb_ybjs_20240604_copy dst
  USING (
    SELECT ROWID AS rid,
           (
             SELECT MAX(y.YBFL_ML) KEEP (DENSE_RANK LAST y.created)
             FROM   yw_spxxhzb y
             WHERE  y.created <= e.created AND y.prodno = e.prodno
           ) AS YBFL_ML
    FROM   exp_yb_ybjs_20240604_copy e
  ) src
  ON dst.ROWID = src.rid
  WHEN MATCHED THEN
    UPDATE
    SET ITEM_ID = src.YBFL_ML;
END update_ybm_for_exp_yb_ybjs;
/
© www.soinside.com 2019 - 2024. All rights reserved.