Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. begin tran in sql server 2008

begin tran in sql server 2008

Scheduled Pinned Locked Moved Database
databasesql-serversysadminhelpquestion
18 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G GuyThiebaut

    Glad it helps. For what is't worth, and I am sure others will disagree with me - don't use the transaction begin, commit rollback system for admin work on the database. Write a select query to test the update, create a backup table of the rows you are about to update, run the update and check the changes. If there is a problem restore the rows using the backup table you created. Using the transaction system to get you out of trouble is bad practise and will one day ;and you in trouble. If you use your current system you are going to lock the rows and there is a real danger that you will forget the commit or you will accidentally run a commit when you did not mean to. How do I know this? I used to do what you are now doing, years ago, and through a considerable experience of embarrassment learnt to use the method I illustrate above. In summary: Don't run an update without testing it and knowing what the results will be. Take a backup, of the rows being updated, to restore the rows in case something goes wrong, which it won't as you fully tested the update with a select statement first.

    “That which can be asserted without evidence, can be dismissed without evidence.”

    ― Christopher Hitchens

    V Offline
    V Offline
    vkEE
    wrote on last edited by
    #8

    Creating the backup table every time an update is executed does not make sense. The database has all these tables _bak and begins to get messy real soon.

    G 1 Reply Last reply
    0
    • V vkEE

      This question is more for my future understanding. Do I have to issue a rollback statement, or since the update is part of begin tran, and since the update failed nothing would update or would it result in a partial update? Thanks.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #9

      vkEE wrote:

      Do I have to issue a rollback statement

      In general, yes. Transactions are pending until either a commit or rollback.

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

      V 1 Reply Last reply
      0
      • L Lost User

        vkEE wrote:

        Do I have to issue a rollback statement

        In general, yes. Transactions are pending until either a commit or rollback.

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

        V Offline
        V Offline
        vkEE
        wrote on last edited by
        #10

        But like GuyThiebaut mentioned, since the update query errored out, the connection would be lost and hence as good as rollback.

        G L 2 Replies Last reply
        0
        • V vkEE

          Creating the backup table every time an update is executed does not make sense. The database has all these tables _bak and begins to get messy real soon.

          G Offline
          G Offline
          GuyThiebaut
          wrote on last edited by
          #11

          I don't think you read my post fully. Before the update run a query like this:

          select * into temp_20140219 from cash_withdrawals where userid = 1234

          You then run your update:

          update cash_withdrawals set withdrawals = -1000000 where id = 1234

          You then check that the update worked correctly:

          select * from cash_withdrawals where userid = 1234

          If everything looks correct you then drop the backup table.

          drop table temp_20140219

          If there is a problem you restore the data:

          delete from cash_withdrawals where id = 1234
          insert into cash_withdrawals
          select * from temp_20140219

          If you carry on with things the way you currently do you will get burnt, maybe not this weeek but it will happen eventually... [EDIT]SQL syntax error corrected[/EDIT]

          “That which can be asserted without evidence, can be dismissed without evidence.”

          ― Christopher Hitchens

          1 Reply Last reply
          0
          • V vkEE

            But like GuyThiebaut mentioned, since the update query errored out, the connection would be lost and hence as good as rollback.

            G Offline
            G Offline
            GuyThiebaut
            wrote on last edited by
            #12

            You are guessing that the data has not been updated. How do you know that the data has not been updated? Because the connection was dropped? Because the Microsoft manual said so or because GuyThiebaut told you so? That is very sloppy thinking and will get you into trouble one day... You need to check your updates and KNOW that the update did or did not happen.

            “That which can be asserted without evidence, can be dismissed without evidence.”

            ― Christopher Hitchens

            V 1 Reply Last reply
            0
            • G GuyThiebaut

              Glad it helps. For what is't worth, and I am sure others will disagree with me - don't use the transaction begin, commit rollback system for admin work on the database. Write a select query to test the update, create a backup table of the rows you are about to update, run the update and check the changes. If there is a problem restore the rows using the backup table you created. Using the transaction system to get you out of trouble is bad practise and will one day ;and you in trouble. If you use your current system you are going to lock the rows and there is a real danger that you will forget the commit or you will accidentally run a commit when you did not mean to. How do I know this? I used to do what you are now doing, years ago, and through a considerable experience of embarrassment learnt to use the method I illustrate above. In summary: Don't run an update without testing it and knowing what the results will be. Take a backup, of the rows being updated, to restore the rows in case something goes wrong, which it won't as you fully tested the update with a select statement first.

              “That which can be asserted without evidence, can be dismissed without evidence.”

              ― Christopher Hitchens

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #13

              GuyThiebaut wrote:

              Using the transaction system to get you out of trouble is bad practise and will one day ;and you in trouble.

              Transactions are great if you need multiple statements to work or fail as if they are one single statement; and yes, one should always take a five-second pause to reflect before doing a commit. To make a short story long.. We do a lot of things on autopilot. I've seen other people drive cars and look around when turning in a bend. They all peek in a similar fashion, starting on the left-shoulder, checking each angle, unto the far right. That routine has been drilled in to protect one from their own autopilot. Check each angle, then turn. Programmers get the same tendencies, and most of us remember each crash and the tendency; still pressing fanatically Ctrl-Shift-S in the days of autorecover? Selecting your entire post and copying it to the clipboard in case of a time-out? We're very familiar with crashes, but there's few resources that explain where there be dragons. So, before committing; check whether you got the correct server (left shoulder), correct database (left screen), correct credentials (mirror), explain to an imaginary friend what your query should do (look ahead) - then explain what each statement actually does (back-mirror), then find someone to blame when it goes wrong (right mirror). Now you can commit. Just don't do it without the five-seconds delay - it works as well as GMails' "undo send" button. It'll save you often, but not often enough to live without backups.

              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

              1 Reply Last reply
              0
              • G GuyThiebaut

                You are guessing that the data has not been updated. How do you know that the data has not been updated? Because the connection was dropped? Because the Microsoft manual said so or because GuyThiebaut told you so? That is very sloppy thinking and will get you into trouble one day... You need to check your updates and KNOW that the update did or did not happen.

                “That which can be asserted without evidence, can be dismissed without evidence.”

                ― Christopher Hitchens

                V Offline
                V Offline
                vkEE
                wrote on last edited by
                #14

                I know the update did not go through because the table has a trigger on the field called updatedate, which updates this field to current datetime when a change occurs, and there were no rows in the database with the time I had performed an update. Thanks.

                G 1 Reply Last reply
                0
                • V vkEE

                  But like GuyThiebaut mentioned, since the update query errored out, the connection would be lost and hence as good as rollback.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #15

                  vkEE wrote:

                  But like GuyThiebaut mentioned, since the update query errored out, the connection would be lost and hence as good as rollback.

                  More importantly; no single part of it was comitted! (a rollback also ends the transaction and cleans it up, but a pending transaction is hardly a disaster - confirming the change by committing it would have been) You can check whether there are pending transactions;

                  SELECT @@TRANCOUNT;

                  Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                  1 Reply Last reply
                  0
                  • V vkEE

                    I know the update did not go through because the table has a trigger on the field called updatedate, which updates this field to current datetime when a change occurs, and there were no rows in the database with the time I had performed an update. Thanks.

                    G Offline
                    G Offline
                    GuyThiebaut
                    wrote on last edited by
                    #16

                    vkEE wrote:

                    and there were no rows in the database with the time I had performed an update

                    ...and you are the only person who could have updated data at that time? It pains me to see you so confident in such a precarious system. At least I wont be there to say "I told you so" when the SHTF the next time you rely on this sort of method to update a large number of rows.

                    “That which can be asserted without evidence, can be dismissed without evidence.”

                    ― Christopher Hitchens

                    1 Reply Last reply
                    0
                    • G GuyThiebaut

                      Those rows are probably going to be locked - when you drop the connection a rollback will automatically take place -> as long as you did not issue a commit in your statement.

                      “That which can be asserted without evidence, can be dismissed without evidence.”

                      ― Christopher Hitchens

                      A Offline
                      A Offline
                      Ananth A
                      wrote on last edited by
                      #17

                      begin tran //insert or update or delete query Commit tran //if insert/update/delete used correct Rollback tran //if insert/update/delete used incorrect

                      G 1 Reply Last reply
                      0
                      • A Ananth A

                        begin tran //insert or update or delete query Commit tran //if insert/update/delete used correct Rollback tran //if insert/update/delete used incorrect

                        G Offline
                        G Offline
                        GuyThiebaut
                        wrote on last edited by
                        #18

                        Have a read of my posts - I am saying that yes this can be done, however there are too many risks involved for me to consider it a suitable pattern for administration on a database.

                        “That which can be asserted without evidence, can be dismissed without evidence.”

                        ― Christopher Hitchens

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups