Skip to content

fix: strip spurious LIMIT when query already has TOP clause in MSSQL adapter#3

Merged
y11t0 merged 1 commit intoCoreBaseHQ:mainfrom
tqrcisio:fix/mssql-top-clause-strips-spurious-limit
Mar 22, 2026
Merged

fix: strip spurious LIMIT when query already has TOP clause in MSSQL adapter#3
y11t0 merged 1 commit intoCoreBaseHQ:mainfrom
tqrcisio:fix/mssql-top-clause-strips-spurious-limit

Conversation

@tqrcisio
Copy link
Copy Markdown
Contributor

@tqrcisio tqrcisio commented Mar 21, 2026

Problem

When a user writes a T-SQL query using SELECT TOP N ..., the QueryModifier (which uses a MySQL-dialect sqlparser) would append LIMIT 1000 to the query. The existing convertLimitToTop function was supposed to handle this, but it had an early return when TOP was detected — leaving the trailing LIMIT intact.

The result was an invalid query like:

SELECT TOP 10 Code, Name FROM Product LIMIT 1000

Which caused SQL Server to return: Incorrect syntax near 'LIMIT'.

Root Cause

In convertLimitToTop, when hasTopRe matched, the function returned the query unchanged — even if a spurious LIMIT N had already been appended by QueryModifier.

Fix

When TOP is detected and a trailing LIMIT clause is also present, strip the LIMIT clause instead of returning the query untouched.

Test

Verified with a SQL Server query using SELECT TOP 10:

  • Before: mssql: Sintaxe incorreta próxima a 'LIMIT'
  • After: Query executes successfully returning 10 rows

@tqrcisio tqrcisio marked this pull request as ready for review March 21, 2026 18:20
@y11t0 y11t0 merged commit 2b0de9b into CoreBaseHQ:main Mar 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants