Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does ANSI_NULLS and ANSI_WARNINGS need to be set to on for linked server queries in PHP?

Tags:

php

sql-server

I have a linked server statement using 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:

SET ANSI_NULLS ON

SET 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.

Does anyone know the reason?

My linked server is a MySQL instance.

like image 516
Lloyd Banks Avatar asked May 29 '13 15:05

Lloyd Banks


People also ask

Why we use set Ansi_nulls on?

ANSI_NULLS should be set to ON for executing distributed queries. ANSI_NULLS must also be ON when you are creating or changing indexes on computed columns or indexed views. If SET ANSI_NULLS is OFF, any CREATE, UPDATE, INSERT, and DELETE statements on tables with indexes on computed columns or indexed views will fail.

What is Ansi_nulls?

ANSI_NULLS define the comparison rule for NULL values in SQL Server. When SET ANSI_NULLS is OFF, the Equals (=) and Not Equal To (<>) comparison operators do not follow the ISO standard. A SELECT statement that uses WHERE column_name = NULL returns the rows that have null values in column_name. Example.

What is a heterogeneous query?

Heterogeneous just means different. So a heterogeneous query is in one database and is querying a DIFFERENT database.


1 Answers

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.

Supposedly, future versions of SQL Server will have SET_NULLS set to ON by default with no option to change it

like image 194
Lloyd Banks Avatar answered Oct 13 '22 00:10

Lloyd Banks