2017-05-21 138 views
0

我有4個表中的過程存儲過程

業務(abnnumber,名)

business_industry(abnnumber,industryid)

業(industryid,蚌)

trade_union使用(unionid)

我被分配到一行中獲得工會職稱,所有企業ABNNUMBER和企業名稱分別在不同行中使用存儲過程。

我嘗試什麼是:

CREATE [OR REPLACE] PROCEDURE INDUSTRY_INFORMATION 
[enter image description here][1](P_INDUSTRYID in integer, 
P_UNIONTITLE OUT VARCHAR2, 
P_BUSINESSNAME OUT VARCHAR2) AS 
BEGIN 
SELECT TRADE_UNION.UNIONTITLE, BUSINESS.BUSINESSNAME INTO    
P_UNIONTITLE,P_BUSINESSNAME 
FROM BUSINESS inner join BUSINESS_INDUSTRY ON 
BUSINESS.ABNNUMBER=BUSINESS_INDUSTRY.ABNNUMBER 
INNER JOIN INDUSTRY ON BUSINESS_INDUSTRY.INDUSTRYID=INDUSTRY.INDUSTRYID 
INNER JOIN TRADE_UNION ON INDUSTRY.UNIONID=TRADE_UNION.UNIONID; 
END; 

Sample data is in the link http://www.mediafire.com/file/8c4dwn4n88n8a42/strd_procedure.txt 

需要的輸出應是

UNIONTITLE(一行)

ABNNUMBER BUSINESS NAME(下一行)

``[1]: https://i.stack.imgur.com/sGuwe.jpg

+0

你需要的數據表或查詢? –

+1

程序代碼是**高度特定於供應商的** - 所以請添加一個標籤以指定您是使用mysql,postgresql,sql-server,oracle還是db2還是別的什麼完全。 –

+0

我提供了數據和輸出格式。 –

回答

0

我懷疑你需要這樣的東西:

create or replace procedure industry_info is 
begin 
    for r in (
    select tu.uniontitle ut, 
      listagg('['||b.abnnumber||'] '||b.businessname, ', ') 
      within group (order by b.businessname) blist 
     from business b 
     join business_industry bi on b.abnnumber = bi.abnnumber 
     join industry i on bi.industryid = i.industryid 
     join trade_union tu on i.unionid = tu.unionid 
     group by tu.uniontitle) 
    loop 
    dbms_output.put_line(r.ut); 
    dbms_output.put_line(r.blist); 
    dbms_output.put_line('-----'); 
    end loop; 
end; 

功能listagg是在Oracle 11g或更高版本中可用。

輸出:

Cleaners' Union 
[12345678912] Consolidated Proerty Services, [12345678929] Gold Cleaning Services, [12345678926] Home Cleaning Services, [12345678924] Shine Cleaning 
----- 
Construction Workers' Union 
[12345678920] Build a House, [12345678919] Construction Solutions, [12345678922] Joe's Rubbish Removal, [12345678918] Leak and Roof Repair, [12345678928] Muscle Rubbish Removals 
----- 
Electricians' Union 
[12345678916] Change the Fuse Electricals, [12345678921] Hire a Wire, [12345678917] Vicky Electricals 
----- 
Movers' Union 
[12345678913] Kohlan Movers, [12345678925] Moveit 
----- 
Mowers' Union 
[12345678923] Do it Right Mowers, [12345678911] James Mowers and Landscape 
----- 
Plumbers' Union 
[12345678927] 24X7 Plumbing Service, [12345678915] Anytime Plumbers, [12345678914] Pumbers Delivered 
-----