SQL Server 开始/结束与开始传输/提交/回滚

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2730341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 06:06:52  来源:igfitidea点击:

SQL Server BEGIN/END vs BEGIN TRANS/COMMIT/ROLLBACK

sqlsql-servertransactions

提问by Rich

I have been trying to find info on the web about the differences between these statements, and it seems to me they are identical but I can't find confirmation of that or any kind of comparison between the two.

我一直试图在网络上查找有关这些陈述之间差异的信息,在我看来它们是相同的,但我找不到对此的确认或两者之间的任何比较。

What is the difference between doing this:

这样做有什么区别:

BEGIN
    -- Some update, insert, set statements
END

and doing this

并这样做

BEGIN TRANS
    -- Some update, insert, set statements
COMMIT TRANS

?

?

Note that there is only the need to rollback in the case of some exception or timeout or other general failure, there would not be a conditional reason to rollback.

请注意,只有在某些异常或超时或其他一般故障的情况下才需要回滚,不会有条件回滚的原因。

回答by Paul Williams

BEGIN and END deal with code blocks. They are similar to the curly braces you see in many languages:

BEGIN 和 END 处理代码块。它们类似于您在许多语言中看到的花括号:

if (somethingIsTrue)
{ // like BEGIN
    // do something here
} // like END

In SQL, this is:

在 SQL 中,这是:

if somethingIsTrue
BEGIN
    -- do something here
END

BEGIN TRAN, COMMIT, and ROLLBACKbegin and end transactions. They do not specify a new block of code; they only mark the transaction boundaries.

BEGIN TRAN, COMMIT, 以及ROLLBACK开始和结束事务。它们不指定新的代码块;它们只标记事务边界。

Note that you can write a BEGIN TRANand COMMITin separate blocks of code. For example, if you want code to be part of a transaction, but you don't want to start a new one if the code is already in a transaction, you can do something like this:

注意,你可以写一个BEGIN TRAN,并COMMIT在单独的代码块。例如,如果您希望代码成为事务的一部分,但如果代码已经在事务中,您又不想开始新的事务,则可以执行以下操作:

declare @TranStarted bit = 0
if @@trancount = 0
begin
    set @TranStarted = 1
    begin tran
end

-- ... do work ...

if @TranStarted = 1
begin
    commit
    set @TranStarted = 0
end

回答by Mike Mooney

The regular BEGIN and END are not used for transactions. Instead, they are just for indicating that some block of code is a single unit, much like braces {} in C#/C++/Java.

常规的 BEGIN 和 END 不用于事务。相反,它们只是为了表明某些代码块是一个单元,就像 C#/C++/Java 中的大括号 {}。

If you have an IF statement or a WHILE loop that does 10 things, you need to enclose them in BEGIN/END so that SQL Server knows that that whole list of 10 statements should be executed as a part of that condition.

如果您有一个 IF 语句或一个执行 10 件事的 WHILE 循环,您需要将它们括在 BEGIN/END 中,以便 SQL Server 知道应该作为该条件的一部分执行 10 个语句的整个列表。

回答by Jamiec

These 2 statements are entirely different.

这两种说法完全不同。

BEGIN..ENDmark a block of code, eg in an if statement

BEGIN..END标记一个代码块,例如在 if 语句中

IF @something = 1
BEGIN
  -- Do something when @something is equal to 1
END

BEGIN TRANS..COMMIT TRANSwrap the enclosing block in a transaction, and depending on server settings will rollback the transaction if an error occurs.

BEGIN TRANS..COMMIT TRANS将封闭块包装在事务中,如果发生错误,将根据服务器设置回滚事务。

回答by Sithlord

It should be mentioned, that there is a Begin; in PostgreSQL, that also initiates a transaction block, which at first confused me.

应该提到的是,有一个开始;在 PostgreSQL 中,这也会启动一个事务块,起初这让我感到困惑。

http://www.postgresql.org/docs/9.0/static/sql-begin.html

http://www.postgresql.org/docs/9.0/static/sql-begin.html

"BEGIN initiates a transaction block, that is, all statements after a BEGIN command will be executed in a single transaction until an explicit COMMIT or ROLLBACK is given. By default (without BEGIN), PostgreSQL executes transactions in "autocommit" mode, that is, each statement is executed in its own transaction and a commit is implicitly performed at the end of the statement (if execution was successful, otherwise a rollback is done)."

“BEGIN 启动一个事务块,即在一个 BEGIN 命令之后的所有语句都将在一个事务中执行,直到给出显式的 COMMIT 或 ROLLBACK。默认情况下(没有 BEGIN),PostgreSQL 以“自动提交”模式执行事务,即,每个语句都在其自己的事务中执行,并且在语句末尾隐式执行提交(如果执行成功,则回滚完成)。”

回答by Md. Parvez Alam

I have not seen END TRANS :)

我还没有看到 END TRANS :)

i think we use END only for BEGIN keyword not for BEGIN trans we use commit or rollback for BEGIN trans

我认为我们仅将 END 用于 BEGIN 关键字而不用于 BEGIN trans 我们对 BEGIN trans 使用提交或回滚