您的位置:首页 > 数据库

PL/SQL 练习

2012-05-30 14:39 309 查看
1.//自动插入100条记录

--手动插入100条数据

--insert into testtable();

--写一个程序,自动插入

declare

--定义循环的初试值

i int:=1;

--定义循环的最大值,并设置为常量

maxrecords constant int:=100;

begin

--循环100次,每次插入一条记录

for i in 1..maxrecords loop

insert into testtable(recordnumber,currentdate) values(i,sysdate);

end loop;

--提出插入完成

dbms_output.put_line('成功录入数据');

end;

/

2.主键自增长

////

declare

v_id temp2.id%type:=101;

v_counter temp2.counter%type:=1;

begin

loop

insert into temp2(id,counter) values(v_id,v_counter)

v_counter:=v_counter+1;

when v_counter>10;

end loop;

end;

/

3.////////////////////////////////////////////

%type %rowtype

%TYPE的属性

声明一个变量使之与数据库某个列的类型定义相同或

与另一个已经定义过的变量类型相同,所以%TYPE要作为列名的后缀:如:

v_last_name s_emp.last_name%TYPE;

v_first_name s_emp.first_name%TYPE; --这样做的好处是我们不必去知晓此列的类型与定义

或:

v_balance NUMBER(7,2);

v_minimum_balance v_balance%TYPE := 10;



--------------------------------------

select * from SCOTT.TESTTABLE where RECORDNUMBER=68;

/////复合类型

set serveroutput on

declare

--定义一个复合类型,名称为 myrecord,能够记录两个列的类型,分别是

--myrecordnumber mycurrentdate

type myrecord is record(

myrecordnumber int,

mycurrentdate date);

--把自定义的,绑定数据类型 myrecord ,赋给变量

srecord myrecord;

begin

--into srecord 相当于赋值,把查询的结果赋值给 变量 second

select * into srecord from SCOTT.TESTTABLE where RECORDNUMBER=68;

dbms_output.put_line(srecord.mycurrentdate);

end;





%rowtype

--在定义一个变量,复合类型,类型与一个已知表中一行所有列的类型一致,

--在一个表,有很多列的情况下,直接复制这个表所有列的类型

Declare

mytable testtable%rowtype;

begin

select * into mytable from scott.testtable where recordnumber=88;

dbms_output.put_line(mytable.currentdate);

end;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: