星辰日月00

欲多则心散,心散则志衰,志衰则思不达也!

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

一、执行SELECT语句
在PL/SQL程序中,使用SELECT INTO语句查询一条记录的信息。
语法格式:

SELECT expression_list INTO variable_list | record_ variable 
  FROM table_name     
  WHERE condition;


 --expression_list指定选择的列或表达式;variable_list指定接收查询结果的标量变量名
--record_variable用于指定接收查询结果的记录变量名,接收查询结果可以使用标量变量也可以
--使用记录变量,当使用标量变量时,变量的个数、顺序应该与查询的目标数据相匹配。
--注意:在PL/SQL块中直接使用SELECT INTO语句时,该语句只能返回一行数据,如果SELECT语句
--返回多行数据,会产生TOO_MANY_ROW异常;如果没有返回数据,则会产生NO_DATA_FOUND异常。

 1 DECLARE
 2  v_id departments.department_id%type;
 3  v_name departments.department_name%type;
 4  v_address departments.address%type;
 5 BEGIN
 6  SELECT * INTO v_id, v_name, v_address
 7   FROM departments
 8   WHERE department_id = 101;
 9  DBMS_OUTPUT.PUT_LINE('系部名称:' || v_name);
10  DBMS_OUTPUT.PUT_LINE('系部地址:' || v_address);
11 END;
12 DECLARE
13  v_student students%ROWTYPE;
14 BEGIN
15  SELECT * INTO v_student
16   FROM students
17   WHERE student_id = 10212;
18  DBMS_OUTPUT.PUT_LINE('姓名 性别 出生日期');
19  DBMS_OUTPUT.PUT_LINE(v_student.name || v_student.sex || v_student.dob);
20 END;
21 DECLARE
22  v_student students%ROWTYPE;
23 BEGIN
24  SELECT * INTO v_student
25   FROM students
26   WHERE name LIKE '王%';   --产生TOO_MANY_ROW异常
27  DBMS_OUTPUT.PUT_LINE('姓名 性别 出生日期');
28  DBMS_OUTPUT.PUT_LINE(v_student.name || v_student.sex || v_student.dob);
29 END;
30 DECLARE
31  v_student students%ROWTYPE;
32 BEGIN
33  SELECT * INTO v_student
34   FROM students
35   WHERE dob = '31-12月-2015';   --产生NO_DATA_FOUND异常
36  DBMS_OUTPUT.PUT_LINE('姓名 性别 出生日期');
37  DBMS_OUTPUT.PUT_LINE(v_student.name || v_student.sex || v_student.dob);
38 END;

 

 

 

二、执行DML语句
1.执行INSERT语句
语法格式:

 1  INSERT INTO table_name [(col1, col2, ..., coln)] --插入一条记录
 2   VALUES(val1,val2,...valn);
 3  INSERT INTO table_name [(col1, col2,..., coln)]
 4   AS SubQuery;
 5 BEGIN
 6  INSERT INTO students
 7   VALUES(10188, NULL, '王一', '', '07-5月-1988', '计算机');
 8 END;
 9 DECLARE
10  v_id students.student_id%TYPE := 10199;
11  v_monitorid students.monitor_id%TYPE := NULL;
12  v_name students.name%TYPE := '张三';
13  v_sex students.sex%TYPE := '';
14  v_dob students.dob%TYPE := '07-5月-1988';
15  v_specialty students.specialty%TYPE := '计算机';
16 BEGIN
17  INSERT INTO students
18   VALUES(v_id, v_monitorid, v_name, v_sex, v_dob, v_specialty);
19 END;
20 BEGIN
21  INSERT INTO students_computer  --没AS的吗?
22   (SELECT * FROM students WHERE specialty = '计算机');
23 END;

 

 

2.执行UPDATE语句
语法格式:

1 UPDATE table_name SET col1 = val1 [, col2 =val2, ..., coln = valn]
2   [WHERE condition(s)];
3 BEGIN
4  UPDATE students
5   SET student_id = 10288,m
6    dob = '07-5月-1988',
7    specialty = '自动化'
8   WHERE student_id = 10188;
9 END;  

 

 

3.执行DELETE语句
语法格式:

 1 delete from table_name [where condition(s)];
 2 begin
 3  delete from students
 4   where student_id = 10188;
 5 end;
 6 declare
 7  v_specialty students.specialty%Type := '计算机';
 8 begin
 9  delete from students
10   where specialty = v_specialty;
11 end;

 

 

三、执行事务处理语句
 在pl/sql程序中,可以使用DML语句。这些DML语句构成了Oracle数据库事务。
与sql操作中可以使用commit、rollback、savepoint等语句处理事务一样,在pl/sql
程序中,同样可以使用上述事务处理语句处理Oracle数据库事务。

 1 begin
 2  insert into students
 3   values(10101, null, '王晓', '', '07-5月-1988', '计算机');
 4  commit;
 5  delete from students
 6   where specialty = '计算机';
 7  rollback;
 8  update students
 9    set student_id = 10288,
10       dob = '07-5月-1998';
11       specialty = '自动化'
12   where student_id = 10101;
13  savepoint spl;
14  delete from students
15   where student_id = 10101;
16  savepoint sp2;
17  rollback to sp1;
18  commit;
19 end;

 

posted on 2012-03-20 11:39  星辰日月00  阅读(3773)  评论(0编辑  收藏  举报