ANSI_NULL and QUOTED_IDENTIFIER:

ANSI_NULL:

When ever we are creating or altering thSQL objects like the Stored Procedures and the User Defined Functions at that time most of us use this. Let us understand this better with an example let us create a table and insert some records,
CREATE TABLE #Demo
(
FName VARCHAR(200),
LName Varchar(200)
)
Insert #Demo values('Ravi','Shekhar')
Insert #Demo values('Isha',NULL)
Insert #Demo values('Santosh','Thakur')
 
SQL Server tends to behave differently when ever we use either 
the SET ANSI_NULL ON or the OFF .

SET ANSI_NULL ON;
If the ANSI_NULL is on then the comparison with NULL value with = or <> returns false.
SET ANSI_NULLS ON
SELECT * FROM #Demo WHERE LName = NULL
SELECT * FROM #Demo WHERE LName <> NULL

The above query returns No result just because of the ANSI_NULL was ON.

In such a case we use IS NULL or IS NOT NULL.
SET ANSI_NULLS ON
SELECT * FROM #Demo WHERE LName IS NULL
SELECT * FROM #Demo WHERE LName IS NOT NULL

The Above query returns the rows with NULL/NOT NULL value.

When this setting value is ON then we need to use IS NULL or IS NOT NULL instead of the comparison operator = and <>.

SET ANSI_NULL OFF;
On the other hand if this setting value is OFF then the comparison with the NULL value using = and <> comparison operator returns TRUE.
SET ANSI_NULLS OFF
SELECT * FROM #Demo WHERE LName = NULL
SELECT * FROM #Demo WHERE LName <> NULL

QUOTED_IDENTIFIER:

When SET QUOTED_IDENTIFIER is ON, identifiers can be delimited by double quotation marks. It other words when QUOTED_IDENTIFIER is set to ON then string inside treated as a identifier not as a literal.
Example
 
When SET QUOTED_IDENTIFIER is Off literals can be delimited by double quotation marks. It other words when QUOTED_IDENTIFIER is set to ON then string inside treated as a identifier not as a literal.
Example
 



I think while rebuilding the indexes it got turned off.

ANSI_NULLS:
When SET ANSI_NULLS is ON, a SELECT statement that uses WHERE column_name = NULL returns zero rows even if there are null values in column_name. A SELECT statement that uses WHERE column_name <> NULL returns zero rows even if there are nonnull values in column_name.
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. A SELECT statement that uses WHERE column_name <> NULL returns the rows that have nonnull values in the column. Also, a SELECT statement that uses WHERE column_name <> XYZ_value returns all rows that are not XYZ_value and that are not NULL.
QUOTED_IDENTIFIER
When SET QUOTED_IDENTIFIER is ON, identifiers can be delimited by double quotation marks, and literals must be delimited by single quotation marks. When SET QUOTED_IDENTIFIER is OFF, identifiers cannot be quoted and must follow all Transact-SQL rules for identifiers. For more information, see Database Identifiers. Literals can be delimited by either single or double quotation marks.
When SET QUOTED_IDENTIFIER is ON (default), all strings delimited by double quotation marks are interpreted as object identifiers. Therefore, quoted identifiers do not have to follow the Transact-SQL rules for identifiers. They can be reserved keywords and can include characters not generally allowed in Transact-SQL identifiers. Double quotation marks cannot be used to delimit literal string expressions; single quotation marks must be used to enclose literal strings. If a single quotation mark (') is part of the literal string, it can be represented by two single quotation marks ("). SET QUOTED_IDENTIFIER must be ON when reserved keywords are used for object names in the database.
CONCAT_NULL_YIELDS_NULL
When SET CONCAT_NULL_YIELDS_NULL is ON, concatenating a null value with a string yields a NULL result. For example, SELECT 'abc' + NULL yields NULL. When SET CONCAT_NULL_YIELDS_NULL is OFF, concatenating a null value with a string yields the string itself (the null value is treated as an empty string). For example, SELECT 'abc' + NULL yields abc.
If SET CONCAT_NULL_YIELDS_NULL is not specified, the setting of the CONCAT_NULL_YIELDS_NULL database option applies.

Comments

Popular posts from this blog