如何在带有 MSSQL 的 CakePHP 中使用 TOP 而不是 LIMIT 关键字?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我从 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/