oracle基础语法汇总(七)
点击蓝色字关注“SQL数据库运维”,回复“SQL”获取2TB学习资源!
既往文章链接(点击即可跳转):
Oracle基础语法汇总(一)
Oracle基础语法汇总(二)
Oracle基础语法汇总(三)
Oracle基础语法汇总(四)
Oracle基础语法汇总(五)
Oracle基础语法汇总(六)
四.使用PL/SQL
可用于创建存储过程,触发器,程序包,给SQL语句的执行添加程序逻辑。
支持SQL,在PL/SQL中可以使用:
数据操纵命令
事务控制命令
游标控制
SQL函数和SQL运算符
支持面向对象编程(OOP)
可移植性
更佳的性能,PL/SQL经过编译执行
分为三个部分:声明部分,可执行部分和异常处理部分
[declaredeclarations]beginexecutable statements[exceptionhandlers]end;打开输出set serverout on;
--根据输入编号获取某学员的成绩--if
declarescore user_tbl.score%type;beginselect score into score from user_tbl where id='&id';if score>90 thendbms_output.put_line('优秀');elsif score>80 thendbms_output.put_line('良好');elsif score>60 thendbms_output.put_line('及格');elsedbms_output.put_line('差');end if;end;
--根据学员姓名获取某学员的成绩--if
declarescore user_tbl.score%type;beginselect score into score from user_tbl where user_name='&name';if score>90 thendbms_output.put_line('优秀');elsif score>80 thendbms_output.put_line('良好');elsif score>60 thendbms_output.put_line('及格');elsedbms_output.put_line('差');end if;end;
--case的使用
declaregrade user_tbl.grade%type;beginselect grade into grade from user_tbl where id='&id';case gradewhen 'A' then dbms_output.put_line('优异');when 'B' then dbms_output.put_line('优秀');when 'C' then dbms_output.put_line('良好');else dbms_output.put_line('一般');end case;end;
--基本循环
declarei number(4):=1;beginloopdbms_output.put_line('loop size:'||i);i:=i+1;exit when i>10;end loop;end;
--while循环
declarei number(4):=1;beginwhile i