2013-03-16 38 views
1

我试图写甲骨文从一个表(职位表)选择所有的特色产业,并把它插入到另一个表中的PL?SQL语句称为dim_industryPL-SQL显式游标

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; 
/

select unique industry from job 

选择10行,但是当我运行pl-sql时,它表示插入了1行。此外,当我做一个

select * from dim_industry 

查询,该表保持空。任何想法为什么会发生?

+0

老兄,如果你这样做与收集和批量相同的情况下它更有效率yar – 2013-03-17 05:05:54

+0

做一个简单的SQL“insert into ... select ...”语句将是最好的方法。 – 2013-03-18 11:46:19

回答

0

ENDLOOP应写为END LOOP
UPDATE
达到你想要什么,你可以这样进行:

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; 
/
+0

谢谢。 pl-sql现在运行。然而,它并没有给我我期望的结果。我正在修改这个问题,如果你再看一遍,我将不胜感激。 – 2013-03-16 18:30:32

+0

@ ErnestTambie:看到更新的答案.. – 2013-03-16 18:56:57

+0

谢谢,这个作品完美。只需确认一下,在这一行中:当行业%未找到时退出; 当你到达行的末尾时,%notfound属性有错误吗? – 2013-03-16 19:16:27

1

跳过这一步:

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; 
/
+0

你好,谢谢你的回复,这也适用:) – 2013-03-16 19:19:43

+1

不客气!是的,这是有效的,并且在性能方面速度更快。 – Sebas 2013-03-16 19:25:56

+0

-1为一个更复杂的解决方案,而不仅仅是使用SQL – 2013-03-18 11:48:12

-1

嘿,我想你在其他很长的路线上做的!!!只是试试这个家伙

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; 
/

如果您使用for loop作为游标,则不需要提及open and close它已隐式地打开和关闭。 注意:如果你想更新或插入使用游标的表上的删除操作更好的用户集合批量操作它比这更快。

这是做同样的第二种方法;

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; 
/

这是第三和有效的方式;

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; 
/
+0

如果您推荐BULK COLLECT/FORALL,您还应该提及LIMIT子句以及如何使用它。如果您的选择返回大量行,则执行批量收集可能会轻松炸毁您的流程内存。 – 2013-03-17 17:29:22

+0

@ Frank Schmitt:很明显,但在这种情况下,我们只处理10条记录!但我应该把!kk dude – 2013-03-18 06:41:03

+0

-1作为一个更复杂的解决方案,而不仅仅是使用SQL – 2013-03-18 11:48:33

4

以前所有的答案都改进 - 而大量聚集是一个非常有用的工具。但是你通常不需要使用PL/SQL来复制数据。在你的情况下,尝试使用像这样的INSERT ... SELECT语句: -

INSERT INTO dim_industry(IndustryName) 
SELECT DISTINCT industry 
FROM job 

PL/SQL是最后的手段恕我直言。我甚至在直接的Oracle SQL中实现了约束求解!

+1

+1这是迄今为止最明智的方法 – 2013-03-17 17:26:36

+0

谢谢,我没有意识到你可以通过select语句插入多行 – 2013-04-23 01:01:36