2017-08-15 110 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表,为什么它不工作。