Hack for MS SQL limit/offset query without ORDER BY clause

This commit is contained in:
Sergey Kurt 2017-03-14 14:24:03 +03:00
parent e3254c1851
commit b783311248

View file

@ -328,6 +328,16 @@ func writeModifiers(q *Query, buf *bytes.Buffer, args *[]interface{}) {
// OFFSET N ROWS
// FETCH NEXT M ROWS ONLY
if q.offset != 0 {
// Hack from https://www.microsoftpressstore.com/articles/article.aspx?p=2314819
// ...
// As mentioned, the OFFSET-FETCH filter requires an ORDER BY clause. If you want to use arbitrary order,
// like TOP without an ORDER BY clause, you can use the trick with ORDER BY (SELECT NULL)
// ...
if len(q.orderBy) == 0 {
buf.WriteString(" ORDER BY (SELECT NULL)")
}
fmt.Fprintf(buf, " OFFSET %d", q.offset)
if q.limit != 0 {