Skip to content

Commit 146044f

Browse files
committed
feat: display approximate row count of tables in database tab on MS SQL
closes #1877
1 parent d8cd61c commit 146044f

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

source/dbconnection.pas

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6822,7 +6822,7 @@ function TDBConnection.GetRowCount(Obj: TDBObject; ForceExact: Boolean=False): I
68226822
Rows: String;
68236823
begin
68246824
// Get row number from a table
6825-
Rows := GetVar('SELECT COUNT(*) FROM '+QuoteIdent(Obj.Database)+'.'+QuoteIdent(Obj.Name), 0);
6825+
Rows := GetVar('SELECT COUNT(*) FROM '+QuotedDbAndTableName(Obj.Database, Obj.Name), 0);
68266826
Result := MakeInt(Rows);
68276827
end;
68286828

@@ -6847,15 +6847,16 @@ function TSqlSrvConnection.GetRowCount(Obj: TDBObject; ForceExact: Boolean=False
68476847
Rows: String;
68486848
begin
68496849
// Get row number from a mssql table
6850-
if ServerVersionInt >= 900 then begin
6850+
if (ServerVersionInt < 900) or ForceExact then begin
6851+
Result := inherited
6852+
end
6853+
else begin
68516854
Rows := GetVar('SELECT SUM('+QuoteIdent('rows')+') FROM '+QuoteIdent('sys')+'.'+QuoteIdent('partitions')+
68526855
' WHERE '+QuoteIdent('index_id')+' IN (0, 1)'+
68536856
' AND '+QuoteIdent('object_id')+' = object_id('+EscapeString(Obj.Database+'.'+Obj.Schema+'.'+Obj.Name)+')'
68546857
);
6855-
end else begin
6856-
Rows := GetVar('SELECT COUNT(*) FROM '+Obj.QuotedDbAndTableName);
6858+
Result := MakeInt(Rows);
68576859
end;
6858-
Result := MakeInt(Rows);
68596860
end;
68606861
{$ENDIF}
68616862

@@ -7424,13 +7425,19 @@ procedure TSqlSrvConnection.FetchDbObjects(db: String; var Cache: TDBObjectList)
74247425
// Tables, views and procedures
74257426
Results := nil;
74267427
// Schema support introduced in MSSQL 2005 (9.0). See issue #3212.
7428+
// RowsInTable added in 12.16
74277429
SchemaSelect := EscapeString('');
74287430
if ServerVersionInt >= 900 then
74297431
SchemaSelect := 'SCHEMA_NAME('+QuoteIdent('schema_id')+')';
74307432
try
7431-
Results := GetResults('SELECT *, '+SchemaSelect+' AS '+EscapeString('schema')+
7432-
' FROM '+QuoteIdent(db)+GetSQLSpecifity(spDbObjectsTable)+
7433-
' WHERE '+QuoteIdent('type')+' IN ('+EscapeString('P')+', '+EscapeString('U')+', '+EscapeString('V')+', '+EscapeString('TR')+', '+EscapeString('FN')+', '+EscapeString('TF')+', '+EscapeString('IF')+')');
7433+
Results := GetResults('SELECT o.*, '+SchemaSelect+' AS '+EscapeString('schema')+', rc.RowsInTable'+
7434+
' FROM '+QuoteIdent(db)+GetSQLSpecifity(spDbObjectsTable)+ ' AS o'+
7435+
' LEFT JOIN ('+
7436+
' SELECT object_id, SUM(rows) AS RowsInTable FROM '+QuoteIdent(db)+'.sys.partitions'+
7437+
' WHERE index_id IN (0,1)'+ // -- heap or clustered index
7438+
' GROUP BY object_id'+
7439+
' ) AS rc ON rc.object_id = o.object_id'+
7440+
' WHERE o.'+QuoteIdent('type')+' IN ('+EscapeString('P')+', '+EscapeString('U')+', '+EscapeString('V')+', '+EscapeString('TR')+', '+EscapeString('FN')+', '+EscapeString('TF')+', '+EscapeString('IF')+')');
74347441
except
74357442
on E:EDbError do;
74367443
end;
@@ -7454,6 +7461,8 @@ procedure TSqlSrvConnection.FetchDbObjects(db: String; var Cache: TDBObjectList)
74547461
obj.NodeType := lntTrigger
74557462
else if (tp = 'FN') or (tp = 'TF') or (tp = 'IF') then
74567463
obj.NodeType := lntFunction;
7464+
obj.Rows := StrToInt64Def(Results.Col('RowsInTable'), -1);
7465+
obj.RowsAreExact := False; // approximate, not guaranteed exact.
74577466
// Set reasonable default value for calculation of export chunks. See #343
74587467
// OFFSET..FETCH supported from v11.0/2012
74597468
// Disabled, leave at -1 and prefer a generic calculation in TfrmTableTools.DoExport

0 commit comments

Comments
 (0)