<i id='elb9b'><tr id='elb9b'><dt id='elb9b'><q id='elb9b'><span id='elb9b'><b id='elb9b'><form id='elb9b'><ins id='elb9b'></ins><ul id='elb9b'></ul><sub id='elb9b'></sub></form><legend id='elb9b'></legend><bdo id='elb9b'><pre id='elb9b'><center id='elb9b'></center></pre></bdo></b><th id='elb9b'></th></span></q></dt></tr></i><div id='elb9b'><tfoot id='elb9b'></tfoot><dl id='elb9b'><fieldset id='elb9b'></fieldset></dl></div>

    <tfoot id='elb9b'></tfoot>
      <legend id='elb9b'><style id='elb9b'><dir id='elb9b'><q id='elb9b'></q></dir></style></legend>
      • <bdo id='elb9b'></bdo><ul id='elb9b'></ul>

    1. <small id='elb9b'></small><noframes id='elb9b'>

    2. 正确使用 SQL Server 中的事务

      Correct use of transactions in SQL Server(正确使用 SQL Server 中的事务)
    3. <small id='wdQGi'></small><noframes id='wdQGi'>

      • <bdo id='wdQGi'></bdo><ul id='wdQGi'></ul>
          <legend id='wdQGi'><style id='wdQGi'><dir id='wdQGi'><q id='wdQGi'></q></dir></style></legend>
          <tfoot id='wdQGi'></tfoot>

              1. <i id='wdQGi'><tr id='wdQGi'><dt id='wdQGi'><q id='wdQGi'><span id='wdQGi'><b id='wdQGi'><form id='wdQGi'><ins id='wdQGi'></ins><ul id='wdQGi'></ul><sub id='wdQGi'></sub></form><legend id='wdQGi'></legend><bdo id='wdQGi'><pre id='wdQGi'><center id='wdQGi'></center></pre></bdo></b><th id='wdQGi'></th></span></q></dt></tr></i><div id='wdQGi'><tfoot id='wdQGi'></tfoot><dl id='wdQGi'><fieldset id='wdQGi'></fieldset></dl></div>

                  <tbody id='wdQGi'></tbody>

              2. 本文介绍了正确使用 SQL Server 中的事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有 2 个命令,需要它们都正确执行或都不执行.所以我觉得我需要一个事务,但我不知道如何正确使用它.

                I have 2 commands and need both of them executed correctly or none of them executed. So I think I need a transaction, but I don't know how to use it correctly.

                下面的脚本有什么问题?

                What's the problem with the following script?

                BEGIN TRANSACTION [Tran1]
                
                INSERT INTO [Test].[dbo].[T1]
                    ([Title], [AVG])
                VALUES ('Tidd130', 130), ('Tidd230', 230)
                
                UPDATE [Test].[dbo].[T1]
                  SET [Title] = N'az2' ,[AVG] = 1
                  WHERE [dbo].[T1].[Title] = N'az'
                
                COMMIT TRANSACTION [Tran1]
                GO
                

                INSERT命令执行了,但是UPDATE命令有问题.

                The INSERT command is executed, but the UPDATE command has a problem.

                如果其中任何一个命令在执行时出错,我如何实现它以回滚这两个命令?

                How can I implement this to rollback both commands if any of them have an error in execution?

                推荐答案

                添加一个 try/catch 块,如果事务成功则提交更改,如果事务失败则回滚事务:

                Add a try/catch block, if the transaction succeeds it will commit the changes, if the transaction fails the transaction is rolled back:

                BEGIN TRANSACTION [Tran1]
                
                  BEGIN TRY
                
                      INSERT INTO [Test].[dbo].[T1] ([Title], [AVG])
                      VALUES ('Tidd130', 130), ('Tidd230', 230)
                
                      UPDATE [Test].[dbo].[T1]
                      SET [Title] = N'az2' ,[AVG] = 1
                      WHERE [dbo].[T1].[Title] = N'az'
                
                      COMMIT TRANSACTION [Tran1]
                
                  END TRY
                
                  BEGIN CATCH
                
                      ROLLBACK TRANSACTION [Tran1]
                
                  END CATCH  
                

                这篇关于正确使用 SQL Server 中的事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致
                SQL query to group by day(按天分组的 SQL 查询)
                What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
                MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
                MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
                Include missing months in Group By query(在 Group By 查询中包含缺失的月份)

                  <small id='i98ip'></small><noframes id='i98ip'>

                    <tfoot id='i98ip'></tfoot>

                    • <legend id='i98ip'><style id='i98ip'><dir id='i98ip'><q id='i98ip'></q></dir></style></legend>

                        <tbody id='i98ip'></tbody>
                      <i id='i98ip'><tr id='i98ip'><dt id='i98ip'><q id='i98ip'><span id='i98ip'><b id='i98ip'><form id='i98ip'><ins id='i98ip'></ins><ul id='i98ip'></ul><sub id='i98ip'></sub></form><legend id='i98ip'></legend><bdo id='i98ip'><pre id='i98ip'><center id='i98ip'></center></pre></bdo></b><th id='i98ip'></th></span></q></dt></tr></i><div id='i98ip'><tfoot id='i98ip'></tfoot><dl id='i98ip'><fieldset id='i98ip'></fieldset></dl></div>
                        • <bdo id='i98ip'></bdo><ul id='i98ip'></ul>