我可以将表作为输出参数的存储过程编写为Oracle中的单个查询吗?

时间:2015-03-24 03:46:21

标签: c# .net stored-procedures oracle-sqldeveloper

我有一个C#方法,它接受一个SQL字符串语句并将数据保存为xml格式。

public XmlDocument GetDBRequestXml(String sql)
{
}

我有一个带输出参数的存储过程作为表。有没有办法将此存储过程作为上述C#方法中的可执行单个SQL语句传递?有人可以帮我这个!!!

create or replace PACKAGE BODY EMPLOYEE_DETAILS AS

    PROCEDURE GET_EMPLOYEES(
        EMP_DEPT_ID EMPLOYEES.DEPARTMENT_ID%TYPE, 
        EMP_SALARY employees.salary%TYPE,
        TBL_EMPLOYEES OUT TABLE_EMPLOYEES)
    IS
        LC_SELECT SYS_REFCURSOR;
        LR_DETAILS DETAILS;
        TBL_EMPLOYEE EMPLOYEE_DETAILS.TABLE_EMPLOYEES := EMPLOYEE_DETAILS.TABLE_EMPLOYEES();

    BEGIN
        OPEN LC_SELECT FOR 
            SELECT EMPLOYEE_ID, FIRST_NAME, LAST_NAME 
            FROM EMPLOYEES
            WHERE DEPARTMENT_ID = EMP_DEPT_ID AND 
                  EMPLOYEES.SALARY > EMP_SALARY;

        LOOP 
            FETCH LC_SELECT INTO LR_DETAILS;
            EXIT WHEN LC_SELECT%NOTFOUND;

            IF LR_DETAILS.EMPLOYEE_ID > 114 THEN
                TBL_EMPLOYEE.extend();
                TBL_EMPLOYEE(TBL_EMPLOYEE.count()) := LR_DETAILS;
            END IF;
        END LOOP;
        CLOSE LC_SELECT;
        TBL_EMPLOYEES := TBL_EMPLOYEE;

    END GET_EMPLOYEES;
END EMPLOYEE_DETAILS; 

1 个答案:

答案 0 :(得分:1)

今天我在这方面跑得很好。我不知道你是否还需要它,但这很好理解。您可以从存储过程输出:

CREATE OR REPLACE PACKAGE Eidmadm.TestPkg AS
  TYPE stringTbl IS TABLE OF varchar2(250) INDEX BY BINARY_INTEGER;

  PROCEDURE TestProc (p_strings out stringTbl );
END;

另一张表,如下所示,无效

TYPE numTbl IS TABLE OF varchar2(100);

c#代码是:

OracleParameter p2 = new OracleParameter(":p_strings", OracleDbType.Varchar2, ParameterDirection.Output);
p2.CollectionType = OracleCollectionType.PLSQLAssociativeArray;
p2.Size = 100; // allocate enough extra space to retrieve expected result
// assign amount of space for each member of returning array
p2.ArrayBindSize = Enumerable.Repeat(250, 100).ToArray(); 

cmd.Parameters.Add(p2);
cmd.ExecuteNonQuery();

// And this is how you retrieve values
OracleString[] oraStrings = (OracleString[])p2.Value;
string[] myP2Values = new string[oraStrings.Length];

for (int i = 0; i < oraNumbers.Length; i++)
    myP2Values[i] = oraStrings[i].Value;

**但最重要的是:**

When you fill your pl/sql table, it needs to start from something larger than `0`, and preferably from `1`. Because and also - if you have index with skipped numbers, i.e. `2,4,6,8`, all those spaces will be part of returning `oracle array` and there will be `oracle null` in them. You would need to check for `null` in your loop
if !oraStrings[i].IsNull {....} 
else {....}

享受!