根据SELECT结果使用多个EXEC语句

时间:2013-07-16 08:25:06

标签: sql oracle plsql exec

我有一个(Oracle SQL)查询来搜索问题:(简化)

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

2 个答案:

答案 0 :(得分:0)

在查询中,您可以使用函数,但不允许使用存储过程。 如果必须调用存储过程,则应使用游标并遍历结果PL/SQL Cursors on Oracle.com

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

答案 1 :(得分: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