@@ -241,6 +241,7 @@ class BaseDialect(abc.ABC):
241
241
TYPE_CLASSES : ClassVar [Dict [str , Type [ColType ]]] = {}
242
242
243
243
PLACEHOLDER_TABLE = None # Used for Oracle
244
+ USE_TOP_INSTEAD_LIMIT : bool = False # True for MsSQL or Teradata
244
245
245
246
def parse_table_name (self , name : str ) -> DbPath :
246
247
"Parse the given table name into a DbPath"
@@ -512,7 +513,10 @@ def render_select(self, parent_c: Compiler, elem: Select) -> str:
512
513
columns = ", " .join (map (compile_fn , elem .columns )) if elem .columns else "*"
513
514
distinct = "DISTINCT " if elem .distinct else ""
514
515
optimizer_hints = self .optimizer_hints (elem .optimizer_hints ) if elem .optimizer_hints else ""
515
- select = f"SELECT { optimizer_hints } { distinct } { columns } "
516
+ if elem .limit_expr is not None and self .USE_TOP_INSTEAD_LIMIT :
517
+ select = f"SELECT TOP { elem .limit_expr } { optimizer_hints } { distinct } { columns } "
518
+ else :
519
+ select = f"SELECT { optimizer_hints } { distinct } { columns } "
516
520
517
521
if elem .table :
518
522
select += " FROM " + self .compile (c , elem .table )
@@ -532,7 +536,7 @@ def render_select(self, parent_c: Compiler, elem: Select) -> str:
532
536
if elem .order_by_exprs :
533
537
select += " ORDER BY " + ", " .join (map (compile_fn , elem .order_by_exprs ))
534
538
535
- if elem .limit_expr is not None :
539
+ if elem .limit_expr is not None and not self . USE_TOP_INSTEAD_LIMIT :
536
540
has_order_by = bool (elem .order_by_exprs )
537
541
select += " " + self .offset_limit (0 , elem .limit_expr , has_order_by = has_order_by )
538
542
0 commit comments