티스토리 뷰

Microsoft SQL Server

XACT_ABORT

GCB 2021. 4. 30. 13:58

-- Behavior

IF OBJECT_ID(N't2', N'U') IS NOT NULL
DROP TABLE t2;
GO

IF OBJECT_ID(N't1', N'U') IS NOT NULL
DROP TABLE t1;
GO

CREATE TABLE t1
(a INT NOT NULL PRIMARY KEY);
CREATE TABLE t2
(a INT NOT NULL REFERENCES t1(a));
GO

INSERT INTO t1 VALUES (1);
INSERT INTO t1 VALUES (3);
INSERT INTO t1 VALUES (4);
INSERT INTO t1 VALUES (6);
GO

--SET XACT_ABORT OFF;
--GO

--BEGIN TRANSACTION;
--INSERT INTO t2 VALUES (1);
--INSERT INTO t2 VALUES (2); -- Foreign key error.
--INSERT INTO t2 VALUES (3);
--COMMIT TRANSACTION;
--GO

--SET XACT_ABORT ON;
--GO

--BEGIN TRANSACTION;
--INSERT INTO t2 VALUES (4);
--INSERT INTO t2 VALUES (5); -- Foreign key error.
--INSERT INTO t2 VALUES (6);
--COMMIT TRANSACTION;
GO

-- SELECT shows only keys 1 and 3 added.
-- Key 2 insert failed and was rolled back, but
-- XACT_ABORT was OFF and rest of transaction
-- succeeded.
-- Key 5 insert error with XACT_ABORT ON caused
-- all of the second transaction to roll back.

SELECT *
FROM t2;
GO

/*
Under user options 16384
IF ( (16384 & @@options) = 16384 ) PRINT 'XACT_ABORT' -- XACT_ABORT
*/

exec p1

/*
CREATE proc p1
as
SET XACT_ABORT OFF;
BEGIN TRANSACTION;
INSERT INTO t2 VALUES (4);
INSERT INTO t2 VALUES (5); -- Foreign key error.
INSERT INTO t2 VALUES (6);
COMMIT TRANSACTION;
*/

/*
(1 row affected)
Msg 547, Level 16, State 0, Procedure p1, Line 6 [Batch Start Line 4]
The INSERT statement conflicted with the FOREIGN KEY constraint "FK__t2__a__4316F928". The conflict occurred in database "testdb0428", table "dbo.t1", column 'a'.
The statement has been terminated.
(1 row affected)
4
6
*/

exec p2

/*
create proc p2
as
INSERT INTO t2 VALUES (4);
INSERT INTO t2 VALUES (5); -- Foreign key error.
INSERT INTO t2 VALUES (6);
go
*/

/*
(1 row affected)
Msg 547, Level 16, State 0, Procedure p2, Line 4 [Batch Start Line 27]
The INSERT statement conflicted with the FOREIGN KEY constraint "FK__t2__a__4316F928". The conflict occurred in database testdb0428", table "dbo.t1", column 'a'.
4
*/

exec p3

/*
create proc p3
as
BEGIN TRAN
INSERT INTO t2 VALUES (4);
INSERT INTO t2 VALUES (5); -- Foreign key error.
INSERT INTO t2 VALUES (6);
COMMIT TRAN
go
*/

/*
(1 row affected)
Msg 547, Level 16, State 0, Procedure p3, Line 5 [Batch Start Line 47]
The INSERT statement conflicted with the FOREIGN KEY constraint "FK__t2__a__4316F928". The conflict occurred in database "testdb0428", table "dbo.t1", column 'a'.
Nothing -- rolled back whole transaction
*/

-- How to set the user options

EXEC sp_configure 'user options', 0 ;
GO
RECONFIGURE ;
GO

/*
-- network protocol: LPC
set quoted_identifier on
set arithabort off
set numeric_roundabort off
set ansi_warnings on
set ansi_padding on
set ansi_nulls on
set concat_null_yields_null on
set cursor_close_on_commit off
set implicit_transactions off
set language us_english
set dateformat mdy
set datefirst 7
set transaction isolation level read committed
*/

-- ARITHABORT

EXEC sp_configure 'user options', 64 ;
GO

RECONFIGURE ;
GO

/*
-- network protocol: LPC
set quoted_identifier on
set arithabort on
set numeric_roundabort off
set ansi_warnings on
set ansi_padding on
set ansi_nulls on
set concat_null_yields_null on
set cursor_close_on_commit off
set implicit_transactions off
set language us_english
set dateformat mdy
set datefirst 7
set transaction isolation level read committed
*/

-- XACT_ABORT

EXEC sp_configure 'user options', 16384 ;
GO

RECONFIGURE ;
GO

/*
-- network protocol: LPC
set quoted_identifier on
set arithabort off
set numeric_roundabort off
set ansi_warnings on
set ansi_padding on
set ansi_nulls on
set concat_null_yields_null on
set cursor_close_on_commit off
set implicit_transactions off
set language us_english
set dateformat mdy
set datefirst 7
set transaction isolation level read committed
*/

-- NUMERIC_ROUNDABORT

EXEC sp_configure 'user options', 8192 ;
GO

RECONFIGURE ;
GO

/*
-- network protocol: LPC
set quoted_identifier on
set arithabort off
set numeric_roundabort on
set ansi_warnings on
set ansi_padding on
set ansi_nulls on
set concat_null_yields_null on
set cursor_close_on_commit off
set implicit_transactions off
set language us_english
set dateformat mdy
set datefirst 7
set transaction isolation level read committed
*/

-- ARITHABORT
-- NUMERIC_ROUNDABORT
--select 8192+64 = 8256

EXEC sp_configure 'user options', 8256 ;
GO

RECONFIGURE ;
GO

/*
-- network protocol: LPC
set quoted_identifier on
set arithabort on
set numeric_roundabort on
set ansi_warnings on
set ansi_padding on
set ansi_nulls on
set concat_null_yields_null on
set cursor_close_on_commit off
set implicit_transactions off
set language us_english
set dateformat mdy
set datefirst 7
set transaction isolation level read committed
*/

-- how to check

SET NOCOUNT ON
SET QUOTED_IDENTIFIER OFF
SELECT @@options

IF ( (1 & @@options) = 1 ) PRINT 'DISABLE_DEF_CNST_CHK'
IF ( (2 & @@options) = 2 ) PRINT 'IMPLICIT_TRANSACTIONS'
IF ( (4 & @@options) = 4 ) PRINT 'CURSOR_CLOSE_ON_COMMIT'
IF ( (8 & @@options) = 8 ) PRINT 'ANSI_WARNINGS'
IF ( (16 & @@options) = 16 ) PRINT 'ANSI_PADDING'
IF ( (32 & @@options) = 32 ) PRINT 'ANSI_NULLS'
IF ( (64 & @@options) = 64 ) PRINT 'ARITHABORT'
IF ( (128 & @@options) = 128 ) PRINT 'ARITHIGNORE'
IF ( (256 & @@options) = 256 ) PRINT 'QUOTED_IDENTIFIER'
IF ( (512 & @@options) = 512 ) PRINT 'NOCOUNT'
IF ( (1024 & @@options) = 1024 ) PRINT 'ANSI_NULL_DFLT_ON'
IF ( (2048 & @@options) = 2048 ) PRINT 'ANSI_NULL_DFLT_OFF'
IF ( (4096 & @@options) = 4096 ) PRINT 'CONCAT_NULL_YIELDS_NULL'
IF ( (8192 & @@options) = 8192 ) PRINT 'NUMERIC_ROUNDABORT'
IF ( (16384 & @@options) = 16384 ) PRINT 'XACT_ABORT'

Ref.
SET XACT_ABORT (Transact-SQL) - SQL Server | Microsoft Docs

댓글
글 보관함
최근에 달린 댓글
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Total
Today
Yesterday