如何在存储过程Oracle中引发异常以锁定表

时间:2019-01-18 13:41:19

标签: oracle exception stored-procedures

我在Oracle中有一个存储过程,用于获取和显示数据。但是我想在其中实现更多功能。

我想要的是,我想抛出一个异常,然后将该异常登录到Oracle表中

样本存储过程

CREATE OR REPLACE PROCEDURE GET_FCA_GISDATA(
                                            P_GRPNAME    IN NVARCHAR2, 
                                            TBLDATA_APP OUT SYS_REFCURSOR 
                                           ) AS
BEGIN
    OPEN TBLDATA_APP FOR

      SELECT IP.ID,
             IP.SAP_ID,
             IP.ID_OD_COUNTCHANGE,
             IP.ID_OD_CHANGEDDATE,
             IP.RRH_COUNTCHANGE,
             IP.RRH_CHANGEDDATE,
             IP.TENANCY_COUNTCHANGE,
             IP.TENANCY_CHANGEDDATE,
             ST.STATUS,
             IP.RFE1_DATE_BAND,
             IP.RFS_DATE_BAND,
             IP.CREATED_BY
        FROM TBL_IPCOLO_MAST_INFO IP
        LEFT JOIN TBL_IPCOLO_STATUS ST
          ON IP.FCA_STATUS = ST.ID
       WHERE UMS_GRP_TO_NAME = P_GRPNAME
         AND ST.ISACTIVE = 1
       ORDER BY 12 DESC;

    COMMIT;

  EXCEPTION
    WHEN OTHERS THEN
      ROLLBACK;  
END GET_FCA_GISDATA;

请建议如何引发异常。

2 个答案:

答案 0 :(得分:0)

如何提出自己的例外情况?像这样:

     using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew,
                new TransactionOptions { IsolationLevel = IsolationLevel.ReadUncommitted, Timeout = new TimeSpan(1, 0, 0) }))
                {
                    try
                    {
                        segregationAssignment = new SegregationAssignment(dbContext).Assign(rmaU);

                        dbContext.SaveChanges();
                        scope.Complete();
                        scope.Dispose();
                    }
                    catch (DbUpdateException eb)
                    {
                        scope.Dispose();
                        return RedirectToAction("Details", details).WithErrorMessage(string.Format(Validations.not_possible_to_operation,
                            details.IsConfirmSegregate ? Buttons.segregate.ToLower() : Buttons.refuse.ToLower(), Models.rma_u));
                    }
                    catch (Exception ex)
                    {
                        scope.Dispose();
                        return RedirectToAction("Details", details).WithErrorMessage(string.Format(Validations.not_possible_to_operation,
                            details.IsConfirmSegregate ? Buttons.segregate.ToLower() : Buttons.refuse.ToLower(), Models.rma_u));
                    }
                }

您无需将其存储在屏幕上,而是将其存储在某些表格中。

答案 1 :(得分:0)

a SQL Select statement like the below one might be added, provided the related synonyms of dynamic performance views' select privileges are granted to your current user :

Select count(1)
  Into v_locked
  From all_objects o
  Join v$locked_object l on o.object_id = l.object_id
  Join v$session s on l.session_id = s.sid
  Join v$session_wait w on s.sid = w.sid
 Where o.object_name = 'TBL_IPCOLO_MAST_INFO'
   and s.status = 'ACTIVE'
   and lower(w.event) like '%enq%contention%' --> "enq: TX - row lock contention"

before invoking the cursor.

If v_locked variable's value is greater than zero then the related table has a row lock contention. That would be alerted and logged in a simple table if v_locked > 0.