2017-03-15 33 views
-1

我想在2代表在這裏給定的ID進行刪除操作使用遊標是須藤代碼在Oracle SQL

declare 
    cursor del_id is 
    select person_id from table_1 where termination is true 

begin 
    for id_x in del_id 
    delete from table_X where id=id_x 
    delete from tabele_Y where id=id_x 

如何做到這一點?我不能直接使用我的光標,請幫忙。 我只是嘗試打印本人身份證

begin 

    for id in del_id 
    LOOP 
    dbms_output.put_line(id); 
    END LOOP; 
end; 

收到此錯誤

Error report - 
ORA-06550: line 11, column 3: 
PLS-00306: wrong number or types of arguments in call to 'PUT_LINE' 
+0

問題是什麼?你有錯誤嗎?一個錯誤的行爲?此外,這是您的確切,完整的代碼或一些您不能轉換成Oracle SQL的僞代碼? – Aleksej

+0

'終止是真的'在Oracle中爲無效 –

回答

1

若要從遊標打印值,你需要明確寫入你想要的列; dbms_output.put_line無法處理可能包含具有不同類型的許多列的行,因此您需要將它傳遞給一個字符串。

SQL> declare 
    2  cursor del_id is select 1 as one, 2 as two from dual; 
    3 begin 
    4 FOR id IN del_id 
    5 LOOP 
    6  dbms_output.put_line(id.one || ' - ' || id.two); 
    7 END LOOP; 
    8 end; 
    9/
1 - 2 

PL/SQL procedure successfully completed. 

如果你需要在某些語句中使用的值從遊標,在你的問題中DELETE,你需要做的是相同的,通過明確地寫在列名;例如:

declare 
    cursor del_id is select 1 as one, 2 as two from dual; 
begin 
    FOR id IN del_id 
    LOOP 
    delete from someTable where someColumn = id.one; 
    END LOOP; 
end; 
+0

我原來的問題是要求刪除你能幫我嗎 –

+0

這沒有什麼不同,只是舉了一個例子 – Aleksej

0

你不需要光標:

BEGIN 
    DELETE FROM table_X 
    WHERE id IN (select person_id from table_1 where termination is true); 

    DELETE FROM table_Y 
    WHERE id IN (select person_id from table_1 where termination is true); 
END; 
/

你也可以使用一個集合:

CREATE TYPE id_type IS TABLE OF INTEGER; 
/

DECLARE 
    ids id_type; 
BEGIN 
    SELECT person_id 
    BULK COLLECT INTO ids 
    FROM table_1 
    WHERE termination is true; 

    DELETE FROM table_X 
    WHERE id MEMBER OF ids; 

    DELETE FROM table_Y 
    WHERE id MEMBER OF ids; 
END; 
/