Two PLSQL statements with begin and end, run fine separately but not together?

前端 未结 3 1148
旧巷少年郎
旧巷少年郎 2020-12-18 02:47

Just wondering if anyone can help with this, I have two PLSQL statements for altering tables (adding extra fields) and they are as follows:

-- Make GC_NAB fi         


        
3条回答
  •  悲哀的现实
    2020-12-18 02:56

    Oracle can take one SQL statement or PL/SQL anonymous block at a time. (Unlike SQL Server that can except a batch at a time.) So, you have a couple of options.

    1. Wrap the two anonymous blocks within another anonymous block:

      begin
        -- Make GC_NAB field for Next Action By Dropdown 
        begin 
        if 'VARCHAR2' = 'NUMBER' and length('VARCHAR2')>0 and length('')>0 then 
          execute immediate 'alter table "SERVICEMAIL6"."ETD_GUESTCARE" add(GC_NAB VARCHAR2(10, ))'; 
        elsif ('VARCHAR2' = 'NUMBER' and length('VARCHAR2')>0 and length('')=0) or 
          'VARCHAR2' = 'VARCHAR2' then 
          execute immediate 'alter table "SERVICEMAIL6"."ETD_GUESTCARE" add(GC_NAB VARCHAR2(10))'; 
        else 
          execute immediate 'alter table "SERVICEMAIL6"."ETD_GUESTCARE" add(GC_NAB VARCHAR2)'; 
        end if; 
        commit; 
        end; 
        -- Make GC_NABID field for Next Action By Dropdown 
        begin 
        if 'NUMBER' = 'NUMBER' and length('NUMBER')>0 and length('')>0 then 
          execute immediate 'alter table "SERVICEMAIL6"."ETD_GUESTCARE" add(GC_NABID NUMBER(, ))'; 
        elsif ('NUMBER' = 'NUMBER' and length('NUMBER')>0 and length('')=0) or 
          'NUMBER' = 'VARCHAR2' then 
          execute immediate 'alter table "SERVICEMAIL6"."ETD_GUESTCARE" add(GC_NABID NUMBER())'; 
        else 
          execute immediate 'alter table "SERVICEMAIL6"."ETD_GUESTCARE" add(GC_NABID NUMBER)'; 
        end if; 
        commit; 
        end;
      end;
      
    2. Tell the tool you are using to submit the PL/SQL to Oracle to send the two block seperately. How to do this will be tool specific. In SQL*PLUS, a / on a line by itself will accomplish this:

        -- Make GC_NAB field for Next Action By Dropdown 
        begin 
        if 'VARCHAR2' = 'NUMBER' and length('VARCHAR2')>0 and length('')>0 then 
          execute immediate 'alter table "SERVICEMAIL6"."ETD_GUESTCARE" add(GC_NAB VARCHAR2(10, ))'; 
        elsif ('VARCHAR2' = 'NUMBER' and length('VARCHAR2')>0 and length('')=0) or 
          'VARCHAR2' = 'VARCHAR2' then 
          execute immediate 'alter table "SERVICEMAIL6"."ETD_GUESTCARE" add(GC_NAB VARCHAR2(10))'; 
        else 
          execute immediate 'alter table "SERVICEMAIL6"."ETD_GUESTCARE" add(GC_NAB VARCHAR2)'; 
        end if; 
        commit; 
        end; 
        /
        -- Make GC_NABID field for Next Action By Dropdown 
        begin 
        if 'NUMBER' = 'NUMBER' and length('NUMBER')>0 and length('')>0 then 
          execute immediate 'alter table "SERVICEMAIL6"."ETD_GUESTCARE" add(GC_NABID NUMBER(, ))'; 
        elsif ('NUMBER' = 'NUMBER' and length('NUMBER')>0 and length('')=0) or 
          'NUMBER' = 'VARCHAR2' then 
          execute immediate 'alter table "SERVICEMAIL6"."ETD_GUESTCARE" add(GC_NABID NUMBER())'; 
        else 
          execute immediate 'alter table "SERVICEMAIL6"."ETD_GUESTCARE" add(GC_NABID NUMBER)'; 
        end if; 
        commit; 
        end;
        /
      

提交回复
热议问题