SQL Server - Heterogeneous Queries Require the ANSI_NULLS and ANSI_WARNINGS Options to Be Set for the Connection

Views: 1.411
Reading Time: 3 minutes

Hello people,
Good night!

In today's post I will talk about a not very common error that occurs in SQL Server when trying to execute queries using Linked Server or cross-server statements (Ex: OPENROWSET, OPENQUERY, etc.) and SQL Server returns the following message:

Msg 7405, Level 16, State 1, Line 45
Heterogeneous queries require the ANSI_NULLS and ANSI_WARNINGS options to be set for the connection. This is kept consistent query semantics. Enable these options and then reissue your query.

The first time I saw this error was when my colleague Henrique Mauri, the guy who's been giving me more blog ideas lately, tried create a sequence and use it in a scalar function which was called from a stored procedure within an application written in C # and came across this error message.

This problem occurs especially when you manually disable the ANSI_NULLS and ANSI_WARNINGS statements before using a linked server in SQL Server. Let's understand why this happens and how to solve it.

Why use ANSI_NULLS and ANSI_WARNINGS?

First of all, I need to make it clear that the issue reported in this post only occurs when at least one of these two settings is OFF. I already talked about both in my post SQL Server SET Commands. The default of SQL Server is that they are ON, but many people disable it to avoid alerts when erroneously using some queries in the database.

Usage examples:
SQL Server - SET ANSI_WARNINGS ANSI_NULLS OFF ON

I strongly recommend that you do NOT disable ANSI_NULLS and ANSI_WARNINGS in your routines. In fact, I don't even remember when I last needed to change the default behavior of a query using SET commands (other than using SET LANGUAGE and SET DATEFORMAT).

According to Microsoft's own website, in a future version of SQL Server, ANSI_NULLS will always be ON and any applications that explicitly set the option to OFF will generate an error. Avoid using this feature in new development work and plan to modify applications that currently use it.

Simulating the problem

To simulate this problem, I will provide below a script that will show exactly this error message. Remember to change the name of LinkedServer to the name of your SQL Server instance.

SQL Server - Heterogeneous Queries Require the ANSI_NULLS and ANSI_WARNINGS Options to Be Set for the Connection

Solving the problem

Guys, the solution to this problem is extremely simple and it is already clear from the error message and everything I already said in the post. Just enable the SET ANSI_WARNINGS and SET ANSI_NULLS parameters.

SQL Server - Resolving Solving Heterogeneous Queries Require the ANSI_NULLS and ANSI_WARNINGS Options to Be Set for the 2 Connection

If you have already done this and are still receiving this error message, look for all of your source code, because that's the problem. Make sure you do not have any SP running and that you are disabling one of the two options or some ad hoc code in your application or database access layer. Also check the create command of the Stored Procedures involved, as it may be that in the CREATE / ALTER PROCEDURE command these options have been disabled.

I hope you enjoyed the post.
To the next!