我有4个表tab_1,tab_2,tab_3和tab_4 如何使用单个动态查询获取所有4个表的计数?
expected result:
count of tab_1 =
count of tab_2 =
count of tab_3 =
count of tab_4 =
先谢谢
答案 0 :(得分:1)
听起来您想要运行四个单独的查询,而不是单个查询。这听起来像是在描述像
这样的东西DECLARE
TYPE tbl_list IS TABLE OF VARCHAR2(30);
l_tables tbl_list := tbl_list( 'table_1', 'table_2', 'table_3', 'table_4' );
l_cnt pls_integer;
BEGIN
FOR i IN 1 .. l_tables.count
LOOP
EXECUTE IMMEDIATE 'SELECT COUNT(*) FROM ' || l_tables(i)
INTO l_cnt;
dbms_output.put_line( 'Count of ' || l_tables(i) || ' = ' || l_cnt );
END LOOP;
END;
答案 1 :(得分:0)
请尝试:
SET SERVEROUTPUT ON
DECLARE
result1 NUMBER; result2 NUMBER; result3 NUMBER; result4 NUMBER;
BEGIN
select count(*) into result1 from tab_1;
select count(*) into result2 from tab_2;
select count(*) into result3 from tab_3;
select count(*) into result4 from tab_4;
DBMS_OUTPUT.PUT_LINE('count of tab_1=' || result1);
DBMS_OUTPUT.PUT_LINE('count of tab_2=' || result2);
DBMS_OUTPUT.PUT_LINE('count of tab_3=' || result3);
DBMS_OUTPUT.PUT_LINE('count of tab_4=' || result4);
END;
答案 2 :(得分:0)
虽然我不明白为什么你会想要这个,但是你问,我们回答。
要将这个全部压缩为单个查询,您可以尝试在双重的1个主查询中使用3个标量子查询。
选择(选择计数()来自tab_1)作为CountOfTab1, (选择计数()来自tab_2)作为CountOfTab2, (选择Count()From tab_3)作为CountOfTab3, (选择Count()From tab_4)作为CountOfTab4 进入countOfTab1, countOfTab2, countOfTab3, countOfTab4 来自Dual