How to Find a Database’s ANSI_NULLS Setting in SQL Server (T-SQL)

SQL Server has an ANSI_NULLS setting that determines how NULL values are evaluated when compared to another value with the Equals (=) and Not Equal To (<>) comparison operators.

While it’s true that you can change the ANSI_NULLS setting at the session level (using SET ANSI_NULLS), each database also has its own ANSI_NULLS setting.

You can check your database to see whether its ANSI_NULLS setting is ON or OFF.

To do this with T-SQL, you can either use the sys.databases catalog view or the DATABASEPROPERTYEX() function.

Continue reading

Find Out Why an Email Failed to Send in SQL Server (T-SQL)

If you’re trying to send email using Database Mail in SQL Server, but it fails to send, you can check the sysmail_event_log view to see why it failed.

The sysmail_event_log view returns one row for each Windows or SQL Server message returned by the Database Mail system. By “message”, I don’t mean the actual mail message. I mean a message such as the error message that explains why the mail failed.

You can also use the sysmail_configure_sp stored procedure to determine what types of messages are logged.

Continue reading

SQL Server Error 109: There are more columns in the INSERT statement than values specified in the VALUES clause

This is a commonly encountered error in SQL Server when inserting data into a table. The full error goes like this:

Msg 109, Level 15, State 1, Line 1
There are more columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement.

This happens when you specify more columns in the INSERT statement than the number of values that you’re trying to insert with the VALUES clause.

This will occur if you accidentally omit one or more values from the VALUES clause.

You’d get a similar (but technically different) error if you tried to do the opposite – specify fewer columns in the INSERT statement than you try to insert.

Continue reading