存储过程

时间:2017-05-21 18:28:41

标签: sql oracle procedure proc-sql

我在程序中使用了4个表

业务(abnnumber,名称)

business_industry(abnnumber,industryid)

业(industryid,蚌)

trade_union(蚌)

我被指派使用存储过程在一行中获得工会头衔以及所有业务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

1 个答案:

答案 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;

Oracle 11g或更高版本中提供了函数listagg

输出:

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
-----