2013-07-16 75 views
0

我有一個(甲骨文SQL)查詢,搜索的問題:(簡體)取決於SELECT結果使用多個EXEC語句

select a.date, (a.counter1 + c.counter)/(c.counter1 - a.counter1) percentdiferent 
    --daily table is agregated to ocupy less space and be faster when searching for the total counters for a day 
    from dailytable a 
    join (
    --the nonaggregated table has values for each minute 
    select trunc(b.date) date, sum(counter1) counter1 
     from minutetable b 
     where trunc(b.datea) = a.date 
     group by trunc(b.date) 
) c 
    on c.date = a.date and c.counter1 <> a.counter1 
    where percentdiferent > 5 

要解決這些問題,我需要執行一個過程:

exec aggregate(tablename, date) 

程序經常變化,我有多個表。有沒有辦法像

with checktables as (
    --above code 
) 

select date 
    from checktables 
    group by date 

if result > 0 
    for each result 
    exec aggregate(tablename,date) 

show results 

+0

可以使用PL/SQL或shell腳本嗎? –

+0

只有pl/sql :(,我們這裏沒有完全訪問機器:( – user2586356

回答

0

在查詢中,您可以使用函數,但不允許使用存儲過程。 如果你要通過結果PL/SQL Cursors on Oracle.com

BEGIN 
    FOR c IN (SELECT * FROM tablename) 
    LOOP 
     your_procedure(c.columnname, c.othercolumnname); 
    END LOOP; 
END; 
0
DECLARE 
    l_check_state NUMBER; 

    /* example procedures */ 
    PROCEDURE work_with_one 
    AS 
    BEGIN 
     DBMS_OUTPUT.PUT_LINE('Procedure one'); 
    END work_with_one; 

    PROCEDURE work_with_two 
    AS 
    BEGIN 
     DBMS_OUTPUT.PUT_LINE('Procedure two'); 
    END work_with_two; 

    PROCEDURE work_with_three 
    AS 
    BEGIN 
     DBMS_OUTPUT.PUT_LINE('Procedure three'); 
    END work_with_three; 
BEGIN 
    /* check query */ 
    WITH checktables AS 
    (
     SELECT ROUND(DBMS_RANDOM.VALUE(1, 3)) AS state 
     FROM dual 
    ) 
    SELECT state 
    INTO l_check_state 
    FROM checktables; 

    /* based on the query result run proper procedure */ 
    IF l_check_state = 1 THEN 
     /* execute proc 1 */ 
     work_with_one(); 
    ELSIF l_check_state = 2 THEN 
     /* execute proc 2 */ 
     work_with_two(); 
    ELSIF l_check_state = 3 THEN 
     /* execute proc 3 */ 
     work_with_three(); 
    ELSE 
     /* no evaluation */ 
     DBMS_OUTPUT.PUT_LINE('Error'); 
    END IF; 
END; 

由於隨機數字(DBMS_RANDOM.VALUE)的調用,你應該使用一個遊標循環中的存儲過程,當你運行了好幾次,你會看到不同的結果。它用於模擬檢查查詢行爲。

Procedure two 
Procedure one 
Procedure two 
Procedure one