修复: _limit作为判断依据

使用_limit作为判断依据
This commit is contained in:
CLKCLK 2021-09-14 12:04:23 +08:00
parent 9c4ea7ef89
commit 5ed9888fa1

View File

@ -42,15 +42,16 @@ namespace FreeSql.Odbc.SqlServer
if (_skip <= 0 && _limit > 0) sb.Append("TOP ").Append(_limit).Append(" "); if (_skip <= 0 && _limit > 0) sb.Append("TOP ").Append(_limit).Append(" ");
sb.Append(field); sb.Append(field);
if (string.IsNullOrEmpty(_orderby)) if (_limit > 0)
{ {
var pktb = _tables.Where(a => a.Table.Primarys.Any()).FirstOrDefault(); if (string.IsNullOrEmpty(_orderby))
if (pktb != null) _orderby = string.Concat(" \r\nORDER BY ", pktb.Alias, ".", _commonUtils.QuoteSqlName(pktb?.Table.Primarys.First().Attribute.Name)); {
else _orderby = string.Concat(" \r\nORDER BY ", _tables.First().Alias, ".", _commonUtils.QuoteSqlName(_tables.First().Table.Columns.First().Value.Attribute.Name)); var pktb = _tables.Where(a => a.Table.Primarys.Any()).FirstOrDefault();
} if (pktb != null) _orderby = string.Concat(" \r\nORDER BY ", pktb.Alias, ".", _commonUtils.QuoteSqlName(pktb?.Table.Primarys.First().Attribute.Name));
if (_skip > 0) else _orderby = string.Concat(" \r\nORDER BY ", _tables.First().Alias, ".", _commonUtils.QuoteSqlName(_tables.First().Table.Columns.First().Value.Attribute.Name));
{ }
sb.Append(", ROW_NUMBER() OVER(").Append(_orderby).Append(") AS __rownum__"); if (_skip > 0) // 注意这个判断,大于 0 才使用 ROW_NUMBER ,否则属于第一页直接使用 TOP
sb.Append(", ROW_NUMBER() OVER(").Append(_orderby).Append(") AS __rownum__");
} }
sb.Append(" \r\nFROM "); sb.Append(" \r\nFROM ");
var tbsjoin = _tables.Where(a => a.Type != SelectTableInfoType.From).ToArray(); var tbsjoin = _tables.Where(a => a.Type != SelectTableInfoType.From).ToArray();