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<=10 loopdbms_output.put_line('while loop size='||i);i:=i+1;end loop;end;

–for循环

declarei number(4):=1;beginfor i in 1..10 loopdbms_output.put_line('for loop Size:'||i);end loop;end;
declarei number(2):=1;j number(2):=1;beginfor i in reverse 1..9 loopfor j in 1..i loopdbms_output.put(j||'x'||i||'='||j*i||' ');end loop;dbms_output.put_line('');end loop;end;

–动态SQL

declareuserId number(2);sql_str varchar2(100);userName user_tbl.user_name%type;beginexecute immediate 'create table testExe(id number,test_name varchar2(20))';userId:='&userId';sql_str:='select user_name from user_tbl where id=:id';execute immediate sql_str into userName using userId;dbms_output.put_line(userName);end;(ordeclareid_param number:='&id_param';sql_str varchar2(100);name_param stu_tbl.stu_name%type;beginsql_str:='select stu_name from stu_tbl where id=:p';execute immediate sql_str into name_param using id_param;dbms_output.put_line(name_param);end;/)

–异常处理

declaregrade number(4);begingrade:='&grade';case gradewhen 1 then dbms_output.put_line('好的');--else dbms_output.put_line('不好');end case;exceptionwhen case_not_found thendbms_output.put_line('输入类型不匹配!');end;

–系统异常

declarerowD user_tbl%rowtype;beginselect * into rowD from user_tbl;dbms_output.put_line(rowD.id||''||rowD.user_name||' '||rowD.password);exceptionwhen too_many_rows thendbms_output.put_line('不能将多行赋予一个属性!');end;ordeclarerowD user_tbl%rowtype;beginselect * into rowD from user_tbl where id=5;dbms_output.put_line(rowD.id||' '||rowD.user_name||' '||rowD.password);exceptionwhen too_many_rows thendbms_output.put_line('不能将多行赋予一个属性!');when no_data_found thendbms_output.put_line('没有您要查找的数据!');end;

–自定义错误

declareinvalidError exception;category varchar2(20);begincategory:='&category';if category not in('附件','顶盘','备件') thenraise invalidError;elsedbms_output.put_line('您输入的类别是:'||category);end if;exceptionwhen invalidError thendbms_output.put_line('无法识别的类别!');end;

–引发应用程序异常

declareapp_exception exception;grade user_tbl.grade%type;beginselect grade into grade from user_tbl where id=&id;if grade='A' thenraise app_exception;elsedbms_output.put_line('查询的等级为:'||grade);end if;exceptionwhen app_exception thenraise_application_error(-20001,'未知的等级!');end;

原创文章,作者:guozi,如若转载,请注明出处:https://www.sudun.com/ask/88698.html

(0)
guozi's avatarguozi
上一篇 2024年6月4日 上午9:22
下一篇 2024年6月4日 上午9:24

相关推荐

  • 苏州网站开发公司鹅鹅鹅,苏州制作网站公司

    苏州网站开发公司是一个令人着迷的行业,它不仅仅是网站开发,更是一门艺术。它可以帮助企业创建独特的网站形象并吸引更多客户。那么,什么是网站制作公司呢?他们有什么特点?提供什么样的服务…

    行业资讯 2024年3月19日
    0
  • 公共邮箱是什么

    公共邮箱,你听说过吗?它是什么,有什么用处?或许你已经猜到了一些,但是还有很多你不知道的。公共邮箱作为网络行业的一个重要组成部分,它的优势和用途也是不容忽视的。那么,如何设置和使用…

    行业资讯 2024年3月20日
    0
  • 无法使用代理服务器

    在如今信息爆炸的时代,网络安全问题已经成为人们关注的焦点。其中,代理服务器作为一种常见的网络安全工具,可以帮助用户隐藏真实IP地址,保护个人隐私。然而,当我们想要使用代理服务器时,…

    行业资讯 2024年3月22日
    0
  • 如何利用seo排名工具提升网站的搜索引擎排名?

    在当今数字化时代,网站的搜索引擎排名已经成为企业提升知名度和影响力的重要手段。但是,如何有效地利用SEO排名工具来提升网站在搜索引擎中的排名,却是许多企业主和网络营销人员头痛不已的…

    行业资讯 2024年3月31日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注