[英]PL-SQL explicit cursors
I am trying to write a PL?SQL statement for oracle that selects all unique Industries from a table (Jobs Table) and inserts it into another table called dim_industry
我正在尝试为oracle编写PL?SQL语句,该语句从一个表(作业表)中选择所有唯一的行业并将其插入另一个名为
dim_industry
表中
DECLARE
cursor industries is select unique industry from job;
ind varchar2(20);
BEGIN
open industries;
for counter in 1..industries%ROWCOUNT
LOOP
FETCH industries into ind;
insert into dim_industry (IndustryName) values(ind);
END LOOP;
close industries;
END;
/
The 的
select unique industry from job
Selects 10 rows, but when I run the pl-sql, it says 1 row inserted. 选择10行,但是当我运行pl-sql时,它说已插入1行。 Also, when I do a
另外,当我做一个
select * from dim_industry
query, the table remains empty. 查询时,该表保持为空。 Any ideas as to why this would happen?
有什么想法为什么会发生这种情况?
All the previous answers are improvements - and BULK COLLECTION is a very useful tool. 以前所有的答案都是改进-批量收集是一个非常有用的工具。 But you generally don't need to use PL/SQL to copy data around.
但是您通常不需要使用PL / SQL来复制数据。 In your case, try just using an INSERT ... SELECT statement like so :-
在您的情况下,请尝试仅使用INSERT ... SELECT语句,例如:
INSERT INTO dim_industry( IndustryName )
SELECT DISTINCT industry
FROM job
PL/SQL is a last resort IMHO. PL / SQL是IMHO的不得已的手段。 I've even implemented constraint solving in straight Oracle SQL!
我什至已经在直接的Oracle SQL中实现了约束解决!
ENDLOOP
should be written as END LOOP
ENDLOOP
应该写为END LOOP
UPDATE 更新
To achieve what you want, You can proceed like this: 要实现您想要的,您可以像这样进行:
DECLARE
cursor industries is select unique industry from job;
ind varchar2(20);
BEGIN
open industries;
LOOP
FETCH industries into ind;
exit when industries %notfound;
insert into dim_industry (IndustryName) values(ind);
END LOOP;
close industries;
commit;
END;
/
skip that step: 跳过该步骤:
DECLARE
TYPE T_IND IS TABLE of job.industry%TYPE;
ind T_IND;
BEGIN
SELECT UNIQUE industry BULK COLLECT INTO ind FROM job;
FORALL i IN ind.FIRST..ind.LAST
INSERT INTO dim_industry (IndustryName) VALUES (ind(i));
-- don't forget to commit;
END;
/
hey i think your doing in other route which is quite lengthy!!!just try this dude 嘿,我认为您在其他路线上所做的时间很长!
DECLARE
CURSOR c1
iS
select unique industry from job;
BEGIN
FOR i IN c1
LOOP
INSERT INTO dim_industry (IndustryName) VALUES
(i.industry
);
END LOOP;
commit;
END;
/
if you use for loop
for cursor then no need mention open and close
it has implicitly open and close. 如果对光标使用
for loop
,则无需提及open and close
它已隐式打开和关闭。 note:if you want to update or insert of delete operation on table using cursor better user collections with bulk operation it's much faster than this. 注意:如果要使用具有更好操作的光标更好的用户集合对表进行更新或插入删除操作,则比这要快得多。
this is second way to do the same; 这是做同样事情的第二种方法;
DECLARE
temp HR.departments.department_name%type;
CURSOR c1
iS
SELECT DISTINCT d.department_name FROM hr.departments d;
BEGIN
open c1;
LOOP
fetch c1 into temp;
INSERT INTO thiyagu.temp_dep VALUES
(temp
);
dbms_output.put_line(temp);
exit when c1%notfound;
END LOOP;
close c1;
commit;
END;
/
this is third and efficient way; 这是第三种有效的方式;
DECLARE
type test_type is table of job.industry%type;
col test_type;
BEGIN
select unique industry bulk collect into col from job;
forall i in col.first..col.last insert into dim_industry values(col(i));
dbms_output.put_line(sql%rowcount);
commit;
END;
/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.