/*Oracle PL/SQL 块*/

来源:互联网 发布:linux 命令输出到变量 编辑:程序博客网 时间:2024/06/15 13:07

[Declare
定义语句块 //定义参数,定义数据类型
] 可省略
Begin
程序块 //写执行语句 不可省略
[Exception]
异常处理语句块 //可以省略
End; //后面有分号
/ 表示pl/sql 块结束

•  := 赋值操作符
•  || 连接操作符
•  – 单⾏注释
•  /**/ 多⾏注释
•  <<>> 标签分隔符
•  .. 范围操作符
•  ** 求幂操作符

create table  student(num int primary key,name varchar2(10)  not null,sex char(2)  );insert into student(num,name,sex) values(1,'刘邦','1');insert into student(num,name,sex) values(2,'吕雉','2');insert into student(num,name,sex) values(3,'张亮','1');insert into student(num,name,sex) values(4,'何炅','2');insert into student(num,name,sex) values(5,'何炅','2');
--set serveroutput on --把结果输出在屏幕上  我们的编译器不用写这句话declare sname varchar2(10); --定义一个参数beginselect name into  sname from student where num=&no;--&表示输入参数 no 随便取 into为赋值dbms_output.put_line(sname); -- 打印语句end;/--程序块中不能用select语句直接做屏幕输出用途,只能提取,select into 放入到变量中,再将变量打印输出

%type

--%type选择表中某一列的数据类型做为参数的数据类型--参数名字  表.列%type;declare sname  student.name%type; --变量sname的数据类型,用name的类型beginselect name into  sname from student where num=&no;--& 表示输入参数 no 随便取 into为赋值dbms_output.put_line(sname); -- 打印语句end;

把表当中的多列合成一个复合型数据类型–相当于c语言中的结构体。

--type 复合型数据类型的名字 is record(参数1   表.列%type,参数2   表.列%type)变量   复合型数据类型的名字;declaretype stu_name is record(ssex student.sex%type,sname student.name%type);s_name stu_name;beginselect sex,name into s_name from student where num=2;dbms_output.put_line('学生性别='||s_name.ssex);dbms_output.put_line('学生姓名='||s_name.sname);end;

把数据构造成一个数组

--Type 数据类型名字 is table of  表.列名%type index by binary_integer; 变量名  数据类型名字;declaretype stu_name is table of student.name%type index by binary_integer;s_name stu_name;beginselect name into s_name(2) from student where num=1;dbms_output.put_line(s_name(2));end;

Loop语法结构

Loop语法结构Loop语句【Exit 退出条件】End loop ;
declarei int :=600;beginloopinsert into student  values(i,'&name','&sex');i:=i+1;exit when i>=605;end loop ;end;select *from student;
原创粉丝点击