两个包含begin和end的PLSQL语句,单独运行但不在一起?

时间:2010-06-11 16:18:36

标签: oracle plsql oracle10g statements

只是想知道是否有人可以帮助解决这个问题,我有两个用于修改表的PLSQL语句(添加额外的字段),它们如下:

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

当我单独运行这两个查询时,没有问题。但是,当如上所示一起运行时,Oracle在启动第二个语句时给出了一个错误:

Error report:
ORA-06550: line 15, column 1:
PLS-00103: Encountered the symbol "BEGIN" 
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:

我认为这意味着第一个陈述没有正确终止......我是否应该在陈述之间插入任何内容以使其正常工作?

3 个答案:

答案 0 :(得分:17)

Oracle一次可以使用一个SQL语句或PL / SQL匿名块。 (与SQL Server不同,除了一次批处理外。)因此,您有几个选择。

  1. 将两个匿名块包装在另一个匿名块中:

    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. 告诉您正在使用的工具将PL / SQL提交给Oracle,以便单独发送两个块。如何做到这一点将是特定工具。在SQL * PLUS中,一行上的/将完成此任务:

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

答案 1 :(得分:8)

是的,每个/后,您需要在该行上设置斜杠end;

答案 2 :(得分:0)

可以通过两种方式实现

1)在pl / sql文件中分别运行两个块。

2)在主块中你可以用这种方式运行两个sbu-block:

library(dplyr)
library(splitstackshape)
# Convert to data.table, adding rownames, and making a copy
DT <- copy(setDT(mydf, keep.rownames = TRUE))

DT %>%
  cSplit("info", "/") %>%             # split "info" column by "/"
  cSplit("info_3", ",", "long") %>%   # split third output from above, long format, by ","
  cSplit("info_3", "=") %>%           # split same column again, wide format, by "="
  group_by(rn) %>%                    # group by row names   
  summarise(time = time[1],           # first value of time...
            val = sum(info_3_2)) %>%  # sum of values from "info_3_2"
  mutate(local = time - val) %>%      # calculate the difference
  left_join(mydf)                     # merge with original data
# Source: local data table [7 x 6]
# 
#      rn   time   val  local                                             info   path
#   (chr)  (dbl) (dbl)  (dbl)                                            (chr)  (chr)
# 1 16940 0.4500 0.328 0.1220 0/0/DNSMX=0.018,DNSMX=0.037,PASSH=0.14,GEO=0.133 /test/
# 2 17151 0.0314    NA     NA                                            0/0/- /test/
# 3 17162 0.2442 0.140 0.1042                           0/0/DNSMX=0,PASSH=0.14 /test/
# 4 23112 0.3962 0.279 0.1172                 0/0/DNSMX=0,PASSH=0.159,GEO=0.12 /test/
# 5 30400 0.0080 0.000 0.0080                                  12035/0/DNSMX=0 /test/
# 6 39649 0.0218 0.000 0.0218                                  11340/0/DNSMX=0 /test/
# 7  6238 0.0027    NA     NA                                            0/0/- /test/