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;
--手动插入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;
相关文章推荐
- oracle pl/sql实例练习
- pl/sql练习
- Oracle 练习P297 131026 PL/SQL块程序
- ORACLE PL/SQL练习(三)
- ORACLE PL/SQL练习(四)
- oracle pl/sql实例练习
- ORACLE PL/SQL练习(一)
- pl/sql习题加练习
- ORACLE PL/SQL练习(六)
- Oracle PL/SQL小练习
- ORACLE PL/SQL练习(二)
- Oracle:PL/SQL--流程控制——作业练习:向表中循环插入ID编号
- PL/SQL经典练习
- oracle pl/sql实例练习
- 定oracle pl/sql实例练习
- oracle pl/sql实例练习
- Oracle 数据库基础学习 (八) PL/SQL综合练习
- PL/SQL Developer 插件程序练习
- PL/SQL语言循环简单语句练习
- ORACLE PL/SQL练习(七)