PL/SQL 记录。我可以记录几个值吗?
PL/SQL Records. Can i put in records few values?
我在 Apex Oracle PL/SQL 工作并试图了解记录的工作原理。
有一段代码。我不明白为什么我会收到“找不到数据”的异常。
我可以在记录中放几行吗?
create table testProducts
(
prod_id number not null,
group_id number (5) not null,
prod_name varchar2(50) not null,
constraint fk_group_id foreign key (group_id)
references testGroups(group_id)
)
declare
type mr is record (
prod_Id testProducts.prod_Id%type
);
obj_mr mr;
maxd number;
egin
select max(prod_Id) into maxd from testProducts;
for i in 10..maxd loop
select testProducts.prod_id into obj_mr from testProducts where
testProducts.prod_Id = i;
dbms_output.put_line(obj_mr.prod_id);
end loop;
end;
为了解决您眼前的问题,您收到 未找到数据 错误,因为您从 prod_id
10 开始循环并按顺序上升到最大值 prod_id
存在于 table 中。如果您的 ID 中有任何空白,则会出现错误。您可以通过这样的错误处理来解决它:
DECLARE
TYPE mr IS RECORD
(
prod_Id testProducts.prod_Id%TYPE,
GROUP_ID testProducts.GROUP_ID%TYPE,
prod_name testProducts.prod_name%TYPE
);
obj_mr mr;
maxd NUMBER;
BEGIN
SELECT MAX (prod_Id) INTO maxd FROM testProducts;
FOR i IN 10 .. NVL (maxd, 1)
LOOP
BEGIN
SELECT prod_id, GROUP_ID, prod_name
INTO obj_mr
FROM testProducts
WHERE prod_Id = i;
DBMS_OUTPUT.put_line (obj_mr.prod_id || '|' || obj_mr.GROUP_ID || '|' || obj_mr.prod_name);
EXCEPTION
WHEN NO_DATA_FOUND
THEN
DBMS_OUTPUT.put_line ('prod_id does not exist: ' || i);
END;
END LOOP;
END;
/
要回答关于多行的问题,RECORD 类型不能有多行,但是 TABLE类型可以。如果您定义 TABLE 类型的 RECORD 类型,那么您可以 select 多行 TABLE类型。有关如何执行此操作的示例,请参见下面的代码。
DECLARE
TYPE mr IS RECORD
(
prod_Id testProducts.prod_Id%TYPE,
GROUP_ID testProducts.GROUP_ID%TYPE,
prod_name testProducts.prod_name%TYPE
);
TYPE mr_t IS TABLE OF mr;
obj_mr_t mr_t;
BEGIN
SELECT prod_id, GROUP_ID, prod_name
BULK COLLECT INTO obj_mr_t
FROM testProducts
ORDER BY prod_id;
FOR i IN 1 .. obj_mr_t.COUNT
LOOP
DBMS_OUTPUT.put_line (
obj_mr_t (i).prod_id || '|' || obj_mr_t (i).GROUP_ID || '|' || obj_mr_t (i).prod_name);
END LOOP;
END;
/
我在 Apex Oracle PL/SQL 工作并试图了解记录的工作原理。 有一段代码。我不明白为什么我会收到“找不到数据”的异常。 我可以在记录中放几行吗?
create table testProducts
(
prod_id number not null,
group_id number (5) not null,
prod_name varchar2(50) not null,
constraint fk_group_id foreign key (group_id)
references testGroups(group_id)
)
declare
type mr is record (
prod_Id testProducts.prod_Id%type
);
obj_mr mr;
maxd number;
egin
select max(prod_Id) into maxd from testProducts;
for i in 10..maxd loop
select testProducts.prod_id into obj_mr from testProducts where
testProducts.prod_Id = i;
dbms_output.put_line(obj_mr.prod_id);
end loop;
end;
为了解决您眼前的问题,您收到 未找到数据 错误,因为您从 prod_id
10 开始循环并按顺序上升到最大值 prod_id
存在于 table 中。如果您的 ID 中有任何空白,则会出现错误。您可以通过这样的错误处理来解决它:
DECLARE
TYPE mr IS RECORD
(
prod_Id testProducts.prod_Id%TYPE,
GROUP_ID testProducts.GROUP_ID%TYPE,
prod_name testProducts.prod_name%TYPE
);
obj_mr mr;
maxd NUMBER;
BEGIN
SELECT MAX (prod_Id) INTO maxd FROM testProducts;
FOR i IN 10 .. NVL (maxd, 1)
LOOP
BEGIN
SELECT prod_id, GROUP_ID, prod_name
INTO obj_mr
FROM testProducts
WHERE prod_Id = i;
DBMS_OUTPUT.put_line (obj_mr.prod_id || '|' || obj_mr.GROUP_ID || '|' || obj_mr.prod_name);
EXCEPTION
WHEN NO_DATA_FOUND
THEN
DBMS_OUTPUT.put_line ('prod_id does not exist: ' || i);
END;
END LOOP;
END;
/
要回答关于多行的问题,RECORD 类型不能有多行,但是 TABLE类型可以。如果您定义 TABLE 类型的 RECORD 类型,那么您可以 select 多行 TABLE类型。有关如何执行此操作的示例,请参见下面的代码。
DECLARE
TYPE mr IS RECORD
(
prod_Id testProducts.prod_Id%TYPE,
GROUP_ID testProducts.GROUP_ID%TYPE,
prod_name testProducts.prod_name%TYPE
);
TYPE mr_t IS TABLE OF mr;
obj_mr_t mr_t;
BEGIN
SELECT prod_id, GROUP_ID, prod_name
BULK COLLECT INTO obj_mr_t
FROM testProducts
ORDER BY prod_id;
FOR i IN 1 .. obj_mr_t.COUNT
LOOP
DBMS_OUTPUT.put_line (
obj_mr_t (i).prod_id || '|' || obj_mr_t (i).GROUP_ID || '|' || obj_mr_t (i).prod_name);
END LOOP;
END;
/