如何在动态查询中使用批量收集?

时间:2015-02-24 16:21:38

标签: oracle plsql oracle11g dynamic-queries

我的代码段目前使用以下代码将数据提取到对象中:

SELECT ORDR_LIST(ORDER_NUMBER,ORDER_TYPE,ORDER_DATE,CLIENT_ID,CLIENT_NAME) BULK COLLECT INTO P_RESULTSET
            FROM
              (SELECT ORDER_NUMBER,
                        ORDER_TYPE,
                        ORDER_DATE,
                        CLIENT_ID,
                        CLIENT_NAME
                      FROM
                        (SELECT ORDER_NUMBER,
                          ORDER_TYPE,
                          TO_CHAR(ORDER_CREATED_DATE,'YYYY-MM-DD HH:MI:SS am') AS ORDER_DATE,
                          CUSTOMER_FIRST_NAME
                          ||' '
                          ||CUSTOMER_SURNAME AS C_NAME,
                          CLIENT_ID,
                          CLIENT_NAME
                        FROM CONNECT_CUSTOMER_TRANS
                        WHERE ORDER_TYPE              =P_ORDER_TYPE
                        AND lower(CUSTOMER_SURNAME)   =lower(P_LAST_NAME)
                        AND lower(CUSTOMER_FIRST_NAME)=lower(P_FIRST_NAME)
                        AND lower(CLIENT_ID)          =lower(P_CLIENT_ID)
                        AND ORDER_CREATED_DATE BETWEEN P_FROM_DT AND P_TO_DT
  )
 WHERE C_NAME=I.CUS_NAME);

现在我已经将select语句动态生成为变量var1。但是我无法在动态部分实现它。

代码应如下所示:

SELECT ORDR_LIST(ORDER_NUMBER,ORDER_TYPE,ORDER_DATE,CLIENT_ID,CLIENT_NAME) BULK COLLECT INTO P_RESULTSET
            FROM var1

将由执行立即执行。

1 个答案:

答案 0 :(得分:6)

将会是这样的:

execute immediate 
'SELECT ORDR_LIST(ORDER_NUMBER,ORDER_TYPE,ORDER_DATE,CLIENT_ID,CLIENT_NAME) 
from ('||var1||')' BULK COLLECT INTO P_RESULTSET;