1)第一个例子反映了你的方法。这还不错,但最好尽量减少删除语句的数量。
CREATE TABLE so_courses_tbl
(
col VARCHAR2(10)
)
;
-- table so_courses_tbl created.
INSERT INTO so_courses_tbl VALUES(1);
-- 1 rows inserted.
DECLARE
CURSOR c1 IS
SELECT 1
FROM DUAL;
cnumber NUMBER;
BEGIN
OPEN c1;
FETCH c1 INTO cnumber;
CLOSE c1;
DELETE
FROM so_courses_tbl
WHERE col = cnumber;
COMMIT;
END;
-- anonymous block completed
SELECT COUNT(1) FROM so_courses_tbl;
-- 0
2)这就是你如何一步完成的。您可以使用 variables is the WHERE 子句使其更灵活(如果您需要它...)
INSERT INTO so_courses_tbl VALUES('X');
INSERT INTO so_courses_tbl VALUES('X');
INSERT INTO so_courses_tbl VALUES('X');
-- 1 rows inserted.
-- 1 rows inserted.
-- 1 rows inserted.
BEGIN
DELETE
FROM so_courses_tbl c
WHERE EXISTS
(
SELECT 1
FROM DUAL d
WHERE c.col = d.dummy
);
DBMS_OUTPUT.PUT_LINE('Number of deleted rows: ' || SQL%ROWCOUNT);
COMMIT;
END;
-- Number of deleted rows: 3
试一试,让我们知道进展如何。