ORACLE和SYBASE数据库中实现数据查询条数限制的SQL语句实现

一、概述
对于某些需要通过数据库与大量数据打交道的软件来说,处理性能相当的重要。为了保证软件能够将所有数据处理完而不至于崩溃,分批处理的思想应运而生。分批处理的具体做法是编写SQL语句,每次返回规定条数的数据给软件处理,待这一批数据处理完之后,再接着处理下一批。
本文通过对具体的数据库表(tb_employeeinfo)的操作过程,展示了ORACLE和SYBASE数据库中分批处理SQL语句的编写方法。

二、ORACLE数据库中的处理
首先,建立tb_employeeinfo表,其定义如下:

begin
execute immediate 'drop table tb_employeeinfo CASCADE CONSTRAINTS';
EXCEPTION WHEN OTHERS THEN NULL;
end;

/
create table tb_employeeinfo
(
    employeeno    varchar2(20)     not null,    -- no. of employee
    employeename  varchar2(20)     not null,    -- name of employee
    employeeage   int              not null     -- age of employee
);
create unique index idx1_tb_employeeinfo on tb_employeeinfo(employeeno);
prompt 'create table tb_employeeinfo ok';
commit;

接着,在tb_employeeinfo表中插入7条数据,如下:

insert into tb_employeeinfo(employeeno, employeename, employeeage) values('A1000', 'ZhangSan', 20);
insert into tb_employeeinfo(employeeno, employeename, employeeage) values('A1001', 'LiSi',     21);
insert into tb_employeeinfo(employeeno, employeename, employeeage) values('A1002', 'WangWu',   21);
insert into tb_employeeinfo(employeeno, employeename, employeeage) values('A1003', 'ZhouLiu',  22);
insert into tb_employeeinfo(employeeno, employeename, employeeage) values('A1004', 'SunQi',    22);
insert into tb_employeeinfo(employeeno, employeename, employeeage) values('A1005', 'LiuBa',    23);
insert into tb_employeeinfo(employeeno, employeename, employeeage) values('A1006', 'ChenShi',  25);

如果我们想要一次性从tb_employeeinfo表中查询出5条数据,该如何处理呢?
ORACLE数据库中有一个rownum用在查询(select)语句中来限制每次执行之后返回的数据条数。例如,本次要从tb_employeeinfo表中返回5条数据,则编写SQL语句如下:

select employeeno, employeename, employeeage from tb_employeeinfo where rownum<=5;

执行结果如下:

SQL> select employeeno, employeename, employeeage from tb_employeeinfo where rownum<=5;

EMPLOYEENO    EMPLOYEENAME    EMPLOYEEAGE
A1000         ZhangSan        20
A1001         LiSi            21
A1002         WangWu          21
A1003         ZhouLiu         22
A1004         SunQi           22

三、SYBASE数据库中的处理
首先,建立tb_employeeinfo表,其定义如下:

if exists(select * from sysobjects where name='tb_employeeinfo')
    drop table tb_employeeinfo
go
create table tb_employeeinfo
(
    employeeno    varchar(20)     not null,    -- no. of employee
    employeename  varchar(20)     not null,    -- name of employee
    employeeage   int             not null     -- age of employee
)
go
create unique index idx1_tb_employeeinfo on tb_employeeinfo(employeeno)
go
print 'create table tb_employeeinfo ok'
go

接着,在tb_employeeinfo表中插入7条数据,如下:

insert into tb_employeeinfo(employeeno, employeename, employeeage) values('A1000', 'ZhangSan', 20)
insert into tb_employeeinfo(employeeno, employeename, employeeage) values('A1001', 'LiSi',     21)
insert into tb_employeeinfo(employeeno, employeename, employeeage) values('A1002', 'WangWu',   21)
insert into tb_employeeinfo(employeeno, employeename, employeeage) values('A1003', 'ZhouLiu',  22)
insert into tb_employeeinfo(employeeno, employeename, employeeage) values('A1004', 'SunQi',    22)
insert into tb_employeeinfo(employeeno, employeename, employeeage) values('A1005', 'LiuBa',    23)
insert into tb_employeeinfo(employeeno, employeename, employeeage) values('A1006', 'ChenShi',  25)

如果我们想要一次性从tb_employeeinfo表中查询出5条数据,该如何处理呢?
在SYBASE数据库中,可以利用“set rowcount X”语句来实现查询条数的限制。例如,本次要从tb_employeeinfo表中返回5条数据,则编写SQL语句如下:

set rowcount 5
select employeeno, employeename, employeeage from tb_employeeinfo
set rowcount 0

执行结果如下:

employeeno           employeename         employeeage 
A1000                ZhangSan             20 
A1001                LiSi                 21 
A1002                WangWu               21 
A1003                ZhouLiu              22 
A1004                SunQi                22

注意,在设置了查询条数为5并查询成功之后,一定要有“set rowcount 0”语句,否则在下次执行的时候,就最多只能返回5条数据。
例如,我们先执行如下语句:

set rowcount 5
select employeeno, employeename, employeeage from tb_employeeinfo

则此时返回的结果与上面一样。
再执行如下语句(本意是要将7条语句都查询出来):

select employeeno, employeename, employeeage from tb_employeeinfo

但此时的结果仍然只返回了5条,与我们的本意不符。
因此,在SYBASE数据库中,“set rowcount X”语句一定要与“set rowcount 0”语句配对使用。

四、总结
相比本文中的数据表,在实际的软件项目中的数据表的字段要更多一些,数据量也要更大一些,在查询语句中也有可能会带有一些其它条件,但基本的SQL语句编写模式是一致的。大家可以参照来编写对应的SQL语句。


本人微信公众号:zhouzxi,请扫描以下二维码:
ORACLE和SYBASE数据库中实现数据查询条数限制的SQL语句实现_第1张图片

你可能感兴趣的:(软件项目实践中的SQL语言一瞥)