0

我有一个搜索问题的(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

?

4

2 回答 2

0

在查询中,您可以使用函数,但不允许使用存储过程。如果您必须调用存储过程,您应该使用游标并循环通过Oracle.com 上的结果 PL/SQL 游标

BEGIN
   FOR c IN (SELECT * FROM tablename) 
   LOOP
       your_procedure(c.columnname, c.othercolumnname);
   END LOOP;
END;
于 2013-07-16T09:00:31.613 回答
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
于 2013-07-16T14:10:26.497 回答