限制查询获取的记录数
我有一个包含 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Tom Kyte 给出了限制 Oracle 获取记录的通用解决方案。
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.
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.
首先,您的
select
语句应该是有序的,否则不能保证您每次都能获得相同的 100 行。您可以做的是进行动态查询(如果您不需要存储过程,只需构建字符串并用之前的实际数值替换
:start
和:end
执行查询)限制rownum
,可能类似于: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 limitsrownum
, maybe something like: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?