oracle 在 sql plus 脚本中运行循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13565093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 01:17:10  来源:igfitidea点击:

Running loop in script in sql plus

oraclesqlplus

提问by user1844205

I am running a script in sql plus, I have a for loop in my script:

我在 sql plus 中运行一个脚本,我的脚本中有一个 for 循环:

BEGIN
  FOR count IN 1..100 LOOP
    INSERT INTO CompanyShare VALUES (count, 1, 250);
  END LOOP;
END;

BEGIN
  FOR count IN 101..200 LOOP
    INSERT INTO CompanyShare VALUES (count, 2, 50);
  END LOOP;
END;

When I run the script this error came up:

当我运行脚本时,出现了这个错误:

ORA-06550: line 6, column 1: PLS-00103: Encountered the symbol "BEGIN"

ORA-06550:第 6 行,第 1 列:PLS-00103:遇到符号“BEGIN”

Where am I going wrong?

我哪里错了?

回答by Robert

Try to add /after end;as below:

尝试添加/end;如下图所示:

BEGIN
  FOR count IN 1..100 LOOP
    INSERT INTO CompanyShare VALUES (count, 1, 250);
  END LOOP;
END;
/ --<-- Here
BEGIN
  FOR count IN 101..200 LOOP
    INSERT INTO CompanyShare VALUES (count, 2, 50);
  END LOOP;
END;

回答by Gaurav Soni

Looking to your logic ,you can even simplify the script based on condition .

看看你的逻辑,你甚至可以根据条件简化脚本。

  BEGIN
  FOR count IN 1..200
   LOOP
    INSERT INTO CompanyShare VALUES (count
                                    ,CASE WHEN count<=100 THEN 1   ELSE 2  END
                                    ,CASE WHEN count<=100 THEN 250 ELSE 50 END
                                    );
   END LOOP;
  END;
  /

回答by Viru

Semicolon is not there after end .Try this

结束后没有分号。试试这个

BEGIN
  FOR count IN 1..100 LOOP
   INSERT INTO CompanyShare VALUES (count, 1, 250);
  END LOOP;
END ;   --****
BEGIN
 FOR count IN 101..200 LOOP
  INSERT INTO CompanyShare VALUES (count, 2, 50);
 END LOOP;
END;