黑马程序员技术交流社区

标题: pl/sql语言 [打印本页]

作者: 邱峁    时间: 2012-12-11 17:35
标题: pl/sql语言
pl/sql语言
事务:事务是指一个单元的工作,这些工作要么全做,要么全部不做

属性:原子性、一致性、独立性和持久性。

      原子性是指事务必须是一个自动的单元工作,要么执行全部数据的修改,要么全部数据的修改都不执行。
          
      一致性是指当事务完成时,必须使所有数据都具有一致的状态。在关系型数据库中,所有的规则必须应用到
事务的修改上,以便维护所有数据的完整性。所有的内部数据结构,例如树状的索引与数据之间的链接,在事务结束之后,必须保证正确。

    独立性是指并行事务的修改必须与其他并行事务的修改相互独立。一个事务看到的数据要么是另外一个事务修改
这些事务之前的状态,要么是第二个事务已经修改完成的数据,但是这个事务不能看到正在修改的数据。这种
特征也称为串行性。


        持久性是指当一个事务完成之后,它的影响永久性的产生在系统中,也就是这种修改写到了数据库中。
SQL语法;

sql的流程控制;

1.

if condition then Statement end if  
该表达式的功能为:若条件为真,执行then后的语句;否则,跳出条件语句执行end if后的语句。

2.

if condition then Statements_1 else Statements_2 end if
该表达式的功能为:如果条件为真执行then后的语句,否则执行else后的语句。


3.

if condition1 then Statements_1 else if condition2 then Statements 2 end if;end if;
then Statements_2 else Statements_3 end if
该表达式的功能为:如果if后的条件成立,执行then后面的语句,否则判断else if后面的条件,条件成立执行第二个then后面的语句,否则执行else后的语句。这是条件语句嵌套。
case语句

Case语句的基本格式如下:

Case变量
WHEN表达式1 then值1
WHEN表达式2 then值2
WHEN表达式3 then值3
WHEN表达式4 then值4
ELSE值5
END;
注:case 后面可以接算术表达式;但when后面只能跟字母或数字...

2、循环控制
(1)loop
statements
exit出口条件end循环控制语句
2.

WHILE 条件.LOOP Statemenfs end loop 循环控制语句
(3).

FOR 变量名(随便定义) IN [REVERSE] start_range..end_range LOOP
statements;
END LOOP;
用pl/sql访问数据库

向数据库中插入一条记录
declare
newname varchar2(30):='张无忌';
newroleid number(3):=2;
createtime date:=sysdate;
begin
insert into users
values(sql_id.nextval,newname,null,newroleid,createtime);
Commit;
end;


select username into name ;将数据库中的用户名取出放到name变量中,只能是单条记录

%TYPE与%ROWTYPE
%TYPE属性捕获其他变量,常量或记录变量中字段的数据类型或数据库表列的数据类型.刚才声明的自定义类型
游标

应用程序无论何时提交sql语句,服务器会至少打开一个游标处理该语句。游标实质上是一个sql语句的工作区。当pl/sql程序提交insert,update,delete语句时,oracle会自动打开一个游标来处理该语句。Oracle 还用游标来自动处理仅返回一行的select 语句。Oracle自动使用的这种游标称为隐式游标。使用隐式游标时,用户只要简单的编写select语句,让pl/sql根据需要处理游标即可
游标的定义和使用

Declare
Cursor ucur is select * from users; --声明游标
us users%rowtype;--定义与游标想匹配的变量
Begin
Open ucur;--打开游标
Fetch ucur into us;
While ucur %found loop --使用循环遍历游标的查询结果
Dbms_output.put_line('姓名:'||us.username||'生日'||us.brithday);
Fetch ucur into us;
End loop;
Close ucur; --关闭游标
End;
%rowcount显示迄今为止从显示游标中取出的行数
使用游标for循环简化处理游

Set serverout on
Declare
Cursor userrow is
Select * from users order by username desc;
Begin
for currentrow in userrow loop
Dbms_output.put_line(currentrow.username||
Currentrow.brithday);
End loop;
End;
创建存储过程

创建存储过程的语句如下:
CREATE[OR REPLACE] PROCEDURE<过程名>
<参数1>,「方式l]<数据类型1>,
<参数2>,[ 方式2]<数据类型2>,
……)
IS|AS  is_或as完全等价
BEGIN
PL/SQL过程体
END<过程名>
创建几个存储过程的例子

打印一行指定数量的字符
Set serveroutput on
create or replace procedure printLine (width in Integer,ch in char default '-') is
begin
for i in 1.. width loop
dbms_output.put(ch);
end loop;
dbms_output.put_line('');
end printLine;

调用存储过程

Execute pintLine(20,’*’);

用存储过程访问数据库
create or replace procedure inuser
(id in number,
name in varchar2,
brithday date,
age in number,
role number,
createtime date
)
is
Begin
insert into users values (id,name,brithday,age, createtime,role);
commit;
return;
exception when others then
dbms_output.put_line(‘error');
rollback;
return;
end inuser;
execute inuser(5,'郭镜',sysdate,3,sysdate);
带出参的存储过程

create or replace procedure copyTohistory
( id in number,result out number)
is
ree varchar2(20); hid number;
begin
select o.remark into ree from han.orders o where pk=id;
if(ree='已发货‘) then
Insert into han.historyorder h(h.pk,h.costmoerId,h.totalMoney,
h.sendDate,h.orderItemid,h.remark)
(select o.pk,o.costmoerId,o.totalMoney,o.sendDate,o.orderItemid,o.remark from han.orders o where o.pk=id);
commit;
end if;
result:=1;
exception when others then
rollback; result:=0;
end copyTohistory;



调用带out参数的存储过程

declare-
o number;--对应于out类型的参数
begin
copyTohistory(6,o);
dbms_output.put_line(o);
end;
删除存储过程drop procedure 过程名






欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2