由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Database版 - SQL Server set implicit_transaction on
相关主题
大家对 Transaction 是怎么看的?问一个SQL Server的问题
MS T-SQL 问题再现急求答案,多谢。
Set autocommit offTransact SQL问题请教
[转载] Can anyone interpret this simple SQL?sql的2个问题 (转载)
correlated subquery谁能帮我看看这个Insert语句要怎么改一下?
About INSERT IGNORE请教大牛一道有趣的SQL题
Merge table with one single query?求解释
SQL Conditional SelectProblem when using SQL " Insert...." to AutoNumber.
相关话题的讨论汇总
话题: tran话题: go话题: implicit话题: print
进入Database版参与讨论
1 (共1页)
y****9
发帖数: 144
1
Hi, SQL Server friends,
I run the following script, my question is why when I set implict_
transaction on, and I issued" begin tran", then execute an insert, I will
see TRANCOUNT=2?
note: I just start to learn sql server, I may ask some silly questions now
and then, thanks for your help!
------- script start------
USE testdata
GO
IF OBJECT_ID('t1','U') IS NOT NULL
DROP TABLE t1;
GO
CREATE table t1 (a int)
GO
-- IMPLICIT_TRANSACTION OFF - autocommit mode
INSERT INTO t1 VALUES (1)
SELECT 'Tran count in transaction'= @@TRANCOUNT -- see 0
GO
PRINT ' '
PRINT '#### Setting IMPLICIT_TRANSACTIONS ON'
GO
SET IMPLICIT_TRANSACTIONS ON
GO
PRINT ' '
PRINT '#### Use implicit transactions'
GO
-- No BEGIN TRAN needed here.
INSERT INTO t1 VALUES (4)
SELECT 'Tran count in transaction'= @@TRANCOUNT -- see 1
COMMIT TRAN
SELECT 'Tran count outside transaction'= @@TRANCOUNT -- see 0
GO
PRINT ' '
PRINT '#### Use explicit transactions with IMPLICIT_TRANSACTIONS ON'
GO
BEGIN TRAN
INSERT INTO t1 VALUES (5)
SELECT 'Tran count in transaction'= @@TRANCOUNT -- see 2 --- why???!!!
COMMIT TRAN
SELECT 'Tran count outside transaction'= @@TRANCOUNT -- see 1
GO
----- script end ----
i****a
发帖数: 36252
2
it becomes 2 transactions. one is from the server automated transaction from
implicit_transaction, other is from your own begin tran

【在 y****9 的大作中提到】
: Hi, SQL Server friends,
: I run the following script, my question is why when I set implict_
: transaction on, and I issued" begin tran", then execute an insert, I will
: see TRANCOUNT=2?
: note: I just start to learn sql server, I may ask some silly questions now
: and then, thanks for your help!
: ------- script start------
: USE testdata
: GO
: IF OBJECT_ID('t1','U') IS NOT NULL

y****9
发帖数: 144
3
Got it thanks. I learned that every "BEGIN TRANS" increment @@TRANSCOUNT by
1. In my case, there are explicit one and implicit one from the insert, so
the count =2.

from

【在 i****a 的大作中提到】
: it becomes 2 transactions. one is from the server automated transaction from
: implicit_transaction, other is from your own begin tran

1 (共1页)
进入Database版参与讨论
相关主题
Problem when using SQL " Insert...." to AutoNumber.correlated subquery
urgent help! insert value into tableAbout INSERT IGNORE
SQL-Help!Merge table with one single query?
How to insert a string into table? ThanksSQL Conditional Select
大家对 Transaction 是怎么看的?问一个SQL Server的问题
MS T-SQL 问题再现急求答案,多谢。
Set autocommit offTransact SQL问题请教
[转载] Can anyone interpret this simple SQL?sql的2个问题 (转载)
相关话题的讨论汇总
话题: tran话题: go话题: implicit话题: print