两个带有 begin 和 end 的 PLSQL 语句单独运行良好但不能一起运行?

发布于 2024-09-05 11:43:31 字数 1485 浏览 6 评论 0原文

只是想知道是否有人可以帮助解决这个问题,我有两个用于更改表(添加额外字段)的 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:

我假设这意味着第一个语句没有正确终止...是否有什么我应该在语句之间放入的内容它工作正常吗?

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

When I run these two queries separately, there are no problems. However, when run together as shown above, Oracle gives me an error when it starts the second statement:

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:

I'm assuming that this means the first statement is not terminated properly... is there anything I should put in between the statements to make it work properly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

她比我温柔 2024-09-12 11:43:31

Oracle 一次可以获取一个 SQL 语句或 PL/SQL 匿名块。 (与 SQL Server 不同,SQL Server 可以一次排除一批。)因此,您有几个选择。

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

    <前><代码>开始
    -- 为下拉列表中的下一步操作创建 GC_NAB 字段
    开始
    如果 'VARCHAR2' = 'NUMBER' 且长度('VARCHAR2')>0 且长度('')>0 则
    立即执行 'alter table "SERVICEMAIL6"."ETD_GUESTCARE" add(GC_NAB VARCHAR2(10, ))';
    elsif ('VARCHAR2' = 'NUMBER' and length('VARCHAR2')>0 and length('')=0) 或
    'VARCHAR2' = 'VARCHAR2' 那么
    立即执行 'alter table "SERVICEMAIL6"."ETD_GUESTCARE" add(GC_NAB VARCHAR2(10))';
    别的
    立即执行 'alter table "SERVICEMAIL6"."ETD_GUESTCARE" add(GC_NAB VARCHAR2)';
    结束如果;
    犯罪;
    结尾;
    -- 为下拉列表中的下一步操作创建 GC_NABID 字段
    开始
    如果 'NUMBER' = 'NUMBER' 且长度('NUMBER')>0 且长度('')>0 则
    立即执行 'alter table "SERVICEMAIL6"."ETD_GUESTCARE" add(GC_NABID NUMBER(, ))';
    elsif ('NUMBER' = 'NUMBER' and length('NUMBER')>0 and length('')=0) 或
    'NUMBER' = 'VARCHAR2' 那么
    立即执行 'alter table "SERVICEMAIL6"."ETD_GUESTCARE" add(GC_NABID NUMBER())';
    别的
    立即执行 'alter table "SERVICEMAIL6"."ETD_GUESTCARE" add(GC_NABID NUMBER)';
    结束如果;
    犯罪;
    结尾;
    结尾;

  2. 告诉您用来将 PL/SQL 提交到 Oracle 的工具分别发送这两个块。如何做到这一点将取决于工具。在 SQL*PLUS 中,单独一行上的 / 将完成此操作:

     -- 为下拉列表中的下一步操作创建 GC_NAB 字段 
      开始 
      如果 'VARCHAR2' = 'NUMBER' 且长度('VARCHAR2')>0 且长度('')>0 则 
        立即执行 'alter table "SERVICEMAIL6"."ETD_GUESTCARE" add(GC_NAB VARCHAR2(10, ))'; 
      elsif ('VARCHAR2' = 'NUMBER' and length('VARCHAR2')>0 and length('')=0) 或 
        'VARCHAR2' = 'VARCHAR2' 那么 
        立即执行 'alter table "SERVICEMAIL6"."ETD_GUESTCARE" add(GC_NAB VARCHAR2(10))'; 
      别的 
        立即执行 'alter table "SERVICEMAIL6"."ETD_GUESTCARE" add(GC_NAB VARCHAR2)'; 
      结束如果; 
      犯罪; 
      结尾; 
      /
      -- 为下拉列表中的下一步操作创建 GC_NABID 字段 
      开始 
      如果 'NUMBER' = 'NUMBER' 且长度('NUMBER')>0 且长度('')>0 则 
        立即执行 'alter table "SERVICEMAIL6"."ETD_GUESTCARE" add(GC_NABID NUMBER(, ))'; 
      elsif ('NUMBER' = 'NUMBER' and length('NUMBER')>0 and length('')=0) 或 
        'NUMBER' = 'VARCHAR2' 那么 
        立即执行 'alter table "SERVICEMAIL6"."ETD_GUESTCARE" add(GC_NABID NUMBER())'; 
      别的 
        立即执行 'alter table "SERVICEMAIL6"."ETD_GUESTCARE" add(GC_NABID NUMBER)'; 
      结束如果; 
      犯罪; 
      结尾;
      /
    

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;
      /
    
不必你懂 2024-09-12 11:43:31

是的,您需要在每个 end; 后面的行上添加斜杠 /

Yes, you need a slash / on the line after each end;.

記柔刀 2024-09-12 11:43:31

可以通过两种方式实现:

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

2)在主块中,您可以通过以下方式运行两个 sbu 块:

begin  // main block
      begin 
       .....   [pl/sql commands]
       ......
      end
      begin   
      .......  [pl/sql commands]
      .......
      end
end // main ends

one can achieve in two ways

1) run the two blocks separately in pl/sql file.

2) in main block you can run two sbu-blocks in this way :

begin  // main block
      begin 
       .....   [pl/sql commands]
       ......
      end
      begin   
      .......  [pl/sql commands]
      .......
      end
end // main ends
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文