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. check before insert

check before insert

Scheduled Pinned Locked Moved Database
helpquestioncareer
11 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.
  • V Offline
    V Offline
    vanikanc
    wrote on last edited by
    #1

    Hi, This is more for error proofing the flow of insert. Is there a way to check, before an insert if that particular order already exists in the table? we have a job that runs by the hour, and on each hour starts with an empty table. if the process fails in the middle, and i want to start it again, if there are already some rows in the table, not to insert those.

    L R C 3 Replies Last reply
    0
    • V vanikanc

      Hi, This is more for error proofing the flow of insert. Is there a way to check, before an insert if that particular order already exists in the table? we have a job that runs by the hour, and on each hour starts with an empty table. if the process fails in the middle, and i want to start it again, if there are already some rows in the table, not to insert those.

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

      vanikanc wrote:

      Is there a way to check, before an insert if that particular order already exists in the table?

      Yes; the primary key. That's the one that uniquely identifies a tupel/record. Hence, that's what you'd need to check. Most databases will do this automatic and throw an error if the record already exists.

      vanikanc wrote:

      if the process fails in the middle, and i want to start it again, if there are already some rows in the table, not to insert those.

      Select a list of all primary key-values in the table, and skip those inserts.

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]

      L 1 Reply Last reply
      0
      • V vanikanc

        Hi, This is more for error proofing the flow of insert. Is there a way to check, before an insert if that particular order already exists in the table? we have a job that runs by the hour, and on each hour starts with an empty table. if the process fails in the middle, and i want to start it again, if there are already some rows in the table, not to insert those.

        R Offline
        R Offline
        R Giskard Reventlov
        wrote on last edited by
        #3

        Wrap the process in a transaction. If it fails, it will get rolled back and the table will remain empty. At that point you can figure out what went wrong, take any remedial action and run the process again.

        "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair. nils illegitimus carborundum me, me, me

        V C 2 Replies Last reply
        0
        • R R Giskard Reventlov

          Wrap the process in a transaction. If it fails, it will get rolled back and the table will remain empty. At that point you can figure out what went wrong, take any remedial action and run the process again.

          "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair. nils illegitimus carborundum me, me, me

          V Offline
          V Offline
          vanikanc
          wrote on last edited by
          #4

          I was planning to put BEGIN TRANSACTION and COMMIT TRANSACTION, but what I am doing is reading from a file, inserting into the database, reading the next line and inserting into database. I have the insert statement in the C# page.

          R 1 Reply Last reply
          0
          • V vanikanc

            I was planning to put BEGIN TRANSACTION and COMMIT TRANSACTION, but what I am doing is reading from a file, inserting into the database, reading the next line and inserting into database. I have the insert statement in the C# page.

            R Offline
            R Offline
            R Giskard Reventlov
            wrote on last edited by
            #5

            Thsi post might be helpful: transactions with C# and class library[^] or this one: Transactions in .net[^].

            "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair. nils illegitimus carborundum me, me, me

            1 Reply Last reply
            0
            • R R Giskard Reventlov

              Wrap the process in a transaction. If it fails, it will get rolled back and the table will remain empty. At that point you can figure out what went wrong, take any remedial action and run the process again.

              "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair. nils illegitimus carborundum me, me, me

              C Offline
              C Offline
              Chris Meech
              wrote on last edited by
              #6

              :thumbsup: Really good answer mark. This will stop the problem from ocurring in the first place. :)

              Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

              R 1 Reply Last reply
              0
              • C Chris Meech

                :thumbsup: Really good answer mark. This will stop the problem from ocurring in the first place. :)

                Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

                R Offline
                R Offline
                R Giskard Reventlov
                wrote on last edited by
                #7

                Thanks.

                "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair. nils illegitimus carborundum me, me, me

                1 Reply Last reply
                0
                • L Lost User

                  vanikanc wrote:

                  Is there a way to check, before an insert if that particular order already exists in the table?

                  Yes; the primary key. That's the one that uniquely identifies a tupel/record. Hence, that's what you'd need to check. Most databases will do this automatic and throw an error if the record already exists.

                  vanikanc wrote:

                  if the process fails in the middle, and i want to start it again, if there are already some rows in the table, not to insert those.

                  Select a list of all primary key-values in the table, and skip those inserts.

                  Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]

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

                  Dude, if he is inserting from a file, then there is no primary key until it is inserted :| -- Aah, good point. We don't show the Autoincrement-value to the user, so the user is using a combination of fields to uniquely identify a record. That used to be the primary-key, until we switched to artificial autoincrement-keys. You're reading the file on a line to line basis? Don't want it in memory completely, because it'd have to be restarted completely if the process dies half way. It'd be an option to write the "current amount of processed records" to another file. If it crashes, read that file and see how many lines you can safely skip. A transaction (as said below) is indeed the best idea :) Also, it'd be wise to load the file in a separate table first, and move it from there to the required structure.

                  Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]

                  1 Reply Last reply
                  0
                  • V vanikanc

                    Hi, This is more for error proofing the flow of insert. Is there a way to check, before an insert if that particular order already exists in the table? we have a job that runs by the hour, and on each hour starts with an empty table. if the process fails in the middle, and i want to start it again, if there are already some rows in the table, not to insert those.

                    C Offline
                    C Offline
                    Corporal Agarn
                    wrote on last edited by
                    #9

                    If you are using SQL server 2008 look at the MERGE command. It will Update or Insert. As to the failure, as stated look at transactions or possibly Try Catch.

                    V 1 Reply Last reply
                    0
                    • C Corporal Agarn

                      If you are using SQL server 2008 look at the MERGE command. It will Update or Insert. As to the failure, as stated look at transactions or possibly Try Catch.

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

                      Thank you. I am using the begin transaction/commit or rollback in catch block. Only thing, you have to specify the transaction as part of the sqlcommand object.

                      C 1 Reply Last reply
                      0
                      • V vanikanc

                        Thank you. I am using the begin transaction/commit or rollback in catch block. Only thing, you have to specify the transaction as part of the sqlcommand object.

                        C Offline
                        C Offline
                        Corporal Agarn
                        wrote on last edited by
                        #11

                        You can use the begin tran or try catch within the T-SQL. If you raise an error on failure you can then have C# rerun the data if that is what you want.

                        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