PL/SQL Challenge 每周一题:2022-9-17 游标循环(21C)

[复制链接]
查看11 | 回复1 | 2008-9-15 01:28:12 | 显示全部楼层 |阅读模式
**答对且答案未经编辑的puber将获得纪念章一枚(答案不可编辑但可发新贴补充或纠正),其他会员如果提供有价值的分析、讨论也可获得纪念章一枚。
每两周的优胜者可获得itpub奖励的技术图书一本。
以往旧题索引:
http://www.itpub.net/forum.php?m ... eid&typeid=1808
原始出处:
https://devgym.oracle.com/
作者:Anthony Harper
运行环境:SQLPLUS, SERVEROUTPUT已打开, **版本要求:21C
注:本题给出答案时候要求给予简要说明才能得到奖品
你有一张表,存储着书籍的作者,以及你为每个作者收集了多少本书:
create table quiz_authors
as
select ** as author_id, 'Jane Austen' as author_name, 1 as books_collected from dual union all
select 102, 'James Joyce', 2 from dual union all
select 103, 'Charles Dickens', 1 from dual union all
select 104, 'Isaac Asimov', 10 from dual;
给定下列这个未完成的代码,哪些选项可以用来取代##REPLACE##,以产生指定的结果:
set serveroutput on;
declare
type t_authors_by_name is table of quiz_authors%rowtype index by quiz_authors.author_name%type;
l_authors t_authors_by_name;
##REPLACE##
for i in indices of l_authors loop
dbms_output.put_line('l_authors(' || i || ').books_collected = ' || l_authors(i).books_collected);
end loop;
end;
/
正确的选项将会打印出这些结果:
l_authors(Charles Dickens).books_collected = 1
l_authors(Isaac Asimov).books_collected = 10
l_authors(James Joyce).books_collected = 2
l_authors(Jane Austen).books_collected = 1

(A)
cursor c_authors is
select
author_id,
author_name,
books_collected
from quiz_authors;
begin
for r in c_authors loop
l_authors(r.author_name) := r;
end loop;
(B)
begin
select author_id, author_name, books_collected
bulk collect into l_authors
from quiz_authors;
(C)
begin
l_authors := t_authors_by_name(for i in (select author_id, author_name, books_collected from quiz_authors) sequence => i);
(D)
begin
l_authors := t_authors_by_name(for i in (select author_id, author_name, books_collected from quiz_authors) index i.author_name => i);
(E)
cursor c_authors is
select

author_id,

author_name,

books_collected
from quiz_authors;
begin
l_authors := t_authors_by_name(for r in c_authors index r.author_name => r);
(F)
type t_authors is table of quiz_authors%rowtype index by pls_integer;
l_authors_temp t_authors;
begin
l_authors_temp := t_authors(for i in (select author_id, author_name, books_collected from quiz_authors) sequence => i);

l_authors := t_authors_by_name(for v in values of l_authors_temp index v.author_name => v);

(G)
type t_authors is table of quiz_authors%rowtype index by pls_integer;
l_authors_temp t_authors;
begin
select author_id, author_name, books_collected
bulk collect into l_authors_temp
from quiz_authors;

l_authors := t_authors_by_name(for v in values of l_authors_temp index v.author_name => v);
(H)
type t_authors is table of quiz_authors%rowtype index by pls_integer;
l_authors_temp t_authors;
begin
select author_id, author_name, books_collected
bulk collect into l_authors_temp
from quiz_authors;

for i in 1..l_authors_temp.count loop
l_authors(l_authors_temp(i).author_name) := l_authors_temp(i);
end loop;
(I)
l_sql varchar2(1000) := 'select author_id, author_name, books_collected from quiz_authors';
begin
l_authors := t_authors_by_name(for r quiz_authors%rowtype in (execute immediate l_sql) index r.author_name => r);

回复

使用道具 举报

千问 | 2008-9-15 01:28:12 | 显示全部楼层
答案ADEFGHI
A:
一个简单的FOR游标循环可以用来填充关联数组,但是有更好的方法。
B:
利用BULK COLLECT是一种快速填充数组的方法,但是仅当索引是数值型的情况下。
这个选项会报如下的编译错误:
PLS-00657: Implementation restriction: bulk SQL with associative arrays with VARCHAR2 key is not supported.
C:
21C增强了FOR循环迭代器,支持使用SQL语句所定义的游标迭代控制器。
可以使用序列迭代器选择关联设置生成的数组索引,使用 SEQUENCE 将数组索引设置为连续数字。
此选择中的代码似乎将数组索引设置为 (1, 2, 3, 4) 而不是作者姓名。如果 SEQUENCE 值被隐式转换为数组索引的字符值,就会出现这种情况。
实际上这段代码不能编译,编译器认识到 SEQUENCE 的使用与 INDEX BY VARCHAR2 的关联数组不兼容:
PLS-00681: named association syntax is required
如果将此选项中的 [SEQUENCE] 替换为索引迭代器关联选项 [INDEX i.author_id],则编译器会隐式将该数字转换为字符串,从而给出以下(不正确的)的结果:

l_authors(**).books_collected = 1
l_authors(102).books_collected = 2
l_authors(103).books_collected = 1
l_authors(104).books_collected = 10
D:
21C增强了FOR循环迭代器,支持使用静态SQL语句所定义的游标迭代控制器。
在本选项中,此关联数组字符串索引被定义为使用索引迭代器关联选项,其中 INDEX 被设置为数组索引的适当表达式。
E:
显式游标也可用于定义游标迭代控件以定义用于初始化数组的记录。
F:
21c 中的另一个新迭代器功能是能够在一个数组上使用 VALUES OF 作为迭代控件来填充第二个数组。
本选项使用 SQL 语句定义游标迭代控制器,用于填充使用序列迭代器关联选项和 SEQUENCE 索引的临时数组。
第二个数组使用第一个数组的 VALUES OF 作为迭代控件和与 INDEX 表达式的索引迭代器关联选项来填充,以将字符串索引设置为迭代器值的 author_name 元素。

G:
本选项使用 BULK COLLECT 填充临时数组,然后使用 VALUES OF 迭代控制器填充第二个数组。
H:
本选项是在 21c 之前使用 BULK COLLECT 填充由 VARCHAR2 索引的关联数组的方式。

I:
游标迭代控件也可以使用动态 SQL 定义。
因为迭代器的类型在编译时是未知的,所以必须明确指定使用动态 SQL 来定义游标迭代控制器。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行