0

Below is my Stored procedure which works fine while calling from the SQL editor. But when calling from code, only the first statement (truncate) works. The insert does not work. Can anyone please help me. Thanks in advance.

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;

Code:

 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;
        }
3
  • "Does not work" isn't enough information to answer your question. If there's an error coming back when you run it, please mention what it is. Commented Jul 19, 2016 at 2:59
  • Hi Peter, I do not get any error. The SP does not execute the insert statement. Commented Jul 19, 2016 at 3:22
  • Is there anything I should change? Please suggest since I am a newbie to Oracle. Commented Jul 19, 2016 at 3:30

1 Answer 1

1

Neither your stored procedure nor your code contain COMMIT operation, so your insertion just rolls back (table truncation cannot be rolled back, so you see its effect anyway). It looks like your SQL editor has autocommit option on, but your program's connection parameters has not. So, you have several options:

  1. Add explicit COMMIT to your stored procedure
  2. Call objConn.commit() after executing your query.
  3. Set autocommit option on your connection (see details here).
Sign up to request clarification or add additional context in comments.

2 Comments

Yes. Adding COMMIT statement solved my problem. Thanks!
I'm glad it helped you. You can mark the answer as correct for other people to know they can rely on it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.