我制作了一个嵌套的表格,其中存储了姓名和姓氏。一个名字有两个姓氏。
CREATE OR REPLACE TYPE list_surname AS TABLE OF VARCHAR2(10);
/
CREATE TABLE person (name varchar2(10),
surname list_surname)
NESTED TABLE surname STORE AS list;
/
INSERT INTO person VALUES('Olsen', list_surname('Arthur', 'Baxter'));
INSERT INTO person VALUES('Jensen', list_surname('Barney', 'Louis'));
INSERT INTO person VALUES('Andersen', list_surname('Chase', 'Mason'));
/
现在我想制作一个匿名区块来计算并显示有多少人甚至有一个包含字母'u'的姓氏,如果那个人的姓氏中有'u'字母,那么我想打印那个人(姓名+姓氏)。这是我到目前为止所做的:
set serveroutput on
DECLARE
v_var VARCHAR2(32);
v_nr NUMBER(30);
cursor c1 (var VARCHAR2) is
select regexp_count(var,'[u]',1,'i') from dual;
BEGIN
FOR i IN person.FIRST .. person.LAST LOOP
v_nr := 0;
open c1(person.list_surname);
loop
fetch c1 into v_nr;
exit when c1%notfound;
IF v_nr > 0 THEN
DBMS_OUTPUT.put_line(person);
end if;
end loop;
close c1;
end loop;
END;
/
我不擅长使用嵌套表,因为这是我的第一次。欢迎任何建议。谢谢!
答案 0 :(得分:0)
尝试以下方法:
set serveroutput on
DECLARE
v_var VARCHAR2 (32);
v_nr NUMBER (30) := 0;
CURSOR c_name
IS
SELECT name FROM person;
CURSOR c_surname (p_name person.name%TYPE)
IS
SELECT VALUE (c) sn
FROM THE (SELECT surname
FROM person
WHERE name = p_name) c;
BEGIN
FOR cur IN c_name
LOOP
FOR cs IN c_surname (cur.name)
LOOP
IF INSTR (cs.sn, 'u') > 0
THEN
v_nr := v_nr + 1;
DBMS_OUTPUT.put_line (
cur.name || ' ' || cs.sn);
END IF;
END LOOP;
END LOOP;
dbms_output.put_line('Total Count: '||v_nr);
END;
/