Oracle 过程 'with query' 插入到 table

Oracle procedure 'with query' insert into table

我在 TOAD 中创建了一个 Oracle SQL 查询,它运行良好。我现在需要将其放入程序中。

查询必须根据不同的条件创建两个计数(我使用了 With Select)并将它们加上日期和位置插入到 table.

有效的查询是

with
Selected_animals as 
(   SELECT  TO_CHAR(SYSDATE,'DD/MM/YYYY' ) as Report_Date,
           loc.name location,
           count(rran.id) as Count_exported
    FROM   rr_animals rran,
           contact con,
           locations loc,
           names nam
    WHERE  con.connum = rran.connum
    AND    con.loc_id = loc.id
    AND    con.connum = nam.connum
    AND    nam.name_type = 'STAND'
    AND    nam.dob IS NOT NULL
    AND    rran.sex IS NOT NULL
    AND    rran.web_display = 'Y'
    AND    rran.web_description IS NOT NULL
    AND    rran.visit_end_date IS NULL
    AND    con.loc_id IS NOT NULL
    AND    con.datedl IS NULL
    AND    rran.hold_user IS NULL
    AND    rran.assess_status IS NULL
    AND    EXISTS (SELECT rrim.id
                   FROM   rr_images rrim
                   WHERE  rrim.image_type = 'KENNEL'
                   AND    rrim.rran_id = rran.id
                   AND    DBMS_LOB.GETLENGTH(rrim.image_object) >0
                   AND    rrim.image_object IS NOT NULL)
    group by loc.NAME ),

total_animals as 
(select vbav.sitename as location,
       count(vbav.rran_ID) as Count_available

from v_bx_all_animal_visits vbav
where visit_end_date is null
and concat != 'DELTD'
and concat != 'DSCD'
group by vbav.sitename)

select Total_animals.location,
       Selected_animals.Report_date,
       Selected_animals.count_exported,
       Total_animals.count_available

from Selected_animals, total_animals
where total_animals.location = selected_animals.location(+)

我已经查看了几种似乎可以编写程序的方法,但似乎没有任何效果。包括在 CREATE 或 REPLACE 下和 BEGIN 之前添加的:

(   o_location out bx_webstats_export_available.LOCATION%TYPE,
       o_date out bx_webstats_export_available.REPORT_DATE%TYPE,
       o_exported out bx_webstats_export_available.COUNT_EXPORTED%TYPE,
       o_available out bx_webstats_export_available.COUNT_AVAILABLE%TYPE   )

还在最后一个 where 语句之后和 End 之前添加:

INSERT INTO bx_webstats_export_available(location, report_date, count_export, count_available)
       values (Total_animals.location,
       Selected_animals.Report_date,
       Selected_animals.count_exported,
       Total_animals.count_available);

谁能帮我在程序中得到这个查询?

这是我第一次从头开始编写程序,我正在努力。

非常感谢,

你想做什么?将 select 的结果插入 table?如果是这样,以下应该就足够了:

create or replace procedure your_proc_name
as
begin
  insert into bx_webstats_export_available(location, report_date, count_export, count_available)
  with selected_animals as (select to_char(sysdate,'DD/MM/YYYY' ) as report_date,
                                   loc.name location,
                                   count(rran.id) as count_exported
                            from   rr_animals rran,
                                   contact con,
                                   locations loc,
                                   names nam
                            where  con.connum = rran.connum
                            and    con.loc_id = loc.id
                            and    con.connum = nam.connum
                            and    nam.name_type = 'STAND'
                            and    nam.dob is not null
                            and    rran.sex is not null
                            and    rran.web_display = 'Y'
                            and    rran.web_description is not null
                            and    rran.visit_end_date is null
                            and    con.loc_id is not null
                            and    con.datedl is null
                            and    rran.hold_user is null
                            and    rran.assess_status is null
                            and    exists (select rrim.id
                                           from   rr_images rrim
                                           where  rrim.image_type = 'KENNEL'
                                           and    rrim.rran_id = rran.id
                                           and    dbms_lob.getlength(rrim.image_object) >0
                                           and    rrim.image_object is not null)
                            group by loc.name),
          total_animals as (select vbav.sitename as location,
                                   count(vbav.rran_id) as count_available
                            from   v_bx_all_animal_visits vbav
                            where  visit_end_date is null
                            and    concat != 'DELTD'
                            and    concat != 'DSCD'
                            group by vbav.sitename)
  select total_animals.location,
         selected_animals.report_date,
         selected_animals.count_exported,
         total_animals.count_available
  from   selected_animals, total_animals
  where  total_animals.location = selected_animals.location(+);
end your_proc_name;
/

如果不是,请详细说明您要满足的要求。