为什么 ANSI_NULLS 和 ANSI_WARNINGS 需要为 PHP 中的链接服务器查询设置为 on?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16818120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does ANSI_NULLS and ANSI_WARNINGS need to be set to on for linked server queries in PHP?
提问by Lloyd Banks
I have a linked server statement using OpenQuery:
我有一个使用 OpenQuery 的链接服务器语句:
SELECT mycol1, mycol2, mycol3
FROM OPENQUERY(MYLINKEDSERVER, 'SELECT * FROM mysqldb.table')
The above works with no qualms in SSMS. When I use PHP MySQL wrappers to run this query on a web application (using same SQL Server credentials), I have to insert the following 2 statements:
以上在 SSMS 中没有任何疑虑。当我使用 PHP MySQL 包装器在 Web 应用程序上运行此查询时(使用相同的 SQL Server 凭据),我必须插入以下 2 条语句:
SET ANSI_NULLS ON
SET ANSI_WARNINGS ON
设置 ANSI_NULLS ON
设置 ANSI_WARNINGS ON
I've read through the definitions for ANSI_NULLS and ANSI_WARNINGS, but don't quite understand why they have to be set to on for the query to function in PHP.
我已经通读了 ANSI_NULLS 和 ANSI_WARNINGS 的定义,但不太明白为什么必须将它们设置为 on 才能使查询在 PHP 中运行。
Does anyone know the reason?
有谁知道原因?
My linked server is a MySQL instance.
我的链接服务器是一个 MySQL 实例。
采纳答案by Lloyd Banks
Did some digging. In short, it means that if you're running a query against a Linked Server that isn't a SQL Server box, then more than likely you'll need to turn these two features on to standardize the way certain comparison operators are handled across different databases.
做了一些挖掘。简而言之,这意味着如果您正在对不是 SQL Server 框的链接服务器运行查询,那么您很可能需要打开这两个功能以标准化处理某些比较运算符的方式不同的数据库。
Supposedly, future versions of SQL Server will have SET_NULLS set to ON by default with no option to change it
据说,未来版本的 SQL Server 将默认将 SET_NULLS 设置为 ON,没有选项可以更改它
回答by Jesenko Sokoljak
In PHP:
在 PHP 中:
- First isssue the SET command ?SET ANSI_NULLS ON; SET ANSI_WARNINGS ON;“
- Then execute other commands like SELECT * FROM customers ....;
- 首先发出 SET 命令 ?SET ANSI_NULLS ON; 设置 ANSI_WARNINGS ON;“
- 然后执行其他命令,如 SELECT * FROM customers ....;
If you combine them all in one script:
如果将它们全部组合在一个脚本中:
SET ANSI_NULLS ON;
SET ANSI_WARNINGS ON;
SELECT * FROM customers ...
the error is issued. Works for me combining two MS SQL servers.
错误被发出。结合两个 MS SQL 服务器对我有用。
回答by Pondlife
This is almost a duplicate of this question. Note that the error message returned in that case was:
这几乎是这个问题的重复。请注意,在这种情况下返回的错误消息是:
Heterogeneous queries require the ANSI_NULLS and ANSI_WARNINGS options to be set for the connection. This ensures consistent query semantics. Enable these options and then reissue your query
异构查询需要为连接设置 ANSI_NULLS 和 ANSI_WARNINGS 选项。这确保了一致的查询语义。启用这些选项,然后重新发出您的查询
And the documentationdoes in fact say that:
事实上,文档确实说:
SET ANSI_NULLS should be set to ON for executing distributed queries
SET ANSI_NULLS 应设置为 ON 以执行分布式查询
It has the same commentfor SET ANSI_WARNINGS. I suspect that as álvaro suggested, your PHP code is hiding an error and there's a very good chance that it's the one mentioned above. This questionon the DBA site goes into much more detail.
它对SET ANSI_WARNINGS具有相同的注释。我怀疑正如 álvaro 所建议的那样,您的 PHP 代码隐藏了一个错误,而且很有可能就是上面提到的那个错误。DBA 站点上的这个问题更详细。