0

我有一个返回引用游标的 Oracle 存储过程。我想在返回光标之前打开光标以检查计数并在需要时抛出异常,但是我在语法和我应该如何做这件事上遇到了问题。

      V_ASN_COUNT           NUMBER;

      OPEN O_CURSOR FOR            
         SELECT  column1, -- a bunch of columns
                 column2,          
                 COUNT(DISTINCT SI.ASN_NO) OVER (PARTITION BY SI.ASN_NO) AS ASN_COUNT   
           FROM AN_ORDER_INFO OI, AN_SHIPMENT_INFO SI
          WHERE -- a bunch of criteria    

      OPEN O_CURSOR;
          LOOP
            FETCH ASN_COUNT INTO V_ASN_COUNT;
          END LOOP;
      CLOSE O_CURSOR;  

      IF(V_ASN_COUNT > 1) THEN
        RAISE MULTIPLE_ASNS;
      END IF;  
4

3 回答 3

2

我认为你可以这样做:

    curid NUMBER;
    desctab DBMS_SQL.DESC_TAB;
    colcnt NUMBER; -- total number of columns
    res NUMBER;
    V_ASN_COUNT NUMBER;
BEGIN

    OPEN O_CURSOR FOR            
    SELECT 
        column1, -- a bunch of columns
        column2,
        ...
        COUNT(DISTINCT SI.ASN_NO) OVER (PARTITION BY SI.ASN_NO) AS ASN_COUNT                                 
    FROM AN_ORDER_INFO OI, AN_SHIPMENT_INFO SI
    WHERE -- a bunch of criteria    

    curid := DBMS_SQL.TO_CURSOR_NUMBER (O_CURSOR);

    DBMS_SQL.DESCRIBE_COLUMNS(curid, colcnt, desctab);
    -- "ASN_COUNT" is the last column, i. e. "colcnt" refers to column number of "ASN_COUNT"
    -- or set colcnt directly, e.g. colcnt := 12;

    FOR i IN 1..colcnt LOOP
        IF desctab(i).col_type = 2 THEN
            DBMS_SQL.DEFINE_COLUMN(curid, i, V_ASN_COUNT);
        ELSIF desctab(i).col_type = 12 THEN
            DBMS_SQL.DEFINE_COLUMN(curid, i, datevar);
            .......
        ELSE
            DBMS_SQL.DEFINE_COLUMN(curid, i, namevar, 25);
        END IF;         
    END LOOP;
    -- I do not know if this loop is needed, perhaps you can simply do 
    -- DBMS_SQL.DEFINE_COLUMN(curid, colcnt, V_ASN_COUNT);
    -- for a single column

    res := DBMS_SQL.FETCH_ROWS(curid); -- Fetch only the first row, no loop required
    DBMS_SQL.COLUMN_VALUE(curid, colcnt, V_ASN_COUNT); -- Loop over all column not required, you just like to get the last column

    IF V_ASN_COUNT > 1 THEN
      RAISE MULTIPLE_ASNS;
    END IF;
    DBMS_SQL.CLOSE_CURSOR(curid);

有关更多详细信息,请查看 Oracle 文档:DBMS_SQL.TO_CURSOR_NUMBER函数。

但是,打开/倒带光标的问题仍然存在!

于 2014-10-15T18:13:47.343 回答
0

继上一个问题之后,如果您想多次打开同一个游标以对其进行计数,您可以执行以下操作:

CREATE OR REPLACE PROCEDURE YOUR_PROC(O_CURSOR OUT SYS_REFCURSOR) is
  ASN_NO NUMBER; -- have to define all columns the cursor returns

  V_CHECK_ASN_NO NUMBER;

  -- local function to generate the cursor, to avoid repeating the text
  -- or using dynamic SQL
  FUNCTION GET_CURSOR RETURN SYS_REFCURSOR IS
    V_CURSOR SYS_REFCURSOR;
  BEGIN
    OPEN V_CURSOR FOR
      SELECT *
      FROM AN_ORDER_INFO OI, AN_SHIPMENT_INFO SI
      -- where bunch of stuff
    RETURN V_CURSOR;
  END;
BEGIN
  -- open the cursor for your check; might be better to have a local
  -- variable for this rather than touching the OUT parameter this early
  O_CURSOR := GET_CURSOR;
  LOOP
    FETCH O_CURSOR INTO ASN_NO; -- and all other columns!
    EXIT WHEN O_CURSOR%NOTFOUND;
    IF V_CHECK_ASN_NO IS NOT NULL AND V_CHECK_ASN_NO != ASN_NO THEN
      -- means we have two distinct values
      CLOSE O_CURSOR;
      RAISE MULTIPLE_ASNS;
    END IF;
    V_CHECK_ASN_NO := ASN_NO;
  END LOOP;
  -- close the check version of the cursor
  CLOSE O_CURSOR;

  -- re-open the cursor for the caller
  O_CURSOR := GET_CURSOR;
END YOUR_PROC;

您可以使用动态 SQL 使用相同的 SQL 字符串打开游标两次,但此版本使用本地函数使游标 SQL 静态化(因此在编译时解析)。

游标被执行两次,并且至少从第一次执行中获取了一些行(如果没有重复,则获取所有行;如果有重复,则可能不会获取所有行)。调用者得到一个包含所有行的新结果集。

于 2014-10-15T17:22:57.893 回答
0

如何确保第一行可用于验证?

此代码只会打开一次游标 - 没有并发问题。游标的前两行都代表预期结果集的第一行 - 获取第一个副本进行验证,如果验证成功则返回其余副本。

不过,您仍然必须获取所有列。

  V_ASN_COUNT           NUMBER;

  OPEN O_CURSOR FOR            
     WITH qry AS ( SELECT  column1, -- a bunch of columns
                           column2,          
                           COUNT(DISTINCT SI.ASN_NO) OVER (PARTITION BY SI.ASN_NO) AS ASN_COUNT   
                   FROM AN_ORDER_INFO OI, AN_SHIPMENT_INFO SI
                   WHERE -- a bunch of criteria 
                   )
     SELECT * 
     FROM   qry 
     WHERE rownum = 1
     UNION ALL
     SELECT *
     FROM   qry;

  -- Consume the expendable first row.
  FETCH O_CURSOR INTO V_ASN_COUNT; -- and all the other columns!

  IF(V_ASN_COUNT > 1) THEN
    CLOSE O_CURSOR;
    RAISE MULTIPLE_ASNS;
  END IF;
于 2014-10-15T19:03:02.647 回答