连接oracle plsql中的输出值

时间:2015-07-24 06:02:20

标签: oracle plsql

create table item_value_tab
as
select 1110 Item_value,000394 bb_to_no, 55555 order_number, 'ABC' cust_no from dual
union all
select 1110 Item_value,000394 bb_to_no, 66666 order_number, 'ABC' cust_no from dual
union all
select 1110 Item_value,000394 bb_to_no, 77777 order_number, 'ABC' cust_no from dual
union all
select 1110 Item_value,000123 bb_to_no, 1111 order_number, 'ABC' cust_no from dual
union all
select 1110 Item_value,000124 bb_to_no, 2222 order_number, 'ABC' cust_no from dual
union all
select 1110 Item_value,000124 bb_to_no, 3333 order_number, 'ABC' cust_no from dual
union all
select 1110 Item_value,000011 bb_to_no, 75257 order_number, 'ABC' cust_no from dual
union all
select 1110 Item_value,000011 bb_to_no, 65257 order_number, 'ABC' cust_no from dual;

在光标1中采用以下代码

SELECT DISTINCT DECODE(nvl(Item_value,0),bb_to_no,null,'Item  Value '||nvl(bb_to_no,0)||' Is not correct for order number '||order_number||' FOR Customer  '||cust_no) vmsg
from item_value_tab;

获得输出

Item Value 000394 Is not correct for order number 55555 FOR Customer  ABC
Item Value 000394 Is not correct for order number 66666 FOR Customer  ABC
Item Value 000394 Is not correct for order number 77777 FOR Customer  ABC

Item Value 000123 Is not correct for order number 1111 FOR Customer  ABC
Item Value 000124 Is not correct for order number 2222 FOR Customer  ABC
Item Value 000124 Is not correct for order number 3333 FOR Customer  ABC

Item Value 000011 Is not correct for order number 75257 FOR Customer  ABC
Item Value 000011 Is not correct for order number 65257 FOR Customer  ABC
-------------------------------------------------------------------------------
create table item_not_tab
as
select 0 item_id,53555 bb_to_no,1052 order_number,'AAA' cust_no from dual
union all
select 0 item_id,53655 bb_to_no,1138 order_number,'AAA' cust_no from dual;

Need to take the below code in cursor 2
select DECODE(nvl(item_id,0),0,'items not created for  '||bb_to_no||'order number '||order_number||' FOR Customer  '||cust_no,NULL) vmsg
from item_not_tab;

获得输出

items not created for 53555 order number 1052 FOR Customer  AAA
items not created for 53655 order number 1052 FOR Customer  AAA

预期输出

Item Value Problems
Item Value 000394 Is not correct for order number 55555,66666,77777 FOR Customer  ABC
Item Value 000123,000124 Is not correct for order number 1111 FOR Customer  ABC
Item Value 000124 Is not correct for order number 2222,3333 FOR Customer  ABC
Item Value 000011 Is not correct for order number 75257,65257 FOR Customer  ABC

Items not found Problems
items not created for 53555,53655 order number 1052 FOR Customer  AAA

注意:请不要在一个游标中加入两个表,我需要使用2个游标。两个表实际上都是相同的,但测试用例的目的是我有2个d / f表

oracle 11g 1.1和1.2

2 个答案:

答案 0 :(得分:1)

您需要首先连接 LOOP 中的tot_number,然后在LOOP之外使用值

例如,

SQL> SET serveroutput ON
SQL> DECLARE
  2    v VARCHAR2(100);
  3  BEGIN
  4    FOR i IN
  5    (SELECT 55555 str FROM dual
  6    UNION
  7    SELECT 66666 str FROM dual
  8    UNION
  9    SELECT 77777 str FROM dual
 10    )
 11    LOOP
 12      v:= ltrim(v||','||i.str, ',');
 13    END LOOP;
 14    dbms_output.put_line('Output is concatenated '||v);
 15  END;
 16  /
Output is concatenated 55555,66666,77777

PL/SQL procedure successfully completed.

SQL>

答案 1 :(得分:1)

请检查以下QUERY:

SELECT DISTINCT o.bb_to_no, 
 (SELECT listagg(order_number,',') WITHIN GROUP (ORDER BY 1 desc) 
  FROM item_value_tab i WHERE o.bb_to_no = i.bb_to_no) AS total_num,
 (SELECT DISTINCT(k.cust_no) FROM item_value_tab k 
   WHERE o.bb_to_no = k.bb_to_no) AS cust_name
 FROM item_value_tab o

(OR)

编辑:请检查一下。

//It create collection of type table to store all the order_numbers

CREATE OR REPLACE TYPE t_varchar2_tab AS TABLE OF NUMBER;

// Below procedure will concatenate the list of order_numbers stored in
// the above created collection type.

CREATE OR REPLACE FUNCTION tab_to_string
(p_varchar2_tab  IN  t_varchar2_tab,
 p_delimiter     IN  VARCHAR2 DEFAULT ',') RETURN VARCHAR2 IS
   l_string     VARCHAR2(32767);
    BEGIN
     FOR i IN p_varchar2_tab.FIRST .. p_varchar2_tab.LAST LOOP
      IF i != p_varchar2_tab.FIRST THEN
       l_string := l_string || p_delimiter;
     END IF;
    l_string := l_string || p_varchar2_tab(i);
   END LOOP;
  RETURN l_string;
 END tab_to_string;


 SELECT bb_to_no,cust_no,
   tab_to_string(CAST(COLLECT(order_number) AS t_varchar2_tab)) 
  AS tot_number
 FROM item_value_tab
 GROUP BY bb_to_no,cust_no;

P.S:您可以在下面的链接中找到有关COLLECT()函数的更多信息。

  http://www.oracle-developer.net/display.php?id=306