Oracle 中的Union、Union All、Intersect、Minus
众所周知的几个结果集集合操作命令,今天详细地测试了一下,发现一些问题,记录备考。 假设我们有一个表Student,包括以下字段与数据: drop table student;
create table student
(
id int primary key,
name nvarchar2(50) not null,
score number not null
);
insert into student values(1,'Aaron',78);
insert into student values(2,'Bill',76);
insert into student values(3,'Cindy',89);
insert into student values(4,'Damon',90);
insert into student values(5,'Ella',73);
insert into student values(6,'Frado',61);
insert into student values(7,'Gill',99);
insert into student values(8,'Hellen',56);
insert into student values(9,'Ivan',93);
insert into student values(10,'Jay',90);
commit;
Union和Union All的区别。
select *
from student
where id < 4
union
select *
from student
where id > 2 and id < 6
结果将是
1 Aaron 78
2 Bill 76
3 Cindy 89
4 Damon 90
5 Ella 73
如果换成Union All连接两个结果集,则返回结果是:
1 Aaron 78
2 Bill 76
3 Cindy 89
3 Cindy 89
4 Damon 90
5 Ella 73
可以看到,Union和Union All的区别之一在于对重复结果的处理。
接下来我们将两个子查询的顺序调整一下,改为
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00
相关文档:
create or replace procedure getok
as
cursor mycur is select ids,name from aaa where name not in (select names from ok);
vempno aaa.ids%type;
vename aaa.name%type;
begin
open mycur;
loop
fetch mycur into vempno,vename;
exit when mycur%notfound;
if mycur%found then
insert ......
两个子查询进行集合运行, 条件就是两个查询的字段个数一致,类型一致,字段名可以不同。
SQL> select * from ta;
ID NAME
---------- --------------------
1 gorey
&nb ......
Oracl 数据库也没有个半段表是否存在,存在则删除的语句,经过研究和改写他人的方法先隆重推出绝对能用性的Oracle判断表是否存在,存在则删除方法,在Oracle10g上试验通过。
方法
CREATE OR REPLACE FUNCTION PROC_NAME(T_NAME IN VARCHAR2) RETURN NUMBER IS
V_CNT number;
V_SQL VARCHAR2(100);
......
oracle补丁下载地址
DB 2009-10-28 18:00 阅读56 评论0
字号: 大大 中中 小小
Oracle 11g 11.1.0.7
ftp://updates.oracle.com/6890831/p6890831_111070_AIX5L_1of2.zip
ftp://updates.oracle.com/6890831/p6890831_111070_AIX5L_2of2.zip
ftp://upda ......