• Oracle数据库----第七周实验____循环与游标


    目录

    Oracle数据库----第七周实验

    循环与游标


    Oracle数据库----第七周实验


    循环与游标

    1. 循环与游标
    2.  循环
    3.  首先设置显示输出结果
    4. Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.3.0
    5. Connected as system@ORCL
    6. SQL> set serveroutput on;
    7. 1.简单循环
    8. 9-11.利用简单循环求1-100之间偶数的和。
    9. SQL>
    10. SQL> declare
    11. 2 v_counter binary_integer :=1;
    12. 3 v_sum number :=0;
    13. 4 begin
    14. 5 loop
    15. 6 if mod(v_counter,2)=0 then
    16. 7 v_sum:=v_sum+v_counter;
    17. 8 end if;
    18. 9 v_counter :=v_counter +1;
    19. 10 exit when v_counter>100;
    20. 11 end loop;
    21. 12 dbms_output.put_line(v_sum);
    22. 13 end;
    23. 14
    24. 15 /
    25. 2550
    26. PL/SQL procedure successfully completed
    27. SQL>
    28. 2.while循环
    29. SQL>
    30. SQL> declare
    31. 2 v_counter binary_integer :=1;
    32. 3 v_sum number :=0;
    33. 4 begin
    34. 5 while v_counter<100 loop
    35. 6 if mod(v_counter,2)=0 then
    36. 7 v_sum :=v_sum+v_counter;
    37. 8 end if;
    38. 9 v_counter:=v_counter+1;
    39. 10 end loop;
    40. 11 Dbms_Output.put_line(v_sum);
    41. 12 end;
    42. 13 /
    43. 2450
    44. PL/SQL procedure successfully completed
    45. 3for循环
    46. SQL>
    47. SQL> declare
    48. 2 v_sum number :=0;
    49. 3 begin
    50. 4 for v_counter in 1..100 loop
    51. 5 if mod(v_counter,2)=0 then
    52. 6 v_sum :=v_sum+v_counter;
    53. 7 end if;
    54. 8 end loop;
    55. 9 Dbms_Output.put_line(v_sum);
    56. 10 end;
    57. 11 /
    58. 运行结果:
    59. 2550
    60. PL/SQL procedure successfully completed
    61. 4跳转结构
    62. 先创建一个表:
    63. temp_table
    64. create table temp_table
    65. (
    66. v_counter varchar2(255),
    67. v_sum varchar2(255)
    68. )
    69. SQL>
    70. SQL> create table temp_table
    71. 2 (
    72. 3 v_counter varchar2(255),
    73. 4 v_sum varchar2(255)
    74. 5 )
    75. 6 /
    76. Table created
    77. SQL>
    78. SQL> declare
    79. 2 v_counter binary_integer :=1;
    80. 3 begin
    81. 4 <<label>>
    82. 5 insert into temp_table values(v_counter,'loop index');
    83. 6 v_counter :=v_Counter+1;
    84. 7 if v_counter<=50 then
    85. 8 goto label;
    86. 9 end if;
    87. 10 end;
    88. 11 /
    89. PL/SQL procedure successfully completed
    90. SQL>
    91.  游标
    92. 9-15
    93. 9-15.根据输入的部门号查询某个部门的员工信息,部门号在程序运行时指定。
    94. SQL>
    95. SQL> declare
    96. 2 v_deptno hr.employees.department_id%type;
    97. 3 cursor c_emp is select * from hr.employees where department_id=v_deptno;
    98. 4 v_emp c_emp%rowtype;
    99. 5 begin
    100. 6 v_deptno :=&x;
    101. 7 open c_emp;
    102. 8 loop
    103. 9 fetch c_emp into v_emp;
    104. 10 exit when c_emp%notfound;
    105. 11 dbms_output.put_line(v_emp.employee_id||' '||v_emp.first_name||' '||v_emp.last_name||' '||v_emp.salary||' '||v_deptno);
    106. 12 end loop;
    107. 13 close c_emp;
    108. 14 end;
    109. 15 /
    110. 弹出输入框:
    111. 输入x的值:100
    112. 则, 运行结果为:
    113. 108 Nancy Greenberg 12000 100
    114. 109 Daniel Faviet 9000 100
    115. 110 John Chen 8200 100
    116. 111 Ismael Sciarra 7700 100
    117. 112 Jose Manuel Urman 7800 100
    118. 113 Luis Popp 6900 100
    119. PL/SQL procedure successfully completed
    120. SQL>
    121. 9-16
    122. 利用简单循环统计并输出各个部门的平均工资
    123. SQL>
    124. SQL> declare
    125. 2 cursor c_dept_stat is
    126. 3 select hr.employees.department_id,avg(hr.employees.salary) avgsal from hr.employees group by
    127. 4 department_id;
    128. 5 v_dept c_dept_stat%rowtype;
    129. 6 begin
    130. 7 open c_dept_stat;
    131. 8 loop
    132. 9 fetch c_dept_stat into v_dept;
    133. 10 exit when c_dept_stat%notfound;
    134. 11 Dbms_Output.put_line(v_dept.department_id||' '||v_dept.avgsal);
    135. 12 end loop;
    136. 13 close c_dept_stat;
    137. 14 end;
    138. 15 /
    139. 运行结果:
    140. 100 8600
    141. 30 4150
    142. 7000
    143. 20 9500
    144. 70 10000
    145. 90 19333.3333333333333333333333333333333333
    146. 110 10150
    147. 50 3475.555555555555555555555555555555555556
    148. 40 6500
    149. 80 8955.882352941176470588235294117647058824
    150. 10 4400
    151. 60 5760
    152. PL/SQL procedure successfully completed
    153. SQL>
    154. 9-17
    155. 利用WHILE循环统计并输出各个部门的平均工资
    156. SQL>
    157. SQL> declare
    158. 2 cursor c_dept_stat is
    159. 3 select hr.employees.department_id,avg(hr.employees.salary) avgsal from hr.employees group by
    160. 4 department_id;
    161. 5 v_dept c_dept_stat%rowtype;
    162. 6 begin
    163. 7 open c_dept_stat;
    164. 8 fetch c_dept_stat into v_dept;
    165. 9 while c_dept_stat%found loop
    166. 10 Dbms_Output.put_line(v_dept.department_id||' '||v_dept.avgsal);
    167. 11 fetch c_dept_stat into v_dept;
    168. 12 end loop;
    169. 13 close c_dept_stat;
    170. 14 end;
    171. 15 /
    172. 100 8600
    173. 30 4150
    174. 7000
    175. 20 9500
    176. 70 10000
    177. 90 19333.3333333333333333333333333333333333
    178. 110 10150
    179. 50 3475.555555555555555555555555555555555556
    180. 40 6500
    181. 80 8955.882352941176470588235294117647058824
    182. 10 4400
    183. 60 5760
    184. PL/SQL procedure successfully completed
    185. SQL>
    186. 9-18
    187. 利用FOR循环统计并输出各个部门的平均工资
    188. SQL>
    189. SQL> declare
    190. 2 cursor c_dept_stat is
    191. 3 select hr.employees.department_id,avg(hr.employees.salary) avgsal from hr.employees group by
    192. 4 department_id;
    193. 5 v_dept c_dept_stat%rowtype;
    194. 6 begin
    195. 7 FOR v_dept IN (
    196. 8 select hr.employees.department_id,avg(hr.employees.salary) avgsal from hr.employees group by
    197. 9 department_id
    198. 10 ) LOOP
    199. 11 Dbms_Output.put_line(v_dept.department_id||' '||v_dept.avgsal);
    200. 12 end loop;
    201. 13 end;
    202. 14 /
    203. 100 8600
    204. 30 4150
    205. 7000
    206. 20 9500
    207. 70 10000
    208. 90 19333.3333333333333333333333333333333333
    209. 110 10150
    210. 50 3475.555555555555555555555555555555555556
    211. 40 6500
    212. 80 8955.882352941176470588235294117647058824
    213. 10 4400
    214. 60 5760
    215. PL/SQL procedure successfully completed
    216. SQL>

    测试全程代码

    1. Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.3.0
    2. Connected as system@ORCL
    3. SQL> set serveroutput on;
    4. SQL>
    5. SQL> DECLARE
    6. 2 v_counter BINARY_INTEGER :=1;
    7. 3 v_sum NUMBER :=0;
    8. 4 BEGIN
    9. 5 LOOP
    10. 6 IF mod(v_counter,2)=0 THEN
    11. 7 v_sum :=v_sum+v_counter
    12. 8 END IF
    13. 9 v_counter :=v_counter+1;
    14. 10 EXIT WHEN v_counter>100;
    15. 11 END LOOP
    16. 12 DBMS_OUTPUT.PUT_LINE(v_sum);
    17. 13 END;
    18. 14
    19. 15 /
    20. DECLARE
    21. v_counter BINARY_INTEGER :=1;
    22. v_sum NUMBER :=0;
    23. BEGIN
    24. LOOP
    25. IF mod(v_counter,2)=0 THEN
    26. v_sum :=v_sum+v_counter
    27. END IF
    28. v_counter :=v_counter+1;
    29. EXIT WHEN v_counter>100;
    30. END LOOP
    31. DBMS_OUTPUT.PUT_LINE(v_sum);
    32. END;
    33. ORA-06550: 第 9 行, 第 3 列:
    34. PLS-00103: 出现符号 "END"在需要下列之一时:
    35. . ( * @ % & = - + ; < / >
    36. at in is mod remainder not rem <an exponent (**)>
    37. <> or != or ~= >= <= <> and or like LIKE2_ LIKE4_ LIKEC_
    38. between || member SUBMULTISET_
    39. SQL>
    40. SQL> DECLARE
    41. 2 v_counter BINARY_INTEGER :=1;
    42. 3 v_sum NUMBER :=0;
    43. 4 BEGIN
    44. 5 LOOP
    45. 6 IF mod(v_counter,2)=0 THEN
    46. 7 v_sum :=v_sum+v_counter
    47. 8 END IF;
    48. 9 v_counter :=v_counter+1;
    49. 10 EXIT WHEN v_counter>100;
    50. 11 END LOOP
    51. 12 DBMS_OUTPUT.PUT_LINE(v_sum);
    52. 13 END;
    53. 14 /
    54. DECLARE
    55. v_counter BINARY_INTEGER :=1;
    56. v_sum NUMBER :=0;
    57. BEGIN
    58. LOOP
    59. IF mod(v_counter,2)=0 THEN
    60. v_sum :=v_sum+v_counter
    61. END IF;
    62. v_counter :=v_counter+1;
    63. EXIT WHEN v_counter>100;
    64. END LOOP
    65. DBMS_OUTPUT.PUT_LINE(v_sum);
    66. END;
    67. ORA-06550: 第 9 行, 第 3 列:
    68. PLS-00103: 出现符号 "END"在需要下列之一时:
    69. . ( * @ % & = - + ; < / >
    70. at in is mod remainder not rem <an exponent (**)>
    71. <> or != or ~= >= <= <> and or like LIKE2_ LIKE4_ LIKEC_
    72. between || member SUBMULTISET_
    73. 符号 ";" 被替换为 "END" 后继续。
    74. ORA-06550: 第 13 行, 第 14 列:
    75. PLS-00103: 出现符号 "."在需要下列之一时:
    76. ;
    77. ORA-06550: 第 14 行, 第 1 列:
    78. PLS-00103: 出现符号 "END"
    79. SQL>
    80. SQL> declare
    81. 2 v_counter binary_integer :=1;
    82. 3 v_sum number :=0;
    83. 4 begin
    84. 5 loop
    85. 6 if mod(v_counter,2)=0 then
    86. 7 v_sum:=v_sum+v_counter;
    87. 8 end if;
    88. 9 v_counter :=v_counter +1;
    89. 10 exit when v_counter>100;
    90. 11 end loop;
    91. 12 dbms_output.put_line(v_sum);
    92. 13 end;
    93. 14
    94. 15 /
    95. 2550
    96. PL/SQL procedure successfully completed
    97. SQL>
    98. SQL> declare
    99. 2 v_counter binary_integer :=1;
    100. 3 v_sum number :=0;
    101. 4 begin
    102. 5 while v_counter<100 loop
    103. 6 if mod(v_counter,2)=0 then;
    104. 7 v_sum :=v_sum+v_counter;
    105. 8 end if;
    106. 9 v_counter:=v_counter+1;
    107. 10 end loop;
    108. 11 Dbms_Output.put_line(v_sum);
    109. 12 end;
    110. 13 /
    111. declare
    112. v_counter binary_integer :=1;
    113. v_sum number :=0;
    114. begin
    115. while v_counter<100 loop
    116. if mod(v_counter,2)=0 then;
    117. v_sum :=v_sum+v_counter;
    118. end if;
    119. v_counter:=v_counter+1;
    120. end loop;
    121. Dbms_Output.put_line(v_sum);
    122. end;
    123. ORA-06550: 第 7 行, 第 29 列:
    124. PLS-00103: 出现符号 ";"在需要下列之一时:
    125. begin case declare exit for
    126. goto if loop mod null pragma raise return select update while
    127. with <an identifier> <a double-quoted delimited-identifier>
    128. <a bind variable> << close current delete fetch lock insert
    129. open rollback savepoint set sql execute commit forall merge
    130. pipe
    131. 符号 "exit" 被替换为 ";" 后继续。
    132. SQL>
    133. SQL> declare
    134. 2 v_counter binary_integer :=1;
    135. 3 v_sum number :=0;
    136. 4 begin
    137. 5 while v_counter<100 loop
    138. 6 if mod(v_counter,2)=0 then
    139. 7 v_sum :=v_sum+v_counter;
    140. 8 end if;
    141. 9 v_counter:=v_counter+1;
    142. 10 end loop;
    143. 11 Dbms_Output.put_line(v_sum);
    144. 12 end;
    145. 13 /
    146. 2450
    147. PL/SQL procedure successfully completed
    148. SQL>
    149. SQL> declare
    150. 2 v_sum number :=0;
    151. 3 begin
    152. 4 for v_counter in 1..100 loop
    153. 5 if mod(v_counter,2)=0 then
    154. 6 v_sum=v_sum+v_counter;
    155. 7 end if;
    156. 8 end loop;
    157. 9 Dbms_Output.put_line(v_sum);
    158. 10 end;
    159. 11 /
    160. declare
    161. v_sum number :=0;
    162. begin
    163. for v_counter in 1..100 loop
    164. if mod(v_counter,2)=0 then
    165. v_sum=v_sum+v_counter;
    166. end if;
    167. end loop;
    168. Dbms_Output.put_line(v_sum);
    169. end;
    170. ORA-06550: 第 7 行, 第 12 列:
    171. PLS-00103: 出现符号 "="在需要下列之一时:
    172. := . ( @ % ;
    173. ORA-06550: 第 8 行, 第 7 列:
    174. PLS-00103: 出现符号 "END"
    175. SQL>
    176. SQL> declare
    177. 2 v_sum number :=0;
    178. 3 begin
    179. 4 for v_counter in 1..100 loop
    180. 5 if mod(v_counter,2)=0 then
    181. 6 v_sum :=v_sum+v_counter;
    182. 7 end if;
    183. 8 end loop;
    184. 9 Dbms_Output.put_line(v_sum);
    185. 10 end;
    186. 11 /
    187. 2550
    188. PL/SQL procedure successfully completed
    189. SQL>
    190. SQL> declare
    191. 2 v_counter binary_integer :=1;
    192. 3 begin
    193. 4 <<label>>
    194. 5 insert into temp_table values(v_counter,'loop index');
    195. 6 v_counter :=v_Counter+1;
    196. 7 if v_counter<=50 then
    197. 8 goto label;
    198. 9 enf if;
    199. 10 end;
    200. 11 /
    201. declare
    202. v_counter binary_integer :=1;
    203. begin
    204. <<label>>
    205. insert into temp_table values(v_counter,'loop index');
    206. v_counter :=v_Counter+1;
    207. if v_counter<=50 then
    208. goto label;
    209. enf if;
    210. end;
    211. ORA-06550: 第 10 行, 第 9 列:
    212. PLS-00103: 出现符号 "IF"在需要下列之一时:
    213. := . ( @ % ;
    214. SQL>
    215. SQL> declare
    216. 2 v_counter binary_integer :=1;
    217. 3 begin
    218. 4 <<label>>
    219. 5 insert into temp_table values(v_counter,'loop index');
    220. 6 v_counter :=v_Counter+1;
    221. 7 if v_counter<=50 then
    222. 8 goto label;
    223. 9 end if;
    224. 10 end;
    225. 11 /
    226. declare
    227. v_counter binary_integer :=1;
    228. begin
    229. <<label>>
    230. insert into temp_table values(v_counter,'loop index');
    231. v_counter :=v_Counter+1;
    232. if v_counter<=50 then
    233. goto label;
    234. end if;
    235. end;
    236. ORA-06550: 第 6 行, 第 15 列:
    237. PL/SQL: ORA-00942: 表或视图不存在
    238. ORA-06550: 第 6 行, 第 3 列:
    239. PL/SQL: SQL Statement ignored
    240. SQL>
    241. SQL> create table temp_table
    242. 2 (
    243. 3 v_counter varchar2(255),
    244. 4 v_sum varchar2(255)
    245. 5 )
    246. 6 /
    247. Table created
    248. SQL>
    249. SQL> declare
    250. 2 v_counter binary_integer :=1;
    251. 3 begin
    252. 4 <<label>>
    253. 5 insert into temp_table values(v_counter,'loop index');
    254. 6 v_counter :=v_Counter+1;
    255. 7 if v_counter<=50 then
    256. 8 goto label;
    257. 9 end if;
    258. 10 end;
    259. 11 /
    260. PL/SQL procedure successfully completed
    261. SQL>
    262. SQL> declare
    263. 2 v_deptno hr.employees.department_id%type;
    264. 3 cursor c_emp is select * from hr.employees where department_id=v_deptno;
    265. 4 v_emp c_emp%rowtype;
    266. 5 begin
    267. 6 v_deptno :=&x;
    268. 7 open c_emp;
    269. 8 loop
    270. 9 fetch c_emp into v_emp;
    271. 10 exit when c_emp%notfound;
    272. 11 Dbms_Output.put_line(v_emp.employees_id||''||v_emp.first_name||''||v_emp.last_name||''||v_emp.salary||' '||v_deptno);
    273. 12 end loop;
    274. 13 close c_emp;
    275. 14 end;
    276. 15 /
    277. declare
    278. v_deptno hr.employees.department_id%type;
    279. cursor c_emp is select * from hr.employees where department_id=v_deptno;
    280. v_emp c_emp%rowtype;
    281. begin
    282. v_deptno :=10;
    283. open c_emp;
    284. loop
    285. fetch c_emp into v_emp;
    286. exit when c_emp%notfound;
    287. Dbms_Output.put_line(v_emp.employees_id||''||v_emp.first_name||''||v_emp.last_name||''||v_emp.salary||' '||v_deptno);
    288. end loop;
    289. close c_emp;
    290. end;
    291. ORA-06550: 第 12 行, 第 33 列:
    292. PLS-00302: 必须声明 'EMPLOYEES_ID' 组件
    293. ORA-06550: 第 12 行, 第 6 列:
    294. PL/SQL: Statement ignored
    295. SQL>
    296. SQL> declare
    297. 2 v_deptno hr.employees.department_id%type;
    298. 3 cursor c_emp is select * from hr.employees where department_id=v_deptno;
    299. 4 v_emp c_emp%rowtype;
    300. 5 begin
    301. 6 v_deptno :=&x;
    302. 7 open c_emp;
    303. 8 loop
    304. 9 fetch c_emp into v_emp;
    305. 10 exit when c_emp%notfound;
    306. 11 Dbms_Output.put_line(v_emp.employees_id||' '||v_emp.first_name||' '||v_emp.last_name||' '||v_emp.salary||' '||v_deptno);
    307. 12 end loop;
    308. 13 close c_emp;
    309. 14 end;
    310. 15 /
    311. declare
    312. v_deptno hr.employees.department_id%type;
    313. cursor c_emp is select * from hr.employees where department_id=v_deptno;
    314. v_emp c_emp%rowtype;
    315. begin
    316. v_deptno :=100;
    317. open c_emp;
    318. loop
    319. fetch c_emp into v_emp;
    320. exit when c_emp%notfound;
    321. Dbms_Output.put_line(v_emp.employees_id||' '||v_emp.first_name||' '||v_emp.last_name||' '||v_emp.salary||' '||v_deptno);
    322. end loop;
    323. close c_emp;
    324. end;
    325. ORA-06550: 第 12 行, 第 33 列:
    326. PLS-00302: 必须声明 'EMPLOYEES_ID' 组件
    327. ORA-06550: 第 12 行, 第 6 列:
    328. PL/SQL: Statement ignored
    329. SQL>
    330. SQL> declare
    331. 2 v_deptno hr.employees.department_id%type;
    332. 3 cursor c_emp is select * from hr.employees where department_id=v_deptno;
    333. 4 v_emp c_emp%rowtype;
    334. 5 begin
    335. 6 v_deptno :=&x;
    336. 7 open c_emp;
    337. 8 loop
    338. 9 fetch c_emp into v_emp;
    339. 10 exit when c_emp%notfound;
    340. 11 dbms_output.put_line(v_emp.employee_id||' '||v_emp.first_name||' '||v_emp.last_name||' '||v_emp.salary||' '||v_deptno);
    341. 12 end loop;
    342. 13 close c_emp;
    343. 14 end;
    344. 15 /
    345. 108 Nancy Greenberg 12000 100
    346. 109 Daniel Faviet 9000 100
    347. 110 John Chen 8200 100
    348. 111 Ismael Sciarra 7700 100
    349. 112 Jose Manuel Urman 7800 100
    350. 113 Luis Popp 6900 100
    351. PL/SQL procedure successfully completed
    352. SQL>
    353. SQL> declare
    354. 2 v_deptno hr.employees.department_id%type;
    355. 3 cursor c_emp is select * from hr.employees where department_id=v_deptno;
    356. 4 v_emp c_emp%rowtype;
    357. 5 begin
    358. 6 v_deptno :=&x;
    359. 7 open c_emp;
    360. 8 loop
    361. 9 fetch c_emp into v_emp;
    362. 10 exit when c_emp%notfound;
    363. 11 Dbms_Output.put_line(v_emp.employee_id||' '||v_emp.first_name||' '||v_emp.last_name||' '||v_emp.salary||' '||v_deptno);
    364. 12 end loop;
    365. 13 close c_emp;
    366. 14 end;
    367. 15
    368. 16 /
    369. PL/SQL procedure successfully completed
    370. SQL>
    371. SQL> declare
    372. 2 cursor c_dept_stat is
    373. 3 select hr.employees.department_id,avg(hr.employees.salary) avgsal from hr.employees group by
    374. 4 department_id;
    375. 5 v_dept c_dept_stat%rowtype;
    376. 6 begin
    377. 7 open c_dept_stat;
    378. 8 loop
    379. 9 fetch c_dept_stat into v_dept;
    380. 10 exit when c_dept_stat%notfound;
    381. 11 Dbms_Output.put_line(v_dept.department_id||' '||v_dept.avgsal);
    382. 12 end loop;
    383. 13 close c_dept_stat;
    384. 14 end;
    385. 15 /
    386. 100 8600
    387. 30 4150
    388. 7000
    389. 20 9500
    390. 70 10000
    391. 90 19333.3333333333333333333333333333333333
    392. 110 10150
    393. 50 3475.555555555555555555555555555555555556
    394. 40 6500
    395. 80 8955.882352941176470588235294117647058824
    396. 10 4400
    397. 60 5760
    398. PL/SQL procedure successfully completed
    399. SQL>
    400. SQL> declare
    401. 2 cursor c_dept_stat is
    402. 3 select hr.employees.department_id,avg(hr.employees.salary) avgsal from hr.employees group by
    403. 4 department_id;
    404. 5 v_dept c_dept_stat%rowtype;
    405. 6 begin
    406. 7 open c_dept_stat;
    407. 8 fetch c_dept_stat into v_dept;
    408. 9 while c_dept_stat%notfound;
    409. 10 Dbms_Output.put_line(v_dept.department_id||' '||v_dept.avgsal);
    410. 11 fetch c_dept_stat into v_dept;
    411. 12 end loop;
    412. 13 close c_dept_stat;
    413. 14 end;
    414. 15 /
    415. declare
    416. cursor c_dept_stat is
    417. select hr.employees.department_id,avg(hr.employees.salary) avgsal from hr.employees group by
    418. department_id;
    419. v_dept c_dept_stat%rowtype;
    420. begin
    421. open c_dept_stat;
    422. fetch c_dept_stat into v_dept;
    423. while c_dept_stat%notfound;
    424. Dbms_Output.put_line(v_dept.department_id||' '||v_dept.avgsal);
    425. fetch c_dept_stat into v_dept;
    426. end loop;
    427. close c_dept_stat;
    428. end;
    429. ORA-06550: 第 10 行, 第 32 列:
    430. PLS-00103: 出现符号 ";"在需要下列之一时:
    431. . ( * @ % & = - + < / > at in
    432. is loop mod remainder not rem <an exponent (**)>
    433. <> or != or ~= >= <= <> and or like LIKE2_ LIKE4_ LIKEC_
    434. between || multiset member SUBMULTISET_
    435. ORA-06550: 第 14 行, 第 6 列:
    436. PLS-00103: 出现符号 "CLOSE"在需要下列之一时:
    437. end not pragma final
    438. instantiable order overriding static member constructor map
    439. ORA-06550: 第 17 行, 第 0 列:
    440. PLS-00103: 出现符号 "end-of-file"在需要下列之一时:
    441. pragma
    442. SQL>
    443. SQL> declare
    444. 2 cursor c_dept_stat is
    445. 3 select hr.employees.department_id,avg(hr.employees.salary) avgsal from hr.employees group by
    446. 4 department_id;
    447. 5 v_dept c_dept_stat%rowtype;
    448. 6 begin
    449. 7 open c_dept_stat;
    450. 8 fetch c_dept_stat into v_dept;
    451. 9 while c_dept_stat%found loop;
    452. 10 Dbms_Output.put_line(v_dept.department_id||' '||v_dept.avgsal);
    453. 11 fetch c_dept_stat into v_dept;
    454. 12 end loop;
    455. 13 close c_dept_stat;
    456. 14 end;
    457. 15 /
    458. declare
    459. cursor c_dept_stat is
    460. select hr.employees.department_id,avg(hr.employees.salary) avgsal from hr.employees group by
    461. department_id;
    462. v_dept c_dept_stat%rowtype;
    463. begin
    464. open c_dept_stat;
    465. fetch c_dept_stat into v_dept;
    466. while c_dept_stat%found loop;
    467. Dbms_Output.put_line(v_dept.department_id||' '||v_dept.avgsal);
    468. fetch c_dept_stat into v_dept;
    469. end loop;
    470. close c_dept_stat;
    471. end;
    472. ORA-06550: 第 10 行, 第 34 列:
    473. PLS-00103: 出现符号 ";"在需要下列之一时:
    474. begin case declare exit for
    475. goto if loop mod null pragma raise return select update while
    476. with <an identifier> <a double-quoted delimited-identifier>
    477. <a bind variable> << close current delete fetch lock insert
    478. open rollback savepoint set sql execute commit forall merge
    479. pipe
    480. 符号 "exit" 被替换为 ";" 后继续。
    481. SQL>
    482. SQL> declare
    483. 2 cursor c_dept_stat is
    484. 3 select hr.employees.department_id,avg(hr.employees.salary) avgsal from hr.employees group by
    485. 4 department_id;
    486. 5 v_dept c_dept_stat%rowtype;
    487. 6 begin
    488. 7 open c_dept_stat;
    489. 8 fetch c_dept_stat into v_dept;
    490. 9 while c_dept_stat%found loop
    491. 10 Dbms_Output.put_line(v_dept.department_id||' '||v_dept.avgsal);
    492. 11 fetch c_dept_stat into v_dept;
    493. 12 end loop;
    494. 13 close c_dept_stat;
    495. 14 end;
    496. 15 /
    497. 100 8600
    498. 30 4150
    499. 7000
    500. 20 9500
    501. 70 10000
    502. 90 19333.3333333333333333333333333333333333
    503. 110 10150
    504. 50 3475.555555555555555555555555555555555556
    505. 40 6500
    506. 80 8955.882352941176470588235294117647058824
    507. 10 4400
    508. 60 5760
    509. PL/SQL procedure successfully completed
    510. SQL>
    511. SQL> declare
    512. 2 cursor c_dept_stat is
    513. 3 select hr.employees.department_id,avg(hr.employees.salary) avgsal from hr.employees group by
    514. 4 department_id;
    515. 5 v_dept c_dept_stat%rowtype;
    516. 6 begin
    517. 7 FOR V_DEPT IN (
    518. 8 select hr.employees.department_id,avg(hr.employees.salary) avgsal from hr.employees group by
    519. 9 department_id;
    520. 10 )LOOP
    521. 11
    522. 12 Dbms_Output.put_line(v_dept.department_id||' '||v_dept.avgsal);
    523. 13 end loop;
    524. 14 end;
    525. 15 /
    526. declare
    527. cursor c_dept_stat is
    528. select hr.employees.department_id,avg(hr.employees.salary) avgsal from hr.employees group by
    529. department_id;
    530. v_dept c_dept_stat%rowtype;
    531. begin
    532. FOR V_DEPT IN (
    533. select hr.employees.department_id,avg(hr.employees.salary) avgsal from hr.employees group by
    534. department_id;
    535. )LOOP
    536. Dbms_Output.put_line(v_dept.department_id||' '||v_dept.avgsal);
    537. end loop;
    538. end;
    539. ORA-06550: 第 10 行, 第 15 列:
    540. PLS-00103: 出现符号 ";"在需要下列之一时:
    541. loop
    542. SQL>
    543. SQL> declare
    544. 2 cursor c_dept_stat is
    545. 3 select hr.employees.department_id,avg(hr.employees.salary) avgsal from hr.employees group by
    546. 4 department_id;
    547. 5 v_dept c_dept_stat%rowtype;
    548. 6 begin
    549. 7 FOR V_DEPT IN (
    550. 8 select hr.employees.department_id,avg(hr.employees.salary) avgsal from hr.employees group by
    551. 9 department_id
    552. 10 )LOOP
    553. 11
    554. 12 Dbms_Output.put_line(v_dept.department_id||' '||v_dept.avgsal);
    555. 13 end loop;
    556. 14 end;
    557. 15 /
    558. declare
    559. cursor c_dept_stat is
    560. select hr.employees.department_id,avg(hr.employees.salary) avgsal from hr.employees group by
    561. department_id;
    562. v_dept c_dept_stat%rowtype;
    563. begin
    564. FOR V_DEPT IN (
    565. select hr.employees.department_id,avg(hr.employees.salary) avgsal from hr.employees group by
    566. department_id;
    567. )LOOP
    568. Dbms_Output.put_line(v_dept.department_id||' '||v_dept.avgsal);
    569. end loop;
    570. end;
    571. ORA-06550: 第 10 行, 第 15 列:
    572. PLS-00103: 出现符号 ";"在需要下列之一时:
    573. loop
    574. SQL>
    575. SQL> declare
    576. 2 cursor c_dept_stat is
    577. 3 select hr.employees.department_id,avg(hr.employees.salary) avgsal from hr.employees group by
    578. 4 department_id;
    579. 5 v_dept c_dept_stat%rowtype;
    580. 6 begin
    581. 7 FOR V_DEPT IN (
    582. 8 select hr.employees.department_id,avg(hr.employees.salary) avgsal from hr.employees group by
    583. 9 department_id
    584. 10 )LOOP
    585. 11
    586. 12 Dbms_Output.put_line(v_dept.department_id||' '||v_dept.avgsal);
    587. 13 end loop;
    588. 14 end;
    589. 15 /
    590. declare
    591. cursor c_dept_stat is
    592. select hr.employees.department_id,avg(hr.employees.salary) avgsal from hr.employees group by
    593. department_id;
    594. v_dept c_dept_stat%rowtype;
    595. begin
    596. FOR V_DEPT IN (
    597. select hr.employees.department_id,avg(hr.employees.salary) avgsal from hr.employees group by
    598. department_id;
    599. )LOOP
    600. Dbms_Output.put_line(v_dept.department_id||' '||v_dept.avgsal);
    601. end loop;
    602. end;
    603. ORA-06550: 第 10 行, 第 15 列:
    604. PLS-00103: 出现符号 ";"在需要下列之一时:
    605. loop
    606. SQL>
    607. SQL> declare
    608. 2 cursor c_dept_stat is
    609. 3 select hr.employees.department_id,avg(hr.employees.salary) avgsal from hr.employees group by
    610. 4 department_id;
    611. 5 v_dept c_dept_stat%rowtype;
    612. 6 begin
    613. 7 FOR v_dept IN (
    614. 8 select hr.employees.department_id,avg(hr.employees.salary) avgsal from hr.employees group by
    615. 9 department_id
    616. 10 ) LOOP
    617. 11 Dbms_Output.put_line(v_dept.department_id||' '||v_dept.avgsal);
    618. 12 end loop;
    619. 13 end;
    620. 14 /
    621. 100 8600
    622. 30 4150
    623. 7000
    624. 20 9500
    625. 70 10000
    626. 90 19333.3333333333333333333333333333333333
    627. 110 10150
    628. 50 3475.555555555555555555555555555555555556
    629. 40 6500
    630. 80 8955.882352941176470588235294117647058824
    631. 10 4400
    632. 60 5760
    633. PL/SQL procedure successfully completed
    634. SQL>
    635. SQL> declare
    636. 2 cursor c_dept_stat is
    637. 3 select hr.employees.department_id,avg(hr.employees.salary) avgsal from hr.employees group by
    638. 4 department_id;
    639. 5 v_dept c_dept_stat%rowtype;
    640. 6 begin
    641. 7 update hr.employees set hr.employees.salary=salary+100 where employee_id=1000;
    642. 8 if sql%notfound then
    643. 9 insert into hr.employees(emploee_id,first_name,last_name,email,hire_date,job_id,department_id)values (employees_seq.nextval,'san','zhang','zs@neusoft.edu.cn',sysdate,'AC_ACCOUNT',200);
    644. 10 end if;
    645. 11 end;
    646. 12 /
    647. declare
    648. cursor c_dept_stat is
    649. select hr.employees.department_id,avg(hr.employees.salary) avgsal from hr.employees group by
    650. department_id;
    651. v_dept c_dept_stat%rowtype;
    652. begin
    653. update hr.employees set hr.employees.salary=salary+100 where employee_id=1000;
    654. if sql%notfound then
    655. insert into hr.employees(emploee_id,first_name,last_name,email,hire_date,job_id,department_id)values (employees_seq.nextval,'san','zhang','zs@neusoft.edu.cn',sysdate,'AC_ACCOUNT',200);
    656. end if;
    657. end;
    658. ORA-06550: 第 10 行, 第 107 列:
    659. PL/SQL: ORA-02289: 序列不存在
    660. ORA-06550: 第 10 行, 第 5 列:
    661. PL/SQL: SQL Statement ignored
    662. SQL>
    663. SQL> begin
    664. 2 update hr.employees set hr.employees.salary=salary+100 where employee_id=1000;
    665. 3 if sql%notfound then
    666. 4 insert into hr.employees(emploee_id,first_name,last_name,email,hire_date,job_id,department_id)values (employees_seq.nextval,'san','zhang','zs@neusoft.edu.cn',sysdate,'AC_ACCOUNT',200);
    667. 5 end if;
    668. 6 end;
    669. 7 /
    670. begin
    671. update hr.employees set hr.employees.salary=salary+100 where employee_id=1000;
    672. if sql%notfound then
    673. insert into hr.employees(emploee_id,first_name,last_name,email,hire_date,job_id,department_id)values (employees_seq.nextval,'san','zhang','zs@neusoft.edu.cn',sysdate,'AC_ACCOUNT',200);
    674. end if;
    675. end;
    676. ORA-06550: 第 5 行, 第 107 列:
    677. PL/SQL: ORA-02289: 序列不存在
    678. ORA-06550: 第 5 行, 第 5 列:
    679. PL/SQL: SQL Statement ignored
    680. SQL>

  • 相关阅读:
    10.导弹拦截
    学习Python中pywifi模块的基本用法
    Oracle数据库SQL优化详解
    像MySql一样在控制台输出打印漂亮的表格
    【C语言】插入排序详解
    关于XML一些介绍和注意事项
    C++学习——vector类的使用
    【IO多路转接】poll&epoll
    线程池|单例模式|STL、智能指针线程安全|读者写者问题
    YOLOv5、v7改进之二十六:改进特征融合网络PANet为ASFF自适应特征融合网络
  • 原文地址:https://blog.csdn.net/m0_63064861/article/details/133950840