限制查询获取的记录数

发布于 2024-09-18 17:47:36 字数 518 浏览 7 评论 0原文

我有一个包含 100 K 条记录的表。我正在根据这个记录集编写一个 XML 文件。我需要一次将 100 条记录写入一个文件。所以我会有 1000 个单独的文件。

目前,为了限制写入文件的记录数量,我使用

SELECT * FROM TABLE WHERE ROWNUM  < 100;

This 获取 100 条记录并将它们写入文件。当我再次执行此操作时,它将再次获取相同的 100 条记录。有什么办法可以消除已经写入的记录吗?

我想创建一个表,在其中插入已写入文件的每条记录的主键。 因此,我将

SELECT * FROM TABLE WHERE ROWNUM < 100 AND PRIMARYKEY NOT IN (SELECT PRIMARYKEY FROM ANOTHERTABLE);

使用 Oracle 9i 和基于控制台的 C# 应用程序。我使用 ODP .NET 进行连接。

还有其他方法可以完成这个过程吗?

I have a table with 100 K records. I am writing an XML file based on this recordset. I need to write 100 records to a file at a time. So I will have 1000 separate files.

Currently to limit number of records getting written to a file, I am using the

SELECT * FROM TABLE WHERE ROWNUM  < 100;

This fetches 100 records and writes them to a file. When I do this again, it will fetch the same 100 records once again. Is there some way of eliminating the records it has already written?

I thought of creating a table where I will insert the primary key of each record that has been written to a file.
So I will then do

SELECT * FROM TABLE WHERE ROWNUM < 100 AND PRIMARYKEY NOT IN (SELECT PRIMARYKEY FROM ANOTHERTABLE);

I am using Oracle 9i and a console based c# app. I use ODP .NET to make the connection.

Is there any other way to do this process?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

纸短情长 2024-09-25 17:47:36

Tom Kyte 给出了限制 Oracle 获取记录的通用解决方案。

select * 
  from ( select a.*, rownum rnum
           from ( YOUR_QUERY_GOES_HERE -- including the order by ) a
          where rownum <= MAX_ROWS )
 where rnum >= MIN_ROWS

http://asktom.oracle .com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:127412348064

我也同意 Scott Anderson 的观点,在这种情况下只需获取所有记录并使用几个循环。

A general solution for limiting the records fetched by Oracle has been given by Tom Kyte.

select * 
  from ( select a.*, rownum rnum
           from ( YOUR_QUERY_GOES_HERE -- including the order by ) a
          where rownum <= MAX_ROWS )
 where rnum >= MIN_ROWS

http://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:127412348064

I also agree with Scott Anderson that in this case just fetch all the records and use a couple loops.

雪化雨蝶 2024-09-25 17:47:36

首先,您的 select 语句应该是有序的,否则不能保证您每次都能获得相同的 100 行。

您可以做的是进行动态查询(如果您不需要存储过程,只需构建字符串并用之前的实际数值替换 :start:end执行查询)限制 rownum,可能类似于:

select *
from(
    select table.*, rownum rn
    from table
    where rownum rownum < :end
    order by ID) s
where s.rn >= :start

UPDATE

我确实同意 Scott Anderson 的观点,为什么不一次获取所有记录并将记录分成 100 组在你的前端系统中?

First of all, your select statement should be ordered, otherwise you aren't guaranteed to get the same 100 rows every time.

What you could do is have a dynamic query (if you don't want a stored procedure than just build the string and subsitute :start and :end with actual numeric values before executing the query) that limits rownum, maybe something like:

select *
from(
    select table.*, rownum rn
    from table
    where rownum rownum < :end
    order by ID) s
where s.rn >= :start

UPDATE

I do agree with Scott Anderson, why not just get all records at once and separate the records into groups of 100's in your front-end system?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文