SQL Server SET Commands Explained
Below are explanations and examples for commonly used SET commands in SQL Server. Each command controls a specific aspect of query execution or session behavior.
Enables a group of ANSI settings.
SET ANSI_DEFAULTS ON;
-- Automatically enables options like ANSI_NULLS, ANSI_WARNINGS, and ANSI_PADDING.
SELECT * FROM sys.objects;Specifies that new columns default to NOT NULL unless specified otherwise.
SET ANSI_NULL_DFLT_OFF ON;
CREATE TABLE Test (Col1 INT);
-- Col1 defaults to NOT NULL.Specifies that new columns default to NULL unless specified otherwise.
SET ANSI_NULL_DFLT_ON ON;
CREATE TABLE Test (Col1 INT);
-- Col1 defaults to NULL.Determines how SQL Server handles NULL comparisons.
SET ANSI_NULLS ON;
SELECT * FROM Test WHERE Col1 = NULL; -- Returns no rows.
SET ANSI_NULLS OFF;
SELECT * FROM Test WHERE Col1 = NULL; -- Returns rows where Col1 is NULL.Controls padding behavior for CHAR and VARCHAR data types.
SET ANSI_PADDING ON;
CREATE TABLE Test (Col1 CHAR(10));
INSERT INTO Test VALUES ('A');
-- Col1 will store 'A ' (padded).Controls whether certain warnings are issued.
SET ANSI_WARNINGS ON;
-- Raises warnings for divide-by-zero or null-in-aggregate operations.
SELECT 1 / 0; -- Error: Divide by zero.Specifies whether a query is terminated on arithmetic errors.
SET ARITHABORT ON;
SELECT 1 / 0; -- Query is terminated.Determines whether arithmetic errors produce a warning or are ignored.
SET ARITHIGNORE ON;
SELECT 1 / 0; -- No error, result is NULL.Specifies whether concatenating a NULL value results in NULL.
SET CONCAT_NULL_YIELDS_NULL ON;
SELECT 'Hello' + NULL; -- Returns NULL.
SET CONCAT_NULL_YIELDS_NULL OFF;
SELECT 'Hello' + NULL; -- Returns 'Hello'.Sets a binary value for the session.
DECLARE @Info VARBINARY(128) = CAST('SessionData' AS VARBINARY(128));
SET CONTEXT_INFO @Info;
SELECT CONTEXT_INFO();Determines whether cursors close after a transaction is committed.
SET CURSOR_CLOSE_ON_COMMIT ON;
DECLARE CursorTest CURSOR FOR SELECT * FROM sys.objects;
OPEN CursorTest;
COMMIT; -- Cursor is closed.Sets the first day of the week.
SET DATEFIRST 1; -- 1 = Monday.
SELECT DATEPART(WEEKDAY, GETDATE());Defines the format for date inputs.
SET DATEFORMAT DMY;
SELECT CAST('31-12-2025' AS DATETIME); -- Valid.Specifies the priority for resolving deadlocks.
SET DEADLOCK_PRIORITY LOW;
-- Session is chosen as the victim in case of a deadlock.Warns about non-standard SQL usage.
SET FIPS_FLAGGER ENTRY;
-- Raises warnings for non-standard SQL.Returns metadata only without data rows.
SET FMTONLY ON;
SELECT * FROM sys.objects;
-- Returns column metadata only.Forces the query optimizer to process joins in the specified order.
SET FORCEPLAN ON;
SELECT * FROM TableA, TableB WHERE TableA.ID = TableB.ID;Allows explicit values to be inserted into an identity column.
SET IDENTITY_INSERT Test ON;
INSERT INTO Test (ID, Name) VALUES (1, 'Manual');
SET IDENTITY_INSERT Test OFF;Automatically starts a transaction for certain commands.
SET IMPLICIT_TRANSACTIONS ON;
DELETE FROM Test;
-- Transaction is started but not committed.
COMMIT;Sets the language for the session.
SET LANGUAGE French;
SELECT DATENAME(MONTH, GETDATE());
-- Returns month name in French.This file provides quick tutorials and examples for SQL Server SET commands, helping users configure and control query execution and session behaviors effectively.