2013-07-16 40 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