1.desc 表名 :显示数据表的结构
2.as 省略了as
比如 mssql中的select name as 姓名 在oracle中 改为 select name 姓名
3.增加了 any, some 的嵌套查询功能 两个参数作用一样 例子如下
select name form class.emp where number>any(select number form student.emp where sex='男')
他查询的是学号大于任何一个男生学号的女生名字
4. 增加了 all 的嵌套查询功能 例子如下
select name form class.emp where number>all(select number form student.emp where sex='男')
他查询的是学号大于所有男生学号的女生名字
5.出现了并操作,交操作,差操作, 例子如下
(select name for class.emp)union (select name for class.dept) 这是并操作意思就是两个查询的并集
(select name for class.emp)intersect (select name for class.dept) 这是交操作意思就是两个查询的交集
(select name for class.emp)minus (select name for class.dept) 这是交操作意思就是两个查询的差
6.数据删除 truncate table 表名 表删除语句 和mssql中大不一样了 告别了delete
现总结这么多 稍后继续
用了一个例子介绍plsql简单语法
set serveroutput on 允许服务器输出
declear 定义局部变量
maxrecords constant int:=100;
i int:=1;
begin 运行局部变量
for i in 1..maxrecords loop
insert into tempuser.testtable(recordnumber,currentdate)
values (i,sysdate); 插入数据
end loop;
dbms_output_line('问世间情为何物?一物降一物');
commit; 提交结果
end; 结束执行
下面是这个程序在mssql里 怎么写 用的时while循环 我觉得mssql里没有loop循环语句
declare
@maxrecords int @i int
@maxrecords = 100 @i =1
begin
while @i<@maxrecords
insert into testtable(recordnumber,currentdate) values (i,sysdate);
print '问世间情为何物?一物降一物';
select @i=@i+1;
end;
比较一下就可以看出来到底那里不一样了
plsql里面命令的结构为
delacre
定义语句段
begin
执行语句段
exception
异常处理语句段
end
这就是plsql程序总体结构图
定义变量与mssql的不同
基本方法
变量名 类型标识符 【notnull】:=值
例 age number(8):=26
多了定义复合数据类型变量的功能
1.多了%type 变量
declare
mydate user。testtable.currentdate%type;
还有 %rowtype类型的变量 可以识变量获得字段的数据类型,使用%rowtype可以识变量获得整个记录的数据类型。
变量名 数据表.列名%type
变量名 数据表%rowtype
declare
mytable testtbale%rowtype 包含了testtable 所有字段 只不过在输出时候可以选择输出那个
begin
shelect * into mytable
from temuuser.tedttbale
where recordnumber=88
dbms_output.put_line(mytable.currentdate);
end;
还有就是有了定义符合变量
格式
type 复合变量名 is record(
变量 类型, 可以有好几个);
变量名 复合变量名 这个变量名就好像java中类的对象一样而复合变量名就是类名可以这样理解 个人观点
begin
select * into 变量名 from 表名 where 条件
dbms_output.put_line(变量名.表中的值)
end
另外还可以定义一维数组
type 表类型 is table of 类型 index by binary_integer
表变量名 表类型
index by binary_integer子句代表以符号整数为索引,
这样访问表类型变量中的数据方法就是“表变量名(索引符号整数)”
Declare
type tabletype1 is table of varchar2(4) index by binary_integer;
type tabletype2 is table of tempuser.testtable.recordnumber%type index by
binary_integer;
table1 tabletype1;
table2 tabletype2;
begin
table1(1):='大学';
table1(2):='大专';
table2(1):=88;
table2(2):=55;
dbms_output.put_line(table1(1)||table2(1));
dbms_output.put_line(table1(2)||table2(2));
end;
一个标准的一维数组
type tabletype1 is table of testtable%rowtype index by binary_integer;
table1 tabletype1;
begin
select * into table1(60)
from tempuser.testtable
where recordnumber=60;
dbms_output.put_line(table1(60).recordnumber||table1(60).currentdate);
end;
在来看下面的这个程序
set serveroutput on
Declare
result integer;
begin
result:=10+3*4-20+5**2;
dbms_output.put_line('运算结果是:'||to_char(result));
end;
2. if..then..else..end if条件控制
if 条件 then
语句段1;
else
语句段2;
end if;
3. if 嵌套条件控制
if 条件1 then
if 条件2 then
语句段1;
else
语句段2;
end if;
else
语句段3;
end if;
4.loop..exit..end loop 循环控制
loop
循环语句段;
if 条件语句 then
exit;
else
退出循环的处理语句段
end if;
end loop;
5. loop..exit..when..end loop 循环控制
采用 loop..exit..when..end loop 循环控制的语法结构与loop..exit..end loop 循环控制类似
exit when 实际上就相当于
if 条件 then
exit;
end if;
6.while..loop..end loop 循环控制
while 条件 loop
执行语句段
end loop;
7.for..in..loop..end 循环控制
for 循环变量 in [reverse] 循环下界..循环上界 loop
循环处理语句段;
end loop;
最后一个出个例子
set serveroutput on
declare
number1 integer:=80;
number2 integer:=90;
i integer:=0;
begin
for i in 1..10 loop
number1:=number1+1; 在mssql里是 sclect @number=@number+1
end loop;
dbms_output.put_line('number1的值:'||to_char(number1));
end;
本人学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;
set serveroutput on
creat or replace procedure tempuser.tempprocedure as
tempdate tempuser.testtable.currentdate%type;
/*定义了一个过程tempprocdeure这个过程有一个变量 tempdate*/
begin
select currentdate
into tempdate
from testtable
where recordnumber=88;/*给过程的变量tempdate赋值*/
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;/*定义了两个变量 loc1 还有 dname1*/
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;
/*利用定义的in型的变量temdptno给loc1还有dname1赋值*/
end;
定义好了 下面开始用了
set serveroutput on
declare
myno scott.dept.deptno%type;
mydname scott.dept.dname%type;
myloc scott.dept.loc%type;
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;