如何編寫一個程序來自動執行一組查詢 (How to write a procedure to execute set of queries automatically)


問題描述

如何編寫一個程序來自動執行一組查詢 (How to write a procedure to execute set of queries automatically)

我已經編寫了一個程序來授予對特定模式的所有表的權限以休息其他模式。

  create or replace PROCEDURE GRANTS_PROC

    IS

    CURSOR GRANTS_CURSOR
    IS
        SELECT 'GRANT SELECT, INSERT, UPDATE, DELETE ON "'
        ||T.OWNER
        ||'"."'
        ||TABLE_NAME
        ||'" TO '
        ||(SELECT rtrim(listagg(U.username||',')
         within group (order by U.username),',') USERNAME
         FROM ALL_USERS U
         WHERE U.USERNAME!=T.OWNER
         AND U.USERNAME IN
        ('AAA','BBB','CCC','DDD','EEE','FFF','GGG','HHH','III'))||';' FINAL_TXT
         FROM  ALL_TABLES T
         WHERE T.OWNER IN
        ('AAA','BBB','CCC','DDD','EEE','FFF','GGG','HHH','III')
         ORDER BY T.OWNER,UPPER(T.TABLE_NAME);
    BEGIN
        ‑‑DBMS_OUTPUT.PUT_LINE('CURSOR_GRANTS.FINAL_TXT');
        ‑‑QRY_TEXT:='ABC';
        FOR CURSOR_GRANTS IN GRANTS_CURSOR
        LOOP
        DBMS_OUTPUT.PUT_LINE(CURSOR_GRANTS.FINAL_TXT);

            EXECUTE IMMEDIATE CURSOR_GRANTS.FINAL_TXT;

        END LOOP;
    END;
/

上面的程序編譯成功,但是在執行時它沒有進入FOR循環運行EXECUTE IMMEDIATE塊,而是編譯了PL/SQL程序成功地。

可以做些什麼來修復我的程序並使其正常工作?


參考解法

方法 1:

All_USERS and ALL_TABLES should be changed to DBA_USERS and DBA_TABLES, Hence my procedure works.

Because the all_tables view shows the tables that the owner of the stored procedure has privileges on, so, unless the stored procedure is being created by a privileged user, or a user that already has those grants (with grant option so they can actually give the privileges), there may be nothing in all_tables that qualifies.

(by sabarish jacksonsabarish jackson)

參考文件

  1. How to write a procedure to execute set of queries automatically (CC BY‑SA 2.5/3.0/4.0)

#for-loop #plsql #procedures #cursor #execute-immediate






相關問題

從R中的類引用列表中獲取類引用字段的最小值 (Get min value of a class reference field from a list of class references in R)

在 SQL Server 2008 中運行 WHILE 或 CURSOR 或兩者 (Running WHILE or CURSOR or both in SQL Server 2008)

danh sách trong python, vòng lặp for, mảng (list in python, loop for, array)

如何編寫一個程序來自動執行一組查詢 (How to write a procedure to execute set of queries automatically)

xPath 在使用 for-each 循環變量時找不到選擇器,但可以正常工作 (xPath not finding selector when using for-each loop variable, but works otherwise)

為什麼for循環重複輸出相同的記錄?JavaScript (Why for loop output same record repeatedly? JavaScript)

在 for 循環中將參數傳遞給 setTimeout (Passing argument to setTimeout in a for loop)

使用python匹配條件後如何從列表的開始迭代開始for循環 (How to start for-loop from the starting iteration of list after matching the condition using python)

BASH:在 for 循環中使用 continue (BASH: Using a continue in a for loop)

如何識別 For / Select / Loop 中的行號 (How do I identify the row number in a For / Select / Loop)

如何循環遍歷列表中的項目不斷附加在循環中的列表? (how to loop through a list where the items of the list are constantly appended in the loop?)

是否可以僅使用 for 循環來實現包含 for 循環的遞歸函數,該循環包含對上述函數的調用? (Can a recursive function containing a for loop that contains a call of the mentioned function be implemented using only for loops?)







留言討論