如何在带有 MSSQL 的 CakePHP 中使用 TOP 而不是 LIMIT 关键字?

发布于 2024-10-18 20:04:04 字数 694 浏览 1 评论 0原文

CakePHP 构造所有以“LIMIT X”结尾的查询,这是 MySQL 语法,而不是普通 SQL,Microsoft SQL Server 不会接受它。即使您使用 ODBC 驱动程序,它也会生成如下查询:

SELECT "Content"."id" AS "Content_dot_id", 
"Content"."name" AS "Content_dot_name" 
FROM "contents" AS "Content"  WHERE (1=1) LIMIT 1

给出:

37000: [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near 'LIMIT'.

因为您想要:

SELECT TOP 1 "Content"."id" AS "Content_dot_id", 
"Content"."name" AS "Content_dot_name"
FROM "contents" AS "Content"  WHERE (1=1) 

采用“LIMIT”的代码位于 dbo_source.php 中,而不是特定于数据库的文件之一(我认为它是函数限制($limit,$offset = null))。

是否有一些设置可以切换到 TOP 语法而不是 LIMIT?

CakePHP constructs all its queries ending with "LIMIT X", which is the MySQL syntax, not plain SQL and Microsoft SQL Server won't accept it. Even if you're using the ODBC driver it'll generate queries like:

SELECT "Content"."id" AS "Content_dot_id", 
"Content"."name" AS "Content_dot_name" 
FROM "contents" AS "Content"  WHERE (1=1) LIMIT 1

Which gives:

37000: [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near 'LIMIT'.

Because you want:

SELECT TOP 1 "Content"."id" AS "Content_dot_id", 
"Content"."name" AS "Content_dot_name"
FROM "contents" AS "Content"  WHERE (1=1) 

The code that takes on the "LIMIT" is in dbo_source.php rather than one of the DB-specific files (I think it's function limit($limit, $offset = null)).

Is there some setting I can toggle to switch to the TOP syntax instead of LIMIT?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凡尘雨 2024-10-25 20:04:04

我从 Google 缓存版本的旧 CakePHP 故障单中找到的解决方案是“使用 MSSQL dbo”。

因此,您需要做的是将 CakePHP 配置为使用 MSSql 驱动程序,它将开始使用“TOP”语法。这是一篇讨论如何做到这一点的博客文章: http://www.johnmckinzie.com/2007/06/29/sql-server-2005-and-cakephp/

The resolution that I found from a Google cached version of an old CakePHP trouble ticket was "Use the MSSQL dbo".

So what you need to do is to configure CakePHP to use the MSSql Driver and it will start using the "TOP" syntax. Here is a blog post talking about how to do that: http://www.johnmckinzie.com/2007/06/29/sql-server-2005-and-cakephp/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文