@@ -202,6 +202,7 @@ class BaseDialect(abc.ABC):
202202 TYPE_CLASSES : ClassVar [Dict [str , Type [ColType ]]] = {}
203203
204204 PLACEHOLDER_TABLE = None # Used for Oracle
205+ USE_TOP_INSTEAD_LIMIT : bool = False # True for MsSQL or Teradata
205206
206207 def parse_table_name (self , name : str ) -> DbPath :
207208 "Parse the given table name into a DbPath"
@@ -471,7 +472,10 @@ def render_select(self, parent_c: Compiler, elem: Select) -> str:
471472 columns = ", " .join (map (compile_fn , elem .columns )) if elem .columns else "*"
472473 distinct = "DISTINCT " if elem .distinct else ""
473474 optimizer_hints = self .optimizer_hints (elem .optimizer_hints ) if elem .optimizer_hints else ""
474- select = f"SELECT { optimizer_hints } { distinct } { columns } "
475+ if elem .limit_expr is not None and self .USE_TOP_INSTEAD_LIMIT :
476+ select = f"SELECT TOP { elem .limit_expr } { optimizer_hints } { distinct } { columns } "
477+ else :
478+ select = f"SELECT { optimizer_hints } { distinct } { columns } "
475479
476480 if elem .table :
477481 select += " FROM " + self .compile (c , elem .table )
@@ -491,7 +495,7 @@ def render_select(self, parent_c: Compiler, elem: Select) -> str:
491495 if elem .order_by_exprs :
492496 select += " ORDER BY " + ", " .join (map (compile_fn , elem .order_by_exprs ))
493497
494- if elem .limit_expr is not None :
498+ if elem .limit_expr is not None and not self . USE_TOP_INSTEAD_LIMIT :
495499 has_order_by = bool (elem .order_by_exprs )
496500 select += " " + self .offset_limit (0 , elem .limit_expr , has_order_by = has_order_by )
497501
0 commit comments