【问题标题】:Finding record count from different tables for the list of values in pl/sql从不同表中查找记录计数以获取 pl/sql 中的值列表
【发布时间】:2015-07-22 07:17:46
【问题描述】:

我有 5 个不同的表,在所有表中 bat_id 是公共列。我有 bat_id 列表,我必须检查所有 5 个表预期示例输出中的每个 bat_id 是否存在记录,如下所示

示例输出:

------------------------------------------------------
l table1 l table2 l table3 l table4 l table5 l bat_id
l    0   l   21   l   5    l     0  l     1  l  452
l    5   l   3    l   0    l     0  l     15 l  123
l    235 l   0    l   0    l     0  l     87 l  586
-------------------------------------------------------

我已经尝试了以下查询,但它一次只能为一个 bat_id 工作如何修改以下查询以检查单个查询中所有 bat_id 值是否存在记录。

我必须找到记录计数的 bat_id 值列表 bat_id = 452,123,586,5869,001

    declare
Bat_id Number 
&Bat_id = 452
begin
select  
(Select count(*)  from Document where bat_id=&Bat_id) as table1,
(Select count(*) from Eds_task where bat_id=&Bat_id) as table2,
(Select count(*) from MI_Doc_Reads where bat_id=&Bat_id) as table3,
(Select count(*) from MI_Batch_Status where bat_id=&Bat_id) as table4,
(Select count(*) from Batch where bat_id=&Bat_id) as table5
from dual;
end

提前致谢

【问题讨论】:

标签: sql sql-server-2008 plsql plsqldeveloper


【解决方案1】:

在匿名块内打印 select 语句结果可能需要游标。我太懒了,所以我就这样做了。

declare
  l_bat_id_list               varchar2(200) := '452,123,586,5869,001';
  l_bat_id_curr_item          number;
  l_list_sep_count            number;
  l_curr_sep_pos              number;
  l_count                     number;
begin
  l_list_sep_count := regexp_count(l_bat_id_list, ',');

  for curr_item in 1..l_list_sep_count + 1 loop
    l_curr_sep_pos     := instr(l_bat_id_list, ',', 1, 1);
    if l_curr_sep_pos != 0 then
      l_bat_id_curr_item := substr(l_bat_id_list, 1, l_curr_sep_pos - 1);
      l_bat_id_list      := substr(l_bat_id_list, l_curr_sep_pos + 1);
    else
      l_bat_id_curr_item := l_bat_id_list;
    end if;

    Select count(*) into l_count from Document where bat_id = l_bat_id_curr_item;
    dbms_output.put_line('table: 1, bat_id: ' || l_bat_id_curr_item || ', count: ' || l_count);
    Select count(*) into l_count from Eds_task where bat_id = l_bat_id_curr_item;
    dbms_output.put_line('table: 2, bat_id: ' || l_bat_id_curr_item || ', count: ' || l_count);
    Select count(*) into l_count from MI_Doc_Reads where bat_id = l_bat_id_curr_item;
    dbms_output.put_line('table: 3, bat_id: ' || l_bat_id_curr_item || ', count: ' || l_count);
    Select count(*) into l_count from MI_Batch_Status where bat_id = l_bat_id_curr_item;
    dbms_output.put_line('table: 4, bat_id: ' || l_bat_id_curr_item || ', count: ' || l_count);
    Select count(*) into l_count from Batch where bat_id = l_bat_id_curr_item;
    dbms_output.put_line('table: 5, bat_id: ' || l_bat_id_curr_item || ', count: ' || l_count);

  end loop;
end;
/

【讨论】:

    猜你喜欢
    • 2022-10-08
    • 1970-01-01
    • 1970-01-01
    • 2018-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多