这篇文章介绍的是pl/sql数据库语言,如有错误或者不当之处,希望各位大神批评指正。
pl/sql块由声明部分、执行部分、异常处理部分组成,结构如下:
DECLEAR /*声明部分:声明pl/sql用到的变量类型及游标,及局部的存储过程及函数*/ BEGIN /*执行部分:过程及sql语句,是程序的主要部分*/ EXCEPTION /*异常部分:错误处理*/ END /*sql块结束*/注:其中,执行部分是必须的
简单的例子 --启用输出 set serveroutput on declare /*声明变量,类型,游标*/ begin /*对程序的执行部分,类似于java中的main方法*/ dbms.output('hello pl/sql!') ; --exception /*对begin发生的异常进行处理 when...then... when...then...*/ end ; /*pl/sql代码块结束*/注:变量的类型同oracle
注:数据类型要对应,长度要大于等于查的字段的长度,推荐使用变量名%type取对应字段
-例,定义一个记录类型用于查学生
declare /*定义记录类型*/ type student_recode is record( v_age student.age%type , v_name student.name%type , v_sex student.sex%type ); /*声明记录类型*/ v_student_record student_recode ; /*或者使用直接使用rowType*/ v_student_record student%rowType ; begin select age,name,sex into student_recode from student where id = 100001 ; dbms_output.put_line(v_student_record.v_name||','||v_student_record.v_age||','||v_student_record.v_sex) ; end ; table%rowType:返回类型与table中的列类型一致param%Type:返回类型与param字段一致ql/sql的赋值符为 := 逻辑运算符、关系运算符、算术运算符同oracle的相关运算符