1

它可能会回应谁,我们想在 11g r2 Oracle DBMS 上使用带有 PIVOT 选项的 SELECT 函数。我们的查询是这样的:

select * from
(SELECT o.ship_to_customer_no, ol.item_no,ol.amount
  FROM t_order o, t_order_line ol
 WHERE o.NO = ol.order_no and ol.item_no in (select distinct(item_no) from t_order_line))
  pivot --xml 
(  SUM(amount) FOR item_no IN ( select distinct(item_no) as item_no_ from t_order_line));

可以看出,XML 被注释掉了,如果作为 PIVOT XML 运行,它会以 XML 格式给出正确的输出,但是我们需要将数据作为未格式化的数据透视数据,但是这句话会抛出错误: ORA-00936: missing expression

欢迎任何决议或想法,

此致

-------------脏但工作过程如下------------------------

格林威治标准时间 17.01.2011 16:39 更新了程序:

 PROCEDURE pr_pivot_item_by_ship_to (
      p_location_code             IN       t_customer.location_code%TYPE,
      p_customer_price_group      IN       t_customer.customer_price_group%TYPE,
      p_shipment_date             IN       t_order.shipment_date%TYPE,
      p_fasdat_status             IN       t_order.fasdat_status%TYPE,
      p_order_type                IN       t_order.order_type%TYPE,
      cur_pivot_item_by_ship_to   OUT      sys_refcursor
   )
   IS
      v_sql            VARCHAR2 (15000);
      v_pivot_items    VARCHAR2 (15000) := '';
      v_query_items    VARCHAR2 (15000) := '';
      v_pivot_orders   VARCHAR2 (15000) := '';
      v_continue       INT              := 0;
   BEGIN
      /*GET ORDER NUMBERS*/
      FOR cur_order_loop IN (SELECT DISTINCT (o.NO) AS order_no
                                        FROM t_order o,
                                             vw_customer_with_ship_to_info wwc
                                       WHERE wwc.customer_price_group =
                                                        p_customer_price_group
                                         AND wwc.location_code =
                                                               p_location_code
                                         AND o.shipment_date = p_shipment_date
                                         AND o.fasdat_status = p_fasdat_status
                                         AND o.order_type = p_order_type
                                         AND wwc.NO = o.customer_no)
      LOOP
         v_pivot_orders :=
                ''',''' || TO_CHAR (cur_order_loop.order_no)
                || v_pivot_orders;
         v_pivot_orders := LTRIM (v_pivot_orders, ''',''');
      END LOOP;

      /*USE ORDER NUMBERS TO FIND ITEMS TO PIVOT BY SHIPMENT PLACE*/
      FOR cur_loop IN
         (SELECT DISTINCT (ol.item_no) AS item_no,
                          REPLACE
                             (REPLACE (SUBSTR (i.description, 1, 20), '''',
                                       ''),
                              '"',
                              ' inch'
                             ) AS description
                     FROM t_order_line ol, t_item i
                    WHERE ol.item_no = i.NO
                      AND ol.order_no IN (
                             SELECT DISTINCT (o.NO) AS order_no
                                        FROM t_order o,
                                             vw_customer_with_ship_to_info wwc
                                       WHERE wwc.customer_price_group =
                                                        p_customer_price_group
                                         AND wwc.location_code =
                                                               p_location_code
                                         AND o.shipment_date = p_shipment_date
                                         AND o.fasdat_status = p_fasdat_status
                                         AND o.order_type = p_order_type
                                         AND wwc.NO = o.customer_no))
      LOOP
         v_query_items := ',''' || cur_loop.item_no || '''' || v_query_items;
         v_pivot_items :=
               ','''
            || cur_loop.item_no
            || ''' as "ad_'
            || cur_loop.description
            || '"'
            || v_pivot_items;
      END LOOP;

      v_query_items := LTRIM (v_query_items, ',');
      v_pivot_items := LTRIM (v_pivot_items, ',');
      v_sql :=
            'select * from
       (SELECT wwc.ship_to_customer_no||''-''|| wwc.ship_to_customer_name as "Müst. Adi ('
         || p_order_type
         || ')", ol.item_no,ol.amount
         FROM t_order o, t_order_line ol,vw_customer_with_ship_to_info wwc
        WHERE o.NO = ol.order_no
        and wwc.no = o.customer_no
        and ol.order_no in (
        (SELECT DISTINCT (o.NO) AS order_no
                                            FROM t_order o,
                                                 vw_customer_with_ship_to_info wwc
                                           WHERE wwc.customer_price_group ='''
         || p_customer_price_group
         || '''
                                             AND wwc.location_code =
                                                                   '''
         || p_location_code
         || '''
                                            AND o.shipment_date = '''
         || p_shipment_date
         || '''

                                             AND o.fasdat_status = '
         || p_fasdat_status
         || '
                                             AND o.order_type = '''
         || p_order_type
         || '''
                                             AND wwc.NO = o.customer_no)
         )
        and OL.ITEM_NO in ('
         || v_query_items
         || ')
        )
         pivot
       (  SUM(amount) FOR item_no IN ('
         || v_query_items                                      --v_pivot_items
         || '))';

      --DBMS_OUTPUT.put_line ('TSQL ' || v_sql);
       --      OPEN cur_pivot_item_by_ship_to FOR
        --         SELECT v_sql
        --           FROM DUAL;
      BEGIN
         OPEN cur_pivot_item_by_ship_to FOR v_sql;
      EXCEPTION
         WHEN NO_DATA_FOUND
         THEN
            NULL;
         WHEN OTHERS
         THEN
            IF SQLCODE = -936
            THEN
               NULL;
            ELSE
               pck_helper.pr_log_error
                                      (SQLCODE,
                                          'p_shipment_date:'
                                       || p_shipment_date
                                       || ','
                                       || 'cur_pivot_item_by_ship_to err. :'
                                       || SQLERRM,
                                       'pr_pivot_item_by_ship_to'
                                      );
            END IF;
      END;
   EXCEPTION
      WHEN NO_DATA_FOUND
      THEN
         NULL;
      WHEN OTHERS
      THEN
         pck_helper.pr_log_error (SQLCODE,
                                     'p_shipment_date:'
                                  || p_shipment_date
                                  || ','
                                  || SQLERRM,
                             'pr_pivot_item_by_ship_to'
                                 );
   END pr_pivot_item_by_ship_to;
END pkg_report;
4

2 回答 2

0

我现在没有可用的 Oracle 11 实例(PIVOT在 Oracle 11 中是新实例),所以我只能推测。我的猜测是,--在. 您是否尝试过用而不是放在它前面来评论它? pivotpivotxml/**/--

例如,如果您从 Java 运行此查询,那么如果您尝试在以下字符串中运行 SQL,我希望您会收到“缺少表达式”错误:

String sql =
  "select * from (SELECT o.ship_to_customer_no, ol.item_no,ol.amount " +
  "FROM t_order o, t_order_line ol " +
  "WHERE o.NO = ol.order_no and ol.item_no in (select distinct(item_no) from t_order_line)) " + 
  "pivot --xml " +
  "( SUM(amount) FOR item_no IN ( select distinct(item_no) as item_no_ from t_order_line))";

因为这个 SQL 字符串被连接成一行,所以--前面的xml会导致 Oracle 忽略它后面的查询中的所有内容。

编辑:在您添加的程序中,我关心的是这部分(缩写):

v_sql := 'begin
           select * from ...;
          end;';

open DENEME for
select v_sql from dual;

我不明白你为什么使用BEGINand 。尝试删除它们。ENDv_sql

另外,请注意不要在 SQL 语句字符串中留下分号。ORA-00911: invalid character如果您尝试使用 EXECUTE IMMEDIATE 或类似方式运行它,以下将给出错误:

v_sql := 'select * from dual;'; /* note the semicolon inside the string */

但以下将是好的:

v_sql := 'select * from dual';

最后,您似乎希望返回此 SQL 查询的结果,而不是查询本身作为字符串。用以下内容替换您的open DENEME for ...语句应该可以满足您的要求:

open DENEME for v_sql;

编辑 2:在您的过程中,有一个对 DBMS_OUTPUT.PUT_LINE 的注释调用。你验证过这里生成的SQL是正确的吗?特别是,您确定没有用于形成v_query_itemsv_pivot_items包含'字符的值吗?

PIVOT使用动态 SQL可能存在问题。我不知道,也无能为力,因为我在这里无法访问 Oracle 11。如果您将 SQL 简化为一个小得多的查询,但仍然有一个查询,您是否仍然会遇到错误PIVOT?换句话说,找到一个简单的PIVOT查询(例如,你可以在 SQL Developer 中成功运行它或类似的东西),编写一个如下所示的过程,看看你是否从中得到任何数据:

CREATE OR REPLACE PROCEDURE dynamic_pivot_test(
  results OUT SYS_REFCURSOR
)
AS
BEGIN
  OPEN results FOR 'SELECT ...'; /* put the PIVOT query here. */
END;
/
于 2011-01-15T15:30:19.040 回答
0

2011 年 1 月 17 日编辑的过程很脏但可以工作,循环游标应该改变,更好的错误。处理动态 sql(目前首屈一指),将在有时间时尝试改进它。谢谢大家的帮助。

于 2011-01-17T14:52:41.573 回答