在我的PL / SQL脚本中,当它表示联接时,如何声明JUSTIFIC_REC?
SELECT *
INTO JUSTIFIC_REC
FROM TABLE1 A
INNER JOIN TABLE2 B
ON A.ID_JUSTIFIC = B.ID_JUSTIFIC ;
我想要做的就是将一个连续的行插入到TABLE3中:
INSERT INTO MOF_OUTACCDTL_REQ VALUES(
JUSTIFIC_rec.ENTRY_COMMENTS || ' ' || JUSTIFIC_rec.DESCRIPTION );
JUSTIFIC_REC的声明应该如何像我脚本的开头一样?
如果不是INNER JOIN,我会写类似: JUSTIFIC_rec TABLE1%ROWTYPE;
答案 0 :(得分:4)
如果我正确理解,可以尝试使用游标行类型,例如这样(不确定通过联接声明select的变量类型是否意味着这个意思)
set serveroutput on;
declare
cursor cur is
SELECT ENTRY_COMMENTS, DESCRIPTION
FROM TABLE1 A
INNER JOIN TABLE2 B
ON A.ID_JUSTIFIC = B.ID_JUSTIFIC ;
justific_rec cur%ROWTYPE;
begin
open cur;
loop
fetch cur into justific_rec;
exit when cur%notfound;
dbms_output.put_line(justific_rec.entry_comments || ' ' || justific_rec.description);
end loop;
close cur;
end;
答案 1 :(得分:1)
问题的答案本身就是问题。您必须使用%row类型 拖曳类型属性可以是以下任何一种类型:
rowtype_attribute :=
{cursor_name | cursor_variable_name | table_name} % ROWTYPE
光标名称:- 先前在当前范围内声明的显式游标。
cursor_variable_name:- 一个PL / SQL强类型游标变量,以前在当前作用域内声明过。
表名称:- 详细说明声明时必须可访问的数据库表或视图。
所以代码看起来像
DECLARE
CURSOR c1 IS
SELECT * FROM TABLE1 A
INNER JOIN TABLE2 B
ON A.ID_JUSTIFIC = B.ID_JUSTIFIC ;
justific_rec c1%ROWTYPE;
BEGIN
open c1;
loop
fetch c1 into justific_rec;
exit when c1%notfound;
INSERT INTO MOF_OUTACCDTL_REQ VALUES(
JUSTIFIC_rec.ENTRY_COMMENTS || ' ' || JUSTIFIC_rec.DESCRIPTION );
end loop;
close c1;
END;
/
答案 2 :(得分:0)
您需要使用BULK COLLECT INTO
SELECT *
BULK COLLECT INTO JUSTIFIC_REC
FROM TABLE1 A
INNER JOIN TABLE2 B
ON A.ID_JUSTIFIC = B.ID_JUSTIFIC ;
JUSTIFIC_REC
必须是具有适当列的TABLE类型
答案 3 :(得分:0)
如果您要做的只是基于select语句插入表中,则无需使用PL / SQL(这意味着您无需打开游标,获取一行,处理该行然后移至下一行)-这就是逐行又是缓慢的处理。
相反,您可以在单个插入语句中完成所有操作,例如:
INSERT INTO mof_outaccdtl_rec (<column being inserted into>) -- please always list the columns being inserted into; this avoids issues with your code when someone adds a column to the table.
SELECT entry_comments || ' ' || description -- please alias your columns! How do we know which table each column came from?
FROM table1 a
inner join table2 b on a.id_justific = b.id_justific;
如果您想将此插入语句嵌入到PL / SQL中,则只需在其周围添加一个开始/结束,就像这样:
begin
INSERT INTO mof_outaccdtl_rec (<column being inserted into>) -- please always list the columns being inserted into; this avoids issues with your code when someone adds a column to the table.
SELECT entry_comments || ' ' || description -- please alias your columns! How do we know which table each column came from?
FROM table1 a
inner join table2 b on a.id_justific = b.id_justific;
end;
/
当使用数据库时,这是首选的解决方案-不按程序考虑集(如果可能)(也就是逐行处理)。维护起来更容易,代码也更容易读写,并且由于不必在每一行的PL / SQL和SQL之间进行多次切换,因此代码性能更高。
上下文切换对性能不利-考虑盛满水的浴缸-用勺子(逐行处理),水罐(分批的行-分批的行)清空浴池是否更快? )或拔出插头(基于套件)?