Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL Server syntax error after upgrade to Laravel 9 #45295

Closed
beard7 opened this issue Dec 14, 2022 · 4 comments
Closed

SQL Server syntax error after upgrade to Laravel 9 #45295

beard7 opened this issue Dec 14, 2022 · 4 comments

Comments

@beard7
Copy link

beard7 commented Dec 14, 2022

  • Laravel Version: 9.41.0
  • PHP Version: 8.0.18
  • Database Driver & Version: ODBC Driver 17 for SQL Server

Description:

Code which worked prior to updrading to Laravel 9 is now producing an error when querying the database.

Steps To Reproduce:

In Laravel 8, this works as expected:
$posts = Post::Approved()->latest()->with('category', 'user')->where('inside_track', 0)->skip(4)->get()->groupBy('category_id')

The same code in Laravel 9 results in the following error:
SQLSTATE[42000]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near 'only'. (SQL: select * from [posts] where [approved_at] is not null and [inside_track] = 0 order by [created_at] desc offset 4 rows fetch next rows only)

Removing the skip(4) fixes the problem, but I need to skip the records.

@github-actions
Copy link

Thank you for reporting this issue!

As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub.

If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team.

Thank you!

@hafezdivandari
Copy link
Contributor

hafezdivandari commented Dec 25, 2022

According to the following lines, select statement is forced to use skip/offset and take/limit together when ordering. Related to PR #39863

if (! empty($components['orders'])) {
return parent::compileSelect($query)." offset {$query->offset} rows fetch next {$query->limit} rows only";
}

Following changes may resolve this issue:

if (! empty($components['orders'])) {
+    $fetchClause = $query->limit ? " fetch next {$query->limit} rows only" : '';
+
+    return parent::compileSelect($query)." offset {$query->offset} rows".$fetchClause;
-    return parent::compileSelect($query)." offset {$query->offset} rows fetch next {$query->limit} rows only";
}

However, this is already fixed/handled in a better way on Laravel 10 via #44937

@driesvints
Copy link
Member

Looks like this will be done better in Laravel v10. Thanks

@josuechevezUEES
Copy link

Illuminate\ Database\ QueryException

SQL SERVER

PHP 8.2.10
10.22.0
SQLSTATE[42000]: [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Sintaxis incorrecta cerca de 'offset'.
SELECT
*
FROM
[ ED_Evaluaciones ]
ORDER BY
[ IdEvaluacion ] DESC OFFSET 10 ROWS FETCH next 11 ROWS ONLY

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants