2017-08-15 112 views
1

我想在Oracle中的過程中使用全局臨時表。爲了實現這個目標,我創建了一個全局臨時表:在C#中使用存儲過程中的全局臨時表

CREATE GLOBAL TEMPORARY TABLE temp_test 
(id int) 
ON COMMIT PRESERVE ROWS; 

而且我已經創建了一個過程太:

CREATE OR REPLACE PROCEDURE PROC_TEST (p_recordset OUT SYS_REFCURSOR) AS 
BEGIN 
OPEN p_recordset FOR 
SELECT * FROM temp_test; 
EXCEPTION 
WHEN NO_DATA_FOUND THEN 
    NULL; 
    WHEN OTHERS THEN 
    -- Consider logging the error and then re-raise 
    RAISE;  
END PROC_TEST; 

當我執行我的臨時表中插入行的程序,它工作正常:

INSERT INTO temp_test (id) values(1); 
INSERT INTO temp_test (id) values(2); 
INSERT INTO temp_test (id) values(3); 
INSERT INTO temp_test (id) values(4); 
INSERT INTO temp_test (id) values(5); 
INSERT INTO temp_test (id) values(6); 
INSERT INTO temp_test (id) values(7); 
INSERT INTO temp_test (id) values(8); 
INSERT INTO temp_test (id) values(9); 
INSERT INTO temp_test (id) values(10); 
INSERT INTO temp_test (id) values(11); 

var c refcursor; 
execute proc_test(:c); 
print c; 

但是,當我在C#應用程序運行它,它不會在此過程中返回任何行,通過這個代碼:

using (OracleConnection connection = new OracleConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MyContext"].ToString())) 
      { 
       connection.Open(); 
       OracleCommand command = connection.CreateCommand(); 

       command.CommandText = "DELETE FROM temp_test"; 
       command.ExecuteNonQuery(); 

       for (int i = 0; i < 10; i++) 
       { 
        command.CommandText = string.Format("INSERT INTO TEMP_INT_1(ID_INT) VALUES ({0})", i); 
        command.ExecuteNonQuery(); 
       } 

       command.CommandText = "PROC_TEST"; 
       command.CommandType = CommandType.StoredProcedure; 

       command.Parameters.Add(new OracleParameter("p_recordset", OracleType.Cursor)).Direction = ParameterDirection.Output; 

       OracleDataAdapter adapter = new OracleDataAdapter(command); 
       DataSet ds = new DataSet(); 
       adapter.Fill(ds); 



       connection.Close(); 
      } 

我該怎麼做才能在我的C#應用​​程序中正確返回這些行?

+2

你有INSERT INTO TEMP_INT_1,不應該是INSERT INTO TEMP_TEST嗎? – tbone

+0

我已經改變它插入到temp_test,它的工作,謝謝 – William

回答

1

我正在尋找在TEMP_INT_1表中插入行,並試圖選擇TEMP_TEST表,爲什麼它不工作。