内部连接两个 table 变量和 return 来自 c# 应用程序中匿名 plsql 块的输出游标
Inner join two table variables and return an output cursor from an anonymous plsql block in a c# application
我有这种情况:
- 我需要将许多联接的结果(15 tables 使用各种过滤器)插入到 table 变量中
var_TB_PROJECT
- 然后将许多其他联接的结果(8 table 具有各种过滤器)插入另一个 table 变量
var_TB_CAMERAS
- 最后,内联这两个table变量
所有这些都在匿名 plsql 块中,因为我无法在源数据库中创建任何对象(无权限)。
这个匿名 plsql 块将在 C# 应用程序中执行,该应用程序将 return 输出游标作为下面的示例代码。
可以吗?
static void TestBlock()
{
string conString = ConfigManager.GetConnectionStringByName("CI_L3");
OracleConnection con = new OracleConnection();
con.ConnectionString = conString;
con.Open();
string cmdtxt = @"
DECLARE
type rectyp is record(TABLE1.FIELD1%TYPE, TABLE2.FIELD1%TYPE, TABLE3.FIELD1%TYPE, ... many fields .... );
type tpTbl is table of rectyp;
var_TB_PROJETO tpTbl;
var_TB_CAMERAS tpTbl;
BEGIN
-- load var_TB_PROJETO
select FIELD1, FIEL2, FIELN .... bulk collect into var_TB_PROJETO
from TABLE1 INNER JOIN TABLE2 ON TABLE1.ID = TABLE2.FID
INNER JOIN TABLE3 ON TABLE2.ID = TABLE3.FID ....
.
.
-- many tables ...
.
.
-- load var_TB_CAMERAS
select FIELD1, FIEL2, FIELN .... bulk collect into var_TB_CAMERAS
from TABLE1 INNER JOIN TABLE2 ON TABLE1.ID = TABLE2.FID
INNER JOIN TABLE3 ON TABLE2.ID = TABLE3.FID ....
.
.
-- many tables ...
.
.
OPEN :1 for SELECT * FROM var_TB_PROJETO INNER JOIN var_TB_CAMERAS
ON var_TB_PROJETO.ID = var_TB_CAMERAS.ID;
END;";
OracleCommand cmd = con.CreateCommand();
cmd.CommandText = cmdtxt;
OracleParameter p1 = cmd.Parameters.Add("refcursor1", OracleDbType.RefCursor);
p1.Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
OracleDataReader dr1 = ((OracleRefCursor)cmd.Parameters[0].Value).GetDataReader();
while (dr1.Read()) {
var st1 = dr1.GetString(0);
}
}
@Goldar:为什么要使用匿名 PL/SQL 块,当它可以作为 SQL 语句
解决时
WITH TB_PROJETO as (
select TABLE1.ID, FIELD1, FIEL2, FIELN
from TABLE1 INNER JOIN TABLE2 ON TABLE1.ID = TABLE2.FID
INNER JOIN TABLE3 ON TABLE2.ID = TABLE3.FID ....
.
.
-- many tables ...
.
.
), TB_CAMERAS as (
select TABLE1.ID, FIELD1, FIEL2, FIELN
from TABLE1 INNER JOIN TABLE2 ON TABLE1.ID = TABLE2.FID
INNER JOIN TABLE3 ON TABLE2.ID = TABLE3.FID ....
.
.
-- many tables ...
.
.
)
SELECT *
FROM TB_PROJETO
INNER JOIN TB_CAMERAS ON TB_PROJETO.ID = TB_CAMERAS.ID;
我有这种情况:
- 我需要将许多联接的结果(15 tables 使用各种过滤器)插入到 table 变量中
var_TB_PROJECT
- 然后将许多其他联接的结果(8 table 具有各种过滤器)插入另一个 table 变量
var_TB_CAMERAS
- 最后,内联这两个table变量
所有这些都在匿名 plsql 块中,因为我无法在源数据库中创建任何对象(无权限)。
这个匿名 plsql 块将在 C# 应用程序中执行,该应用程序将 return 输出游标作为下面的示例代码。
可以吗?
static void TestBlock()
{
string conString = ConfigManager.GetConnectionStringByName("CI_L3");
OracleConnection con = new OracleConnection();
con.ConnectionString = conString;
con.Open();
string cmdtxt = @"
DECLARE
type rectyp is record(TABLE1.FIELD1%TYPE, TABLE2.FIELD1%TYPE, TABLE3.FIELD1%TYPE, ... many fields .... );
type tpTbl is table of rectyp;
var_TB_PROJETO tpTbl;
var_TB_CAMERAS tpTbl;
BEGIN
-- load var_TB_PROJETO
select FIELD1, FIEL2, FIELN .... bulk collect into var_TB_PROJETO
from TABLE1 INNER JOIN TABLE2 ON TABLE1.ID = TABLE2.FID
INNER JOIN TABLE3 ON TABLE2.ID = TABLE3.FID ....
.
.
-- many tables ...
.
.
-- load var_TB_CAMERAS
select FIELD1, FIEL2, FIELN .... bulk collect into var_TB_CAMERAS
from TABLE1 INNER JOIN TABLE2 ON TABLE1.ID = TABLE2.FID
INNER JOIN TABLE3 ON TABLE2.ID = TABLE3.FID ....
.
.
-- many tables ...
.
.
OPEN :1 for SELECT * FROM var_TB_PROJETO INNER JOIN var_TB_CAMERAS
ON var_TB_PROJETO.ID = var_TB_CAMERAS.ID;
END;";
OracleCommand cmd = con.CreateCommand();
cmd.CommandText = cmdtxt;
OracleParameter p1 = cmd.Parameters.Add("refcursor1", OracleDbType.RefCursor);
p1.Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
OracleDataReader dr1 = ((OracleRefCursor)cmd.Parameters[0].Value).GetDataReader();
while (dr1.Read()) {
var st1 = dr1.GetString(0);
}
}
@Goldar:为什么要使用匿名 PL/SQL 块,当它可以作为 SQL 语句
解决时 WITH TB_PROJETO as (
select TABLE1.ID, FIELD1, FIEL2, FIELN
from TABLE1 INNER JOIN TABLE2 ON TABLE1.ID = TABLE2.FID
INNER JOIN TABLE3 ON TABLE2.ID = TABLE3.FID ....
.
.
-- many tables ...
.
.
), TB_CAMERAS as (
select TABLE1.ID, FIELD1, FIEL2, FIELN
from TABLE1 INNER JOIN TABLE2 ON TABLE1.ID = TABLE2.FID
INNER JOIN TABLE3 ON TABLE2.ID = TABLE3.FID ....
.
.
-- many tables ...
.
.
)
SELECT *
FROM TB_PROJETO
INNER JOIN TB_CAMERAS ON TB_PROJETO.ID = TB_CAMERAS.ID;