打开,获取,进入多个变量

时间:2015-05-12 14:44:41

标签: plsql

我试图在不使用循环的情况下从游标中获取2个变量。

CREATE OR REPLACE PROCEDURE NAK.SET_ORDERS(P_ORDER_ID NAK.ORDER_ID%TYPE)

CURSOR C_GET_ORDER_NO IS
  SELECT O.ORDER_ID, O.ORDER_MAL FROM NAK.ORDERS O WHERE O.ORDER_ID = P_ORDER_ID;

BEGIN

  V_ORDER_SEQ := NULL;
  V_ORDER_MAL := NULL;
  OPEN C_GET_ORDER_NO;
  FETCH C_GET_ORDER_NO VALUES(O.ORDER_ID, O.ORDER_MAL)
    INTO (V_ORDER_ID, V_ORDER_MAL);
  CLOSE C_GET_ORDER_NO;

END;

1 个答案:

答案 0 :(得分:0)

你真的需要一个明确的游标吗?你可以这样做:

    CREATE OR REPLACE PROCEDURE NAK.SET_ORDERS(P_ORDER_ID IN NAK.ORDER_ID%TYPE)

      V_ORDER_SEQ := NULL;
      V_ORDER_MAL := NULL;

    BEGIN

      SELECT O.ORDER_ID, 
             O.ORDER_MAL 
        INTO V_ORDER_SEQ,
             V_ORDER_MAL  
        FROM NAK.ORDERS O 
       WHERE O.ORDER_ID = P_ORDER_ID;

   EXCEPTION
       WHEN NO_DATA_FOUND THEN
            dbms_output.put_line("No record found");
       WHEN TOO_MANY_ROWS THEN
            dbms_output.put_line("More than one record found");
       WHEN OTHER THEN
            dbms_output.put_line("Other problem happend");
    END;

重要:如果查询没有返回一条记录,此过程将返回异常。 (ORA-01403:未找到数据或ORA-00913:值太多)

或者你应该能够做出类似的事情:

CREATE OR REPLACE PROCEDURE NAK.SET_ORDERS(P_ORDER_ID NAK.ORDER_ID%TYPE)

CURSOR C_GET_ORDER_NO IS
  SELECT O.ORDER_ID, 
         O.ORDER_MAL 
    FROM NAK.ORDERS O 
   WHERE O.ORDER_ID = P_ORDER_ID;

BEGIN
  V_ORDER_SEQ := NULL;
  V_ORDER_MAL := NULL;

  OPEN C_GET_ORDER_NO;
  FETCH C_GET_ORDER_NO INTO V_ORDER_ID, V_ORDER_MAL;
  CLOSE C_GET_ORDER_NO;

END;