Return value from Oracle stored procedure
我在 Oracle 中有一个存储过程。我希望如果它成功更新记录,它返回一个返回值
提前致谢。我的代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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; |
您可以从过程中定义一个
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | 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; / |
您的调用块必须定义 OUT 参数,如下所示:How to return oracle output parameters from a stored procedure in .NET
您对"成功"的定义是什么?目前,如果您的过程引发异常,则失败,否则成功。这还不够好吗?
您可以使用一个参数来扩展过程的签名,该参数告诉您更新了多少记录...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | 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; |
请注意,您可以改为传递标志或 1 或 0,如 @Kaushik 解决方案中所示,但这是一种反模式。因为:
Just want retun 1 when it succeded to update the record
你可以这样做:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | 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; |
如果出现异常,这仍然会抛出 - 这是一件好事 - 但会区分更新零记录和一条记录(或更多)。