【问题标题】:SQL/ Function LoopsSQL/函数循环
【发布时间】:2016-01-04 13:05:30
【问题描述】:

我正在尝试编写一个函数,该函数将一直运行,直到将某些内容插入我的表中。我希望它每 10 秒检查一次给我输出消息的条目。

我知道我必须使用 dbms_lock.sleep(10) 和 while 循环。

但任何其他建议都会非常有帮助。

【问题讨论】:

标签: sql function plsql oracle11g


【解决方案1】:

我完全同意 Frank Schmitt 的观点,SGBD 的触发器就像普通编程语言中的事件一样,对数据库的(数据库、表、行、列等)元素的许多操作都有触发器.

【讨论】:

    【解决方案2】:

    有几种方法可以回答您的问题:

    注意事项:

    我已经假设了 PL/SQL 的基本知识。

    今天我面前没有 IDE,我也不知道 您的架构等,因此代码将尽最大努力,您将需要 将您的桌面沙柱等替换为其中。

    我假设使用 DBMS_OUTPUT 就足够了 你的“输出信息”

    1) 按照您的要求,每 10 秒循环和轮询一次 这是一种非常讨厌的方式来识别何时将记录输入到您的数据库中。

    CREATE OR REPLACE 
    FUNCTION check_for_record
    AS
       -- Declare the cursor that checks for your record
       CURSOR db_rec_cur
       IS
          SELECT 1
            FROM <table_name>
           WHERE <criteria>;
    
       -- Variable to hold cursor output
       v_db_rec db_rec_cur%ROWTYPE;
    BEGIN
       -- Enable DBMS_OUTPUT
       DBMS_OUTPUT.ENABLE(1000000);
    
       -- Start the polling loop
       LOOP
          -- Output a message when testing for the record
          DBMS_OUTPUT.put_line(TO_CHAR(sysdate, 'YYYYMMDDHH24MISS')||': Checking for DB record');
    
          --  Test if the record is in the DB
          OPEN db_rec_cur;
          FETCH db_rec_cur INTO v_db_rec;
          CLOSE db_rec_cur;
    
          -- Exit the polling loop when a record has been found
          EXIT WHEN v_db_rec IS NOT NULL;
    
          -- IF the loop has not exited, sleep foir 10 seconds
          DBMS_LOCK.sleep(10);
    
       -- End the loop
       END LOOP;
    EXCEPTION
       WHEN others
       THEN
          -- Check the cursor is closed
          IF db_rec_cur%ISOPEN
          THEN 
             CLOSE db_rec_cur;
          END IF;
    
          -- Output the error
          DBMS_OUTPUT.put_line(SQLERRM);
    
          -- Propagate the error
          RAISE;
    END check_for_record;
    /
    

    2) 使用数据库触发器在插入记录时通知您

    如果您正在查找已插入的特定值,则:

    CREATE OR REPLACE
    TRIGGER recordinsert_trg
    AFTER INSERT ON <table_name>
    FOR EACH ROW
    DECLARE
    BEGIN
       -- Enable DBMS_OUTPUT
       DBMS_OUTPUT.ENABLE(1000000); 
    
       -- Check the ":new" values inserted to see if they match your criteria
       IF :new.<your_column> = <your_specific_value>
       THEN
          -- Output a message when testing for the record
          DBMS_OUTPUT.put_line(TO_CHAR(sysdate, 'YYYYMMDDHH24MISS')||': Your value was inserted!');
       END IF;
    EXCEPTION
       WHEN others
       THEN
          -- Output the error
          DBMS_OUTPUT.put_line(SQLERRM);
    
          -- Propagate the error
          RAISE;
    END recordinsert_trg;
    /
    

    如果您只是想知道某些东西/任何东西何时插入到您的表格中,那么:

    CREATE OR REPLACE
    TRIGGER recordinsert_trg
    AFTER INSERT ON <table_name>
    DECLARE
    BEGIN
       -- Enable DBMS_OUTPUT
       DBMS_OUTPUT.ENABLE(1000000); 
    
       -- Output a message when the record was inserted
       DBMS_OUTPUT.put_line(TO_CHAR(sysdate, 'YYYYMMDDHH24MISS')||': A record was inserted!');
    EXCEPTION
       WHEN others
       THEN
          -- Output the error
          DBMS_OUTPUT.put_line(SQLERRM);
    
          -- Propagate the error
          RAISE;
    END recordinsert_trg;
    /
    

    我希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2014-08-22
      • 2011-09-18
      • 2017-04-04
      • 2020-09-16
      • 1970-01-01
      • 2012-08-22
      • 2013-04-03
      • 2018-08-06
      相关资源
      最近更新 更多