简体   繁体   中英

How to Limit # of Rows Returned using Openquery on a Linked Server

I have an AS400 Linked Server, and I'm trying to run some queries and get some sample sets, but I can't figure out how to limit the number of rows returned.

I tried:

SELECT * FROM OPENQUERY(LINKED, 'SELECT * FROM LINKED.APLUS.CUS WHERE CMSUSP != ''S'' LIMIT 100')

and

SELECT * FROM OPENQUERY(LINKED, 'SELECT TOP(100) * FROM LINKED.APLUS.CUS WHERE CMSUSP != ''S'' ')

I know I can add the TOP to the SELECT FROM OPENQUERY, but I'm trying to not have the AS400 return 100k rows when I only need 100.

Based on my experience with an AS400 system, I believe you're looking for FETCH FIRST N ROWS ONLY

SELECT * FROM ... WHERE ... ORDER BY ... FETCH FIRST 100 ROWS ONLY

In your example that would be:

SELECT * FROM OPENQUERY(LINKED, 'SELECT * FROM LINKED.APLUS.CUS WHERE CMSUSP != ''S'' FETCH FIRST 100 ROWS ONLY')

It is recommended to include an ORDER BY to ensure that the results are consistent.

See this question and answer for additional details: How to do SQL select top N ... in AS400

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM