Running loop in script in sql plus

房东的猫 提交于 2019-12-24 00:22:58

问题


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

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"

Where am I going wrong?


回答1:


Try to add / after end; as below:

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;



回答2:


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



回答3:


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;


来源:https://stackoverflow.com/questions/13565093/running-loop-in-script-in-sql-plus

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!