从后面的代码调用时,存储过程中的Oracle插入查询不起作用

时间:2016-07-19 02:48:39

标签: c# oracle stored-procedures

下面是我的存储过程,它可以在从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;
        }

1 个答案:

答案 0 :(得分:1)

您的存储过程和代码都不包含COMMIT操作,因此您的插入只会回滚(无法回滚表截断,因此无论如何都会看到它的效果)。看起来您的SQL编辑器上有自动提交选项,但您的程序的连接参数没有。所以,你有几个选择:

  1. 向您的存储过程添加显式COMMIT
  2. 执行查询后致电objConn.commit()
  3. 在您的连接上设置autocommit选项(请参阅详情here)。