这篇文章主要介绍“怎么使用CONSTANT变量与INDEX BY数组”,在日常操作中,相信很多人在怎么使用CONSTANT变量与INDEX BY数组问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么使用CONSTANT变量与INDEX BY数组”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
我创建了如下包头:
CREATE OR REPLACE PACKAGE plch_pkg AUTHID DEFINER IS TYPE names_t IS TABLE OF VARCHAR2 (20) INDEX BY PLS_INTEGER; END plch_pkg; /
哪些选项在执行之后会显示"3"?
(A)
DECLARE l_names CONSTANT plch_pkg.names_t := plch_pkg.names_t ('Horton', 'Hears', 'A Who') ; BEGIN DBMS_OUTPUT.put_line (l_names.COUNT); END; /
SQL> DECLARE 2 l_names CONSTANT plch_pkg.names_t 3 := plch_pkg.names_t ('Horton', 'Hears', 'A Who') ; 4 BEGIN 5 DBMS_OUTPUT.put_line (l_names.COUNT); 6 END; 7 / DECLARE l_names CONSTANT plch_pkg.names_t := plch_pkg.names_t ('Horton', 'Hears', 'A Who') ; BEGIN DBMS_OUTPUT.put_line (l_names.COUNT); END; ORA-06550: 第 3 行, 第 10 列: PLS-00222: 在此范围中不存在名为 'NAMES_T' 的函数 ORA-06550: 第 2 行, 第 14 列: PL/SQL: Item ignored ORA-06550: 第 5 行, 第 26 列: PLS-00320: 此表达式的类型声明不完整或格式不正确 ORA-06550: 第 5 行, 第 4 列: PL/SQL: Statement ignored SQL>
(B)
DECLARE l_names CONSTANT plch_pkg.names_t := plch_pkg.names_t () ; BEGIN l_names (1) := 'Horton'; l_names (2) := 'Hears'; l_names (3) := 'A Who'; DBMS_OUTPUT.put_line (l_names.COUNT); END; /
SQL> DECLARE 2 l_names CONSTANT plch_pkg.names_t 3 := plch_pkg.names_t () ; 4 BEGIN 5 l_names (1) := 'Horton'; 6 l_names (2) := 'Hears'; 7 l_names (3) := 'A Who'; 8 DBMS_OUTPUT.put_line (l_names.COUNT); 9 END; 10 / DECLARE l_names CONSTANT plch_pkg.names_t := plch_pkg.names_t () ; BEGIN l_names (1) := 'Horton'; l_names (2) := 'Hears'; l_names (3) := 'A Who'; DBMS_OUTPUT.put_line (l_names.COUNT); END; ORA-06550: 第 3 行, 第 29 列: PLS-00222: 在此范围中不存在名为 'NAMES_T' 的函数 ORA-06550: 第 2 行, 第 14 列: PL/SQL: Item ignored ORA-06550: 第 5 行, 第 4 列: PLS-00320: 此表达式的类型声明不完整或格式不正确 ORA-06550: 第 5 行, 第 4 列: PL/SQL: Statement ignored ORA-06550: 第 6 行, 第 4 列: PLS-00320: 此表达式的类型声明不完整或格式不正确 ORA-06550: 第 6 行, 第 4 列: PL/SQL: Statement ignored ORA-06550: 第 7 行, 第 4 列: PLS-00320: 此表达式的类型声明不完整或格式不正确 ORA-06550: 第 7 行, 第 4 列: PL/SQL: Statement ignored ORA-06550: 第 8 行, 第 26 列: PLS-00320: 此表达式的类型声明不完整或格式不正确 ORA-06550: 第 8 行, 第 4 列: PL/SQL: Statement ignored SQL>
(C)
CREATE OR REPLACE FUNCTION plch_dr_seuss_names RETURN plch_pkg.names_t IS l_return plch_pkg.names_t; BEGIN l_return (1) := 'Horton'; l_return (2) := 'Hears'; l_return (3) := 'A Who'; RETURN l_return; END plch_dr_seuss_names; / DECLARE l_names CONSTANT plch_pkg.names_t := plch_dr_seuss_names () ; BEGIN DBMS_OUTPUT.put_line (l_names.COUNT); END; /
SQL> CREATE OR REPLACE FUNCTION plch_dr_seuss_names 2 RETURN plch_pkg.names_t 3 IS 4 l_return plch_pkg.names_t; 5 BEGIN 6 l_return (1) := 'Horton'; 7 l_return (2) := 'Hears'; 8 l_return (3) := 'A Who'; 9 RETURN l_return; 10 END plch_dr_seuss_names; 11 / Function created SQL> DECLARE 2 l_names CONSTANT plch_pkg.names_t 3 := plch_dr_seuss_names () ; 4 BEGIN 5 DBMS_OUTPUT.put_line (l_names.COUNT); 6 END; 7 / 3 PL/SQL procedure successfully completed SQL>
(D)
CREATE OR REPLACE FUNCTION plch_dr_seuss_names ( name1_in IN VARCHAR2 , name2_in IN VARCHAR2 , name3_in IN VARCHAR2) RETURN plch_pkg.names_t IS l_return plch_pkg.names_t; BEGIN l_return (1) := name1_in; l_return (2) := name2_in; l_return (3) := name3_in; RETURN l_return; END plch_dr_seuss_names; / DECLARE l_names CONSTANT plch_pkg.names_t := plch_dr_seuss_names ('Horton', 'Hears', 'A Who') ; BEGIN DBMS_OUTPUT.put_line (l_names.COUNT); END; /
SQL> CREATE OR REPLACE FUNCTION plch_dr_seuss_names ( 2 name1_in IN VARCHAR2 3 , name2_in IN VARCHAR2 4 , name3_in IN VARCHAR2) 5 RETURN plch_pkg.names_t 6 IS 7 l_return plch_pkg.names_t; 8 BEGIN 9 l_return (1) := name1_in; 10 l_return (2) := name2_in; 11 l_return (3) := name3_in; 12 RETURN l_return; 13 END plch_dr_seuss_names; 14 / Function created SQL> DECLARE 2 l_names CONSTANT plch_pkg.names_t 3 := plch_dr_seuss_names ('Horton', 'Hears', 'A Who') ; 4 BEGIN 5 DBMS_OUTPUT.put_line (l_names.COUNT); 6 END; 7 / 3 PL/SQL procedure successfully completed SQL>
答案CD. ORACLE只对嵌套表(NESTED TABLE)和可变数组(VARRAY)提供构造函数(constructor function), ASSOCIATED ARRAY (即INDEX BY表)则没有这个构造函数,必须采用自定义函数。常量在声明的部分必须赋值,不能够在块中赋值。
到此,关于“怎么使用CONSTANT变量与INDEX BY数组”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。