PL/SQL中带有create和cursor的过程



我们能用进行程序吗

首先创建一个表,假设

  create table INCOME_GROUP(income_compare_groups varchar(100)) ;

然后将数据插入该表。

 insert into INCOME_GROUP values (10-20);

然后将此表用作光标。

 CURSOR c1 IS(select *from INCOME_GROUP);

例如,我正在这样做。

BEGIN 
create table INCOME_GROUP(income_compare_groups varchar(100)) ;
DECLARE
   CURSOR c1 IS(select * income_Group);
BEGIN
   FOR acct IN c1 LOOP  -- process each row one at a time
      INSERT INTO temp_test
            VALUES (acct.income_compare_groups);

   END LOOP;
   COMMIT;
END;
END;

但是我犯了一些错误。

ORA-06550: line 2, column 4:
PLS-00103: Encountered the symbol "CREATE" when expecting one of the following:
   ( begin case declare exit for goto if loop mod null pragma
   raise return select update while with <an identifier>
   <a double-quoted delimited-identifier> <a bind variable> <<
   continue close current delete fetch lock insert open rollback
   savepoint set sql execute commit forall merge pipe purge

在阅读评论后,我尝试了这个-

BEGIN 
     EXECUTE IMMEDIATE 'create table INCOME_GROUP
 (
  income_compare_groups varchar(100)
  )'; 
DECLARE
   CURSOR c1 IS
     (select * from
       INCOME_GROUP
    );

BEGIN
 FOR acct IN c1 LOOP  -- process each row one at a time

      INSERT INTO temp_test
            VALUES (acct.income_compare_groups, null);

   END LOOP;
   COMMIT;
END;
END;

但它似乎并没有创建表。!!!!

你可以这样做:

create or replace procedure cpy_inc_comp_grps
as
cur_1 sys_refcursor;
compare_group varchar2(100);
begin
  execute immediate 'create table income_group(income_compare_groups varchar2(100))';
  open cur_1 for 'select income_compare_groups from income_group';
  LOOP
  FETCH cur_1 INTO compare_group;
    DBMS_OUTPUT.PUT_LINE('INSERT INTO temp_test VALUES (rec.income_compare_groups');
  EXIT WHEN cur_1%NOTFOUND;
  END LOOP;
  close cur_1;
  execute immediate 'drop table income_group';
end;

并使用以下代码进行测试:

begin
  cpy_inc_comp_grps;
end;

你必须用你想做的任何插入件替换dbms_output.put_line(...)部件。

它必须是这样的:

DECLARE
    cur SYS_REFCURSOR;
     v_income_compare_groups VARCHAR(100);
BEGIN
    EXECUTE IMMEDIATE 'CREATE TABLE INCOME_GROUP(income_compare_groups VARCHAR(100))';
    OPEN cur FOR 'SELECT * income_Group';
    LOOP
        FETCH cur INTO v_income_compare_groups;
        EXIT WHEN cur%NOTFOUND;
        INSERT INTO temp_test VALUES (v_income_compare_groups);
    END LOOP;
    CLOSE cur;
    COMMIT;
END;

您必须使用动态Cursor,因为当您编译包时,表INCOME_GROUP还不存在,并且在CURSOR c1 IS(select * income_Group); 处会出现错误

然而,有几个问题:如果表已经存在,则会出现错误。你必须先检查一下,或者写一个异常处理程序。这个过程是无用的,因为你首先创建一个(空)表,然后选择它——它永远不会选择任何东西!

试试这个。

execute immediate 'create table INCOME_GROUP(income_compare_groups varchar(100))';

相关内容

  • 没有找到相关文章

最新更新