我希望能够在ODP.Net中以select语句结束运行多个查询。我正在清理,更新,然后从表中选择。
举个例子:
BEGIN
DELETE FROM StagingTable;
INSERT INTO StagingTable (Col1,Col2,Col3)
SELECT Value1,Value2,Value3
FROM MainTable;
UPDATE StagingTable
SET Col1 = FUNCTION(Col1);
END;
/
SELECT * FROM StagingTable;
这适用于SQL Developer但不适用于ODP.Net。我收到这个错误:
ORA-06550:第26行第1栏: PLS-00103:遇到符号“/”
但如果我删除/我得到:
ORA-06550:第27行第1栏: PLS-00103:遇到符号“SELECT”
我想做甚么可能吗?我不想创建包并开始创建类型等。理想情况下,我想通过.Net来做到这一点。
答案 0 :(得分:0)
与带有t-sql的SQL Server不同,oracle有两个上下文,SQL Plus和PL / SQL(开始/结束是一个匿名的plsql块,其中选择外部是SQL Plus)。在plsql块启动后,我还没有想出一种从SQL Plus查询返回集合的方法。
但是,您当然可以在plsql块中执行所有操作并返回引用游标:
OracleCommand cmd = new OracleCommand(
@"
declare
localUseVar varchar(50);
begin
select dummy into localUseVar from dual;
open :refcursor for
select dummy, localUseVar FROM DUAL
where :fakeparam=:fakeparam;
end;
");
cmd.BindByName = true;
cmd.Parameters.Add("fakeparam", "fake");
cmd.Parameters.Add("refcursor", OracleDbType.RefCursor).Direction = System.Data.ParameterDirection.Output;
using (cmd.Connection = new OracleConnection("DATA SOURCE=localhost:1521/xe; PERSIST SECURITY INFO = True; USER ID = TEMPLATE; Password=password"))
{
cmd.Connection.Open();
var reader = cmd.ExecuteReader();
Assert.IsTrue(reader.Read());
Assert.AreEqual(reader[0], reader[1]);
}
更多信息: http://www.brothersincode.com/post/executing-SQL-Plus-Batches-from-Net.aspx
您的具体案例如下:
OracleCommand cmd = new OracleCommand(
@"
begin
DELETE FROM StagingTable;
INSERT INTO StagingTable (Col1,Col2,Col3)
SELECT Value1,Value2,Value3
FROM MainTable;
UPDATE StagingTable
SET Col1 = FUNCTION(Col1);
open :refcursor for SELECT * FROM StagingTable;
end;
");
cmd.BindByName = true;
cmd.Parameters.Add("refcursor", OracleDbType.RefCursor).Direction = System.Data.ParameterDirection.Output;
using (cmd.Connection = new OracleConnection("DATA SOURCE=localhost:1521/xe; PERSIST SECURITY INFO = True; USER ID = TEMPLATE; Password=password"))
{
cmd.Connection.Open();
var reader = cmd.ExecuteReader();
}