我有一个程序我需要捕获错误按摩如果count = 0然后获取记录否则如果计数> 0错误表中的捕获错误按摩

时间:2015-12-18 17:21:19

标签: oracle plsql

PROCEDURE p_get_empdetails(pcu_scheme_info OUT SYS_REFCURSOR)
IS  
BEGIN


    -- To Select scheme_ref,scheme_cat and quote_ref
    OPEN pcu_scheme_info FOR
        SELECT emp_ref
              ,emp_cat
              ,emp_quote_ref
        FROM emp_reset_id e
        WHERE estimated_quote_expiry_date <= lpcd_system_date
        AND q.quote_ref IN (SELECT DISTINCT qrc.quote_ref
                            FROM nb005 nb, 
                                 emp_reset_id e
                            WHERE nb.qteref = e.emp_quote_ref
                            AND nb.pcsstg <> 51);                
END p_get_scheme_details;

我需要使用以下查询来获取记录计数= 0或计数&gt; 0

SELECT COUNT(*)
FROM nb005 nb JOIN emp_reset_id e ON nb.qteref = qrc.emp_quote_ref
WHERE nb.pcsstg=51;

2 个答案:

答案 0 :(得分:0)

如果您希望在计数&gt;时完成程序而没有任何错误0并成功结束:

CREATE OR REPLACE PROCEDURE p_get_empdetails(pcu_scheme_info OUT SYS_REFCURSOR)
IS
number v_count := 0;
BEGIN

  SELECT COUNT(*) into 
  FROM nb005 nb JOIN emp_reset_id e ON nb.qteref = qrc.emp_quote_ref
  WHERE nb.pcsstg=51;

  IF v_count > 0 THEN

  --This is where you capture error message in error table.

  ELSE
  -- To Select scheme_ref,scheme_cat and quote_ref
  OPEN pcu_scheme_info FOR
      SELECT emp_ref
            ,emp_cat
            ,emp_quote_ref
      FROM emp_reset_id e
      WHERE estimated_quote_expiry_date <= lpcd_system_date
      AND q.quote_ref IN (SELECT DISTINCT qrc.quote_ref
                          FROM nb005 nb, 
                               emp_reset_id e
                          WHERE nb.qteref = e.emp_quote_ref
                          AND nb.pcsstg <> 51);                
  END IF;
END p_get_scheme_details;

如果您希望在计数为&gt;时程序失败? 0,但仍想将记录插入错误表(我的首选方式):

CREATE OR PROCEDURE p_get_empdetails(pcu_scheme_info OUT SYS_REFCURSOR)
IS
v_count number := 0;
v_excep exception;
v_err_msg varchar2(1000) := '';
BEGIN

    SELECT COUNT(*) into 
    FROM nb005 nb JOIN emp_reset_id e ON nb.qteref = qrc.emp_quote_ref
    WHERE nb.pcsstg=51;

    IF v_count > 0 THEN

    raise v_excep;

    ELSE
    -- To Select scheme_ref,scheme_cat and quote_ref
    OPEN pcu_scheme_info FOR
        SELECT emp_ref
              ,emp_cat
              ,emp_quote_ref
        FROM emp_reset_id e
        WHERE estimated_quote_expiry_date <= lpcd_system_date
        AND q.quote_ref IN (SELECT DISTINCT qrc.quote_ref
                            FROM nb005 nb, 
                                 emp_reset_id e
                            WHERE nb.qteref = e.emp_quote_ref
                            AND nb.pcsstg <> 51);                
    END IF;

    EXCEPTION
    WHEN v_excep THEN
      v_err_msg := 'v_count was greater than 0. '||CHR(10);
      --This is where you capture error message in error table.
      RAISE_APPLICATION_ERROR(-20555,v_err_msg||'Error: ' || to_char ( SQLCODE ) || ': ' || SQLERRM || '. Backtrace Result: ' || dbms_utility.format_error_backtrace);
    WHEN OTHERS THEN
      RAISE_APPLICATION_ERROR(-20502,v_err_msg||'Error: ' || to_char ( SQLCODE ) || ': ' || SQLERRM || '. Backtrace Result: ' || dbms_utility.format_error_backtrace);

END p_get_scheme_details;

答案 1 :(得分:0)

您还可以在正在使用的程序中声明游标。
然后使用记录从游标中声明的查询中获取结果。
例如,您甚至可以循环遍历所有行并进行处理。
例如:

DECLARE 
    CURSOR CursorName IS 
        SELECT  COUNT(*) ColumnOne 
        FROM    TableA 
        WHERE   Name = 'Me';

    RecordNumber CursorName%ROWTYPE;

BEGIN 
    -- Fetch the records from the cursor. 
    OPEN CursorName; 
    LOOP
        -- Line by line 
        FETCH CursorName INTO RecordNumber;

        -- Do something with the record. 
        IF RecordNumber.ColumnOne = 0 THEN
            -- Do something with the record. 
        END IF;

        EXIT WHEN CursorName %NOTFOUND;
    END LOOP;
    CLOSE CursorName;

END;
/

我没有手动编写数据库来测试我的代码,但我认为这应该可行。