网络编程
位置:首页>> 网络编程>> 数据库>> plsql与tsql的语法不同(2)

plsql与tsql的语法不同(2)

 来源:asp之家 发布时间:2009-09-13 17:33:00 

标签:plsql,tsql,语法

本人学java 的 对plsql一看觉的很简单 和java比起来简单多了但是oracle 命令只是一部分更多的东西需要我去学习 自夸一下 哈哈
在plsql 多了事务处理命令
commit命令
commit事务提交命令。在oracle中为了保证数据的一致性在内存中将为每个客户机建立工作区,就是说在用commit命令之前的操作都在这个工作群里完成,只有在用commit命令之后才会把你写的命令写入到数据库中。
有个自动进行事务提交的命令
set auto on
关闭为 set auto off
rollback命令
rollback是事务回滚命令,在没有提交commit命令千,如果发现delete insert update等操需要恢复的话,可以用rollback命令会滚到上次commit时的状态。
set auto off 要先关闭自动提交
select * from scott.emp;
delete form scott.emp;
rollback
这个时候就可以看到 scott.emp还是以前的没有变化
savepoint命令
这个命令时保存点命令。事务通常由多个命令组成,可以将每个事务划分成若干个部分进行保存,这样回滚每个保存点,就不必回滚整个事务。
创建保存点 savepoint 保存点名
回滚保存点 rollback to 保存点名
来个例子
insert into scott.emp(empno,ename,sal) values(9000,'wang',2500); 先插入一个值
savepoint insertpoint; 创建一个还原点,名字叫insertpoint
rollback to insertpoint; 还原到那个还原点
下面开始说游标
这个东西在mssql里没有吧 我没有印象
游标不是c里面的指针,我一看到这个词就想到了指针可惜何c里面的指针大不一样 不要弄混了 估计没人会弄混。
游标可以说是一个临时的数据存放的地方
要用游标先要定义
cursor 游标名 is select 语句
cursor这是游标的关键字 selcet建立游标的查询命令
看个例子
set serveroutput on
declare
tempsal scott.emp.sal%type 定义了一个变量他是scott.emp.sal同一个类型
cursor mycursor is 定义一个游标mycursor
select * from scott.emp
where sal>tempsal;
begin
tempsal:=800;
open mycursor; 打开这个游标
end;
晕忘了 只是打开游标没有用 还要提取游标的数据
用fetch命令
fetch 游标名 into 变量1,变量2,。。。。;
或者
fetch 游标名 into 记录型变量名;
上面那个程序要改一下
set serveroutput on
declare
tempsal scott.emp.sal%type 定义了一个变量他是scott.emp.sal同一个类型
cursor mycursor is 定义一个游标mycursor
select * from scott.emp
where sal>tempsal
new scott.emp%rowtype; 有定义了一个新的变量
begin
tempsal:=800;
open mycursor; 打开这个游标
fetch mycursor into new; 读取游标数据把他添加到new中
dbms_output._line(to_char(new.sal)); 显示结果
close mysursor; close关闭这个游标
end;
游标的属性
1.%isopen属性
就是判断游标是否打开,如果没有打开就是用fetch语句提示错误
2.%found属性
就是测试前一个fetch语句是否有值,有就返回true 没有就返回false
3.%notfound属性 和上面的相反
4.%rowcount属性 判断游标的数据行数就是有多少数据
下面说下过程的概念 sql里没有
完整的过程的结构如下
create or replace 过程名 as
声明语句段;
begin
执行语句段;
exception
异常处理语句段;
end;
过程是有名称的程序块,as关键词代替了无名块的declare
创建实例的过程
创建一个名为tempprocdeure的过程,create是创建过程的标识符,replace表示如果又同名的过程将覆盖原过程。定义了一个变量,其类型何testtable数据表中的currentdate字段的类型相同,都是日期型,将数据表中的recordnumber字段为88的 currentdate字段内容送入变量中,然后输出结果。
set serveroutput on
creat or replace procedure tempuser.tempprocedure as
tempdate tempuser.testtable.currentdate%type;
begin
select currentdate
into tempdate
from testtable
where recordnumber=88;
dbms_output.put_line(to_char(tempdate));
end;
使用过程
set serveroutput on
begin
tempprocedure;
end;
下面说下带参数的过程
1.参数类型
in 读入参数 程序向过程传递数值
out 读出参数 过程向程序传递数值
in out 双向参数 程序过程互相传递数值
定义带参数的过程
set serveroutput on
creat or replace procedure scott.tempprocedure(
tempdeptno in scott.dept.deptno%type,/*定义了一个in类型的变量*/
tempdname out scott.dept.danme%type,/*定义了一个out类型的变量*/
temploc in out scott.dept.loc%type)as /*定义了一个inout型的变量*/
loc1 scott.dept.doc%type;
dname1 scott.dept.dname%type;
begin
select loc into loc1
from scott.dept
where deptno=tempdeptno;
select danme into danme1
from scott.dept
where deptno=tempdeptno;
temploc:='地址'||loc1;
tempdname:='姓名'||dname1;
end;
定义好了 下面开始用了
set serveroutput on
declare
myno scott.dept.deptno%type;
mydname scott.dept.dname%type;
myloc scott.dept.loc%type;
begin
myno:=10;
mydname:=”;
myloc:=”;
scott.tempprocedure(myno,mydname,myloc);
dbms_output.put_line(myno);
dbms_output.put_line(mydname);
dbms_output.put_line(myloc);
end;
搞定了
就是说用重新定义的三个变量当参数传递给上面定义的过程过程里带参数的变量可以接受这三个变量的值
用java语言来解释就是那个过程就是类 带三个参数
这三个变量就是数据 当然没有对象了哈哈毕竟不是java么哈哈
今天写到这里了 我要下班了 7.3
异常处理
就是程序中要处理特殊情况的办法
1. 定义异常处理
定义异常处理的语法如下:
declare
异常名 exception;
2. 触发异常处理
触发异常处理的语法如下:
raise 异常名;
3. 处理异常
触发异常处理后,可以定义异常处理部分,语法如下:
Exception
When 异常名 1 then
异常处理语句段 1;
When 异常名 2 then
异常处理语句段 2;
下面的 PL/SQL 程序包含了完整的异常处理定义、触发、处理的过程。定义名为 salaryerror
的异常,在 scott.emp 数据表中查找 empno=7566 的记录,将其值放入变量 tempsal 中,判断
tempsal 值若不在 900 和2600 之间,说明该员工的薪水有问题,将激活异常处理,提示信息。
set serveroutput on
declare
salaryerror exception;
tempsal scott.emp.sal%type;
begin
select sal into tempsal
from scott.emp
where empno=7566;
if tempsal <900 or tempsal>2600 then
raise salaryerror;
end if;
exception
when salaryerror then
dbms_output.put_line('薪水超出范围');
end;

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com