删除嵌套 Table 中的记录 (plsql)
Deleting Records in a Nested Table (plsql)
我想删除记录类型中的列(emp_sal)有5000+的元素,但是它给我一个错误。我不知道我是否将 .DELETE 方法放在了正确的区域。
代码:
DECLARE
TYPE rec IS RECORD (
emp_id HR.EMPLOYEES.EMPLOYEE_ID%TYPE,
emp_fname HR.EMPLOYEES.FIRST_NAME%TYPE,
emp_lname HR.EMPLOYEES.LAST_NAME%TYPE,
emp_job HR.EMPLOYEES.JOB_ID%TYPE,
emp_sal HR.EMPLOYEES.SALARY%TYPE );
TYPE rec_table IS TABLE OF rec;
rec_list rec_table := rec_table();
BEGIN
SELECT HR.EMPLOYEES.EMPLOYEE_ID,
HR.EMPLOYEES.FIRST_NAME,
HR.EMPLOYEES.LAST_NAME,
HR.EMPLOYEES.JOB_ID,
HR.EMPLOYEES.SALARY
BULK COLLECT INTO rec_list
FROM HR.EMPLOYEES;
FOR i IN rec_list.FIRST..rec_list.LAST LOOP
IF (rec_list(i).emp_sal > 5000) THEN
rec_list(i).DELETE();
END IF;
DBMS_OUTPUT.PUT_LINE('element: '||i||' '||
'emp id: '||rec_list(i).emp_id||
' full name: '||rec_list(i).emp_fname||
' '||rec_list(i).emp_lname||
' job: '||rec_list(i).emp_job||
' salary: '||rec_list(i).emp_sal);
END LOOP;
END;
输出:
ORA-06550: line 28, column 20: PLS-00302: component 'DELETE' must be declared
谢谢!
已解决。
是否这样做:
FOR i IN rec_list.FIRST..rec_list.LAST LOOP
IF (rec_list(i).emp_sal > 5000) THEN
rec_list.DELETE(i);
CONTINUE;
END IF;
DBMS_OUTPUT.PUT_LINE('element: '||i||' '||
'emp id: '||rec_list(i).emp_id||
' full name: '||rec_list(i).emp_fname||
' '||rec_list(i).emp_lname||
' job: '||rec_list(i).emp_job||
' salary: '||rec_list(i).emp_sal);
END LOOP;
------------------------输出:
element: 6 emp id: 105 full name: David Austin job: IT_PROG salary: 4800
element: 7 emp id: 106 full name: Valli Pataballa job: IT_PROG salary: 4800
element: 8 emp id: 107 full name: Diana Lorentz job: IT_PROG salary: 4200
element: 16 emp id: 115 full name: Alexander Khoo job: PU_CLERK salary: 3100
element: 17 emp id: 116 full name: Shelli Baida job: PU_CLERK salary: 2900
element: 18 emp id: 117 full name: Sigal Tobias job: PU_CLERK salary: 2800
element: 19 emp id: 118 full name: Guy Himuro job: PU_CLERK salary: 2600
element: 20 emp id: 119 full name: Karen Colmenares job: PU_CLERK salary: 2500
element: 26 emp id: 125 full name: Julia Nayer job: ST_CLERK salary: 3200
谢谢!
如果你想从集合中删除一个元素,那你就用错了
DELETE with no parameters removes all elements from a collection, setting COUNT to 0.
DELETE(n) removes the nth element from an associative array with a numeric key or a nested table. If the associative array has a string key, the element corresponding to the key value is deleted. If n is null, DELETE(n) does nothing.
DELETE(m,n) removes all elements in the range m..n from an associative array or nested table. If m is larger than n or if m or n is NULL, DELETE(m,n) does nothing.
所以在你的例子中正确的是
IF (rec_list(i).emp_sal > 5000) THEN
rec_list.DELETE(i);
END IF;
我想删除记录类型中的列(emp_sal)有5000+的元素,但是它给我一个错误。我不知道我是否将 .DELETE 方法放在了正确的区域。 代码:
DECLARE
TYPE rec IS RECORD (
emp_id HR.EMPLOYEES.EMPLOYEE_ID%TYPE,
emp_fname HR.EMPLOYEES.FIRST_NAME%TYPE,
emp_lname HR.EMPLOYEES.LAST_NAME%TYPE,
emp_job HR.EMPLOYEES.JOB_ID%TYPE,
emp_sal HR.EMPLOYEES.SALARY%TYPE );
TYPE rec_table IS TABLE OF rec;
rec_list rec_table := rec_table();
BEGIN
SELECT HR.EMPLOYEES.EMPLOYEE_ID,
HR.EMPLOYEES.FIRST_NAME,
HR.EMPLOYEES.LAST_NAME,
HR.EMPLOYEES.JOB_ID,
HR.EMPLOYEES.SALARY
BULK COLLECT INTO rec_list
FROM HR.EMPLOYEES;
FOR i IN rec_list.FIRST..rec_list.LAST LOOP
IF (rec_list(i).emp_sal > 5000) THEN
rec_list(i).DELETE();
END IF;
DBMS_OUTPUT.PUT_LINE('element: '||i||' '||
'emp id: '||rec_list(i).emp_id||
' full name: '||rec_list(i).emp_fname||
' '||rec_list(i).emp_lname||
' job: '||rec_list(i).emp_job||
' salary: '||rec_list(i).emp_sal);
END LOOP;
END;
输出:
ORA-06550: line 28, column 20: PLS-00302: component 'DELETE' must be declared
谢谢!
已解决。
是否这样做:
FOR i IN rec_list.FIRST..rec_list.LAST LOOP
IF (rec_list(i).emp_sal > 5000) THEN
rec_list.DELETE(i);
CONTINUE;
END IF;
DBMS_OUTPUT.PUT_LINE('element: '||i||' '||
'emp id: '||rec_list(i).emp_id||
' full name: '||rec_list(i).emp_fname||
' '||rec_list(i).emp_lname||
' job: '||rec_list(i).emp_job||
' salary: '||rec_list(i).emp_sal);
END LOOP;
------------------------输出:
element: 6 emp id: 105 full name: David Austin job: IT_PROG salary: 4800
element: 7 emp id: 106 full name: Valli Pataballa job: IT_PROG salary: 4800
element: 8 emp id: 107 full name: Diana Lorentz job: IT_PROG salary: 4200
element: 16 emp id: 115 full name: Alexander Khoo job: PU_CLERK salary: 3100
element: 17 emp id: 116 full name: Shelli Baida job: PU_CLERK salary: 2900
element: 18 emp id: 117 full name: Sigal Tobias job: PU_CLERK salary: 2800
element: 19 emp id: 118 full name: Guy Himuro job: PU_CLERK salary: 2600
element: 20 emp id: 119 full name: Karen Colmenares job: PU_CLERK salary: 2500
element: 26 emp id: 125 full name: Julia Nayer job: ST_CLERK salary: 3200
谢谢!
如果你想从集合中删除一个元素,那你就用错了
DELETE with no parameters removes all elements from a collection, setting COUNT to 0.
DELETE(n) removes the nth element from an associative array with a numeric key or a nested table. If the associative array has a string key, the element corresponding to the key value is deleted. If n is null, DELETE(n) does nothing.
DELETE(m,n) removes all elements in the range m..n from an associative array or nested table. If m is larger than n or if m or n is NULL, DELETE(m,n) does nothing.
所以在你的例子中正确的是
IF (rec_list(i).emp_sal > 5000) THEN
rec_list.DELETE(i);
END IF;