从代码隐藏调用时,Oracle在存储过程中插入查询不起作用

本文关键字:存储过程 存储 过程中 插入 不起作用 查询 Oracle 隐藏 代码 调用 | 更新日期: 2023-09-27 17:57:29

下面是我的存储过程,它在从SQL编辑器调用时运行良好。但是从代码调用时,只有第一条语句(truncate)有效。插件不起作用。有人能帮我吗?提前谢谢。

SP:

create or replace PROCEDURE GETALARMLIST
(
pEqCode              ESPC_O_ALARM_DATA.EQCODE%TYPE,
pOccMinTime          ESPC_O_ALARM_DATA.OCCTIME%TYPE,
pOccMaxTime          ESPC_O_ALARM_DATA.OCCTIME%TYPE,
displayparam         OUT SYS_REFCURSOR
)
AS 
BEGIN
EXECUTE IMMEDIATE 'TRUNCATE TABLE tempalarmlist';
INSERT INTO tempalarmlist(OCCDATE,FIRSTOCCTIME,COUNT)
SELECT substr(a.OCCTIME, 1, 4) || '/' || substr(a.OCCTIME, 5, 2) || '/' || substr(a.OCCTIME, 7, 2),min(OCCTIME), count(*)
FROM ESPC_O_ALARM_DATA a WHERE EQCODE = pEqCode AND a.OCCTIME > pOccMinTime  AND a.OCCTIME < pOccMaxTime 
GROUP BY substr(a.OCCTIME, 1, 4) || '/' || substr(a.OCCTIME, 5, 2) || '/' || substr(a.OCCTIME, 7, 2)
ORDER BY substr(a.OCCTIME, 1, 4) || '/' || substr(a.OCCTIME, 5, 2) || '/' || substr(a.OCCTIME, 7, 2);

UPDATE tempalarmlist
SET 
ALMID=(select ALMID from ESPC_O_ALARM_DATA where ESPC_O_ALARM_DATA.OCCTIME = tempalarmlist.FIRSTOCCTIME and ESPC_O_ALARM_DATA.EQCODE = pEqCode),
ALMCODE =(select ALMCODE from ESPC_O_ALARM_DATA where ESPC_O_ALARM_DATA.OCCTIME = tempalarmlist.FIRSTOCCTIME and ESPC_O_ALARM_DATA.EQCODE = pEqCode),
ALMTXT= (select ALMTXT from ESPC_O_ALARM_DATA where ESPC_O_ALARM_DATA.OCCTIME = tempalarmlist.FIRSTOCCTIME and ESPC_O_ALARM_DATA.EQCODE = pEqCode);

OPEN displayparam FOR  SELECT  * FROM tempalarmlist;
END GETALARMLIST;

代码:

 public static DataTable GetAlarmList(string datasource, string eqCode, string OccMinTime, string OccMaxTime){
            OracleConnection objConn = new OracleConnection();
            objConn.ConnectionString = System.Configuration.ConfigurationManager.AppSettings[datasource];
            OracleCommand objCmd = new OracleCommand();
            objCmd.Connection = objConn;
            objCmd.CommandType = CommandType.StoredProcedure;
            objCmd.CommandText = "GETALARMLIST";
            objCmd.Parameters.Add("pEqCode", eqCode);
            objCmd.Parameters.Add("pOccMinTime", OccMinTime);
            objCmd.Parameters.Add("pOccMaxTime", OccMaxTime);
            objCmd.Parameters.Add("displayparam", OracleType.Cursor).Direction = ParameterDirection.Output;
            DataTable dtAlarmList = new DataTable();
            try
            {
                objConn.Open();
                OracleDataAdapter oda = new OracleDataAdapter(objCmd);
                oda.Fill(dtAlarmList);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                objConn.Close();
            }
            return dtAlarmList;
        }

从代码隐藏调用时,Oracle在存储过程中插入查询不起作用

您的存储过程和代码都不包含COMMIT操作,所以您的插入只是回滚(表截断不能回滚,所以您可以看到它的效果)。SQL编辑器似乎启用了自动提交选项,但程序的连接参数没有。所以,你有几个选择:

  1. 将显式COMMIT添加到存储过程中
  2. 执行查询后调用objConn.commit()
  3. 在连接上设置autocommit选项(请参阅此处的详细信息)