修复: sqlserver row_number排序不一致

sqlserver 使用row_number进行分页时 第一页和后续几页排序不一致的问题
This commit is contained in:
CLKCLK 2021-09-14 11:04:53 +08:00
parent 3b4029658d
commit 9c4ea7ef89

View File

@ -41,14 +41,15 @@ namespace FreeSql.Odbc.SqlServer
//if (_limit > 0) sb.Append("TOP ").Append(_skip + _limit).Append(" "); //TOP 会引发 __rownum__ 无序的问题 //if (_limit > 0) sb.Append("TOP ").Append(_skip + _limit).Append(" "); //TOP 会引发 __rownum__ 无序的问题
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))
{
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));
else _orderby = string.Concat(" \r\nORDER BY ", _tables.First().Alias, ".", _commonUtils.QuoteSqlName(_tables.First().Table.Columns.First().Value.Attribute.Name));
}
if (_skip > 0) if (_skip > 0)
{ {
if (string.IsNullOrEmpty(_orderby))
{
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));
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__"); sb.Append(", ROW_NUMBER() OVER(").Append(_orderby).Append(") AS __rownum__");
} }
sb.Append(" \r\nFROM "); sb.Append(" \r\nFROM ");