Can there be a database-agnostic SQL query to fetch top N rows?

后端 未结 5 795
有刺的猬
有刺的猬 2021-01-28 05:33

We want to be able to select top N rows using a SQL Query. The target database could be Oracle or MySQL. Is there an elegant approach to this? (Needless to say, we\'re dealing w

5条回答
  •  时光说笑
    2021-01-28 06:38

    I don't think that's possible even just between mysql and mssql. I do an option for simulating such behaviour though:

    1. create views that have an auto incremented int column; say 'PagingHelperID'
    2. write queries like: SELECT columns FROM viewname WHERE PagingHelperID BETWEEN startindex AND stopindex

    This will make ordering difficult, you will need different views for every order in which you intend to retreive data.

    You could also "rewrite" your sql on the fly when querying depending on the database and define your own method for the rewriter, but I don't think there is any "good" way to do this.

提交回复
热议问题