如果您从嵌套表中删除元素,会发生什么?

问题描述 投票:0回答:1
在学习嵌套表的过程中,我遇到了一条声明,即使您从嵌套表中删除一个值,索引仍然存在,但已分配为null。这是真的吗?

在SQL Developer上运行时,下面的代码下面的代码不会提供精确的输出。 而不是给出此输出:

Index 3 exists but no value assigned.

它给了我:

Index 3 does not exist.
我不相信 - 它如何与所说的上述语句不匹配。
DECLARE
   TYPE nt_type IS TABLE OF NUMBER;
   nt nt_type := nt_type(10, 20, 30, 40, 50);
BEGIN
   DBMS_OUTPUT.PUT_LINE('Before DELETE: Count = ' || nt.COUNT); -- Output: 5

   nt.DELETE(3);  -- Deletes the 3rd element (value 30)

   DBMS_OUTPUT.PUT_LINE('After DELETE: Count = ' || nt.COUNT); -- Output: 4

   IF nt.EXISTS(3) THEN
      DBMS_OUTPUT.PUT_LINE('Index 3 exists but no value assigned.');
   ELSE  
      DBMS_OUTPUT.PUT_LINE('Index 3 does not exist.');
   END IF;
   
   -- Display all elements including sparse indexes
   FOR i IN 1..nt.LAST LOOP
      IF nt.EXISTS(i) THEN
         DBMS_OUTPUT.PUT_LINE('Element at index ' || i || ' = ' || COALESCE(TO_CHAR(nt(i)), 'NULL'));
      END IF;
   END LOOP;
END;
/

指望输出:

Before DELETE: Count = 5 After DELETE: Count = 4 Index 3 exists but no value assigned. Element at index 1 = 10 Element at index 2 = 20 Element at index 3 = NULL Element at index 4 = 40 Element at index 5 = 50

MySQL开发人员输出

Before DELETE: Count = 5 After DELETE: Count = 4 Index 3 does not exist. Element at index 1 = 10 Element at index 2 = 20 Element at index 4 = 40 Element at index 5 = 50

当您从嵌套表集合中删除一个元素时,它将该位置标记为“不存在的”(与现有的相同但持有
oracle-database plsql plsqldeveloper
1个回答
0
投票
值不同),但是却不在内部对内存分配。因此,假设您从集合的末尾删除了,则可以将值重新分配到同一位置而不首先调用

.extend

方法。但是,一旦您到达从未分配过的位置,您就无法不先将其分配给它,因为这就是分配内存的原因。
    
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.