我想在Oracle的程序中使用全局临时表。为此,我创建了一个全局临时表:
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#应用程序中正确返回这些行?
答案 0 :(得分:1)
我在TEMP_INT_1表中寻找插入行,并试图选择TEMP_TEST表,这就是它无法正常工作的原因。