简体   繁体   中英

Return value from Oracle stored procedure

I have a stored procedure in Oracle. I want that if it updates the record successfully the it returns a return value 1 else 0 . Let me know how can I do it. I am working with Angular, Asp.net, WebAPi and Oracle database.

Thanks in advance. My code is as follows:

CREATE OR REPLACE Procedure NML.AddProductProcedure(V_CRD_COD In varchar2,
                                        V_ITM_COD In varchar2,
                                        V_SRL_NUM In varchar2,
                                        V_UOM_ABR IN varchar2,
                                        V_QTD_RTE IN varchar2,
                                        V_QTY_PRC In varchar2,
                                        V_QTN_NUM IN varchar2,
                                        V_PMT_FLG IN varchar2,
                                        V_TAX_FLG IN varchar2,
                                        V_DLV_FLG IN varchar2)
IS
BEGIN
    UPDATE NML.pgi_00_13 
    SET UOM_ABR = V_UOM_ABR,
        QTD_RTE = V_QTD_RTE,
        QTY_PRC = V_QTY_PRC,
        QTN_NUM = V_QTN_NUM,
        PMT_FLG = V_PMT_FLG,
        TAX_FLG = V_TAX_FLG,
        DLV_FLG = V_DLV_FLG
    WHERE
        CRD_COD = V_CRD_COD 
        AND ITM_COD = V_ITM_COD 
        AND SRL_NUM = V_SRL_NUM;   
END;

You can define an OUT variable from the procedure that gets set to 1 on successful completion or 0 from an EXCEPTION .

CREATE OR REPLACE PROCEDURE nml.addproductprocedure (
     p_crd_cod   IN VARCHAR2,
     p_itm_cod   IN VARCHAR2,
     p_srl_num   IN VARCHAR2,
     p_uom_abr   IN VARCHAR2,
     p_qtd_rte   IN VARCHAR2,
     p_qty_prc   IN VARCHAR2,
     p_qtn_num   IN VARCHAR2,
     p_pmt_flg   IN VARCHAR2,
     p_tax_flg   IN VARCHAR2,
     p_dlv_flg   IN VARCHAR2,
     p_status   OUT NUMBER    --use this variable
)
     IS
BEGIN
     UPDATE nml.pgi_00_13
     SET uom_abr = p_uom_abr,
         qtd_rte = p_qtd_rte,
         qty_prc = p_qty_prc,
         qtn_num = p_qtn_num,
         pmt_flg = p_pmt_flg,
         tax_flg = p_tax_flg,
         dlv_flg = p_dlp_flg
     WHERE crd_cod = p_crd_cod AND itm_cod = p_itm_cod AND srl_num = p_srl_num;
     p_status := 1;

     EXCEPTION
       WHEN OTHERS THEN
     p_status := 0;
END;
/

Your calling block must define OUT parameters as shown here : How to return oracle output parameters from a stored procedure in .NET

What's your definition of "success"? At the moment if your procedure throws an exception it failed otherwise it succeeded. Is that not good enough?

You could extend the signature of the procedure with a parameter which tells you how many records were updated...

CREATE OR REPLACE Procedure NML.AddProductProcedure(
    V_CRD_COD In varchar2,
    V_ITM_COD In varchar2,
    V_SRL_NUM In varchar2,
    V_UOM_ABR IN varchar2,
    V_QTD_RTE IN varchar2,
    V_QTY_PRC In varchar2,
    V_QTN_NUM IN varchar2,
    V_PMT_FLG IN varchar2,
    V_TAX_FLG IN varchar2,
    V_DLV_FLG IN varchar2,
    p_updated_cnt out pls_integer                                           
    )IS
 BEGIN

   Update NML.pgi_00_13 
   set 
       UOM_ABR=V_UOM_ABR,
       QTD_RTE=V_QTD_RTE,
       QTY_PRC=V_QTY_PRC,
       QTN_NUM=V_QTN_NUM,
       PMT_FLG=V_PMT_FLG,
       TAX_FLG=V_TAX_FLG,
       DLV_FLG=V_DLV_FLG
   Where CRD_COD=V_CRD_COD 
   AND ITM_COD=V_ITM_COD 
   AND SRL_NUM=V_SRL_NUM;   

   -- count of rows updated by preceding statement
   p_updated_cnt := sql%rowcount;

 END;

Note that you could instead pass a flag or 1 or 0 as shown in @Kaushik solution but this is an anti-pattern. Because:

  1. It returns success when the statement updates zero rows, which is a technical success but probably not a successful completion from a business perspective.
  2. A calling program can choose to ignore the flag value, whereas it must handle a propagated exception.
  3. If the calling program wants to act when the flag value indicates failure it has no idea why the called procedure failed. Exceptions on the other hand are usually specific enough to support decision making.

Just want retun 1 when it succeded to update the record

Well you could do this:

CREATE OR REPLACE Procedure NML.AddProductProcedure(
    V_CRD_COD In varchar2,
    V_ITM_COD In varchar2,
    V_SRL_NUM In varchar2,
    V_UOM_ABR IN varchar2,
    V_QTD_RTE IN varchar2,
    V_QTY_PRC In varchar2,
    V_QTN_NUM IN varchar2,
    V_PMT_FLG IN varchar2,
    V_TAX_FLG IN varchar2,
    V_DLV_FLG IN varchar2,
    p_updated_flag out pls_integer                                           
    )IS
 BEGIN

   Update NML.pgi_00_13 
   set 
       UOM_ABR=V_UOM_ABR,
       QTD_RTE=V_QTD_RTE,
       QTY_PRC=V_QTY_PRC,
       QTN_NUM=V_QTN_NUM,
       PMT_FLG=V_PMT_FLG,
       TAX_FLG=V_TAX_FLG,
       DLV_FLG=V_DLV_FLG
   Where CRD_COD=V_CRD_COD 
   AND ITM_COD=V_ITM_COD 
   AND SRL_NUM=V_SRL_NUM;   

   if sql%rowcount > 0 then
       p_update_flag := 1;
   else
       p_update_flag := 0;
   end if;

 END;

This will still hurl if there's an exception - which is a good thing - but distinguishes between updating zero records and one record (or more).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM