【问题标题】:Deleting Records in a Nested Table (plsql)删除嵌套表中的记录 (plsql)
【发布时间】:2019-03-08 20:01:21
【问题描述】:

我想删除记录类型中列 (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

谢谢!

【问题讨论】:

    标签: sql oracle plsql oracle11g plsqldeveloper


    【解决方案1】:

    如果你想从集合中删除一个元素,那你就用错了

    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;
    

    【讨论】:

      【解决方案2】:

      已解决。

      这样做:

          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
      

      谢谢!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多