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. Is it recommended to use trigger to manage business rules with SQL Server?

Is it recommended to use trigger to manage business rules with SQL Server?

Scheduled Pinned Locked Moved Database
databasequestionsql-serversysadminbusiness
10 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.
  • B Offline
    B Offline
    Bastien Vandamme
    wrote on last edited by
    #1

    What is the cost of a trigger in SQL Server? Could we use them or should we avoid to use them? What are the best practice about triggers? Can we use them as business rules? I'm trying to check the status of a record. I have a table with job offers. Each job offer can have one or more application with differents status. For simplicity let says step 1, 2, 3, 4 and 5. When an application reach the step 5 I must check I have only one application with status 5 by job offer and I must set all other application to step 4. Am I clear? Can I do that or this kind of complex check with a trigger?

    Kornfeld Eliyahu PeterK J M 3 Replies Last reply
    0
    • B Bastien Vandamme

      What is the cost of a trigger in SQL Server? Could we use them or should we avoid to use them? What are the best practice about triggers? Can we use them as business rules? I'm trying to check the status of a record. I have a table with job offers. Each job offer can have one or more application with differents status. For simplicity let says step 1, 2, 3, 4 and 5. When an application reach the step 5 I must check I have only one application with status 5 by job offer and I must set all other application to step 4. Am I clear? Can I do that or this kind of complex check with a trigger?

      Kornfeld Eliyahu PeterK Offline
      Kornfeld Eliyahu PeterK Offline
      Kornfeld Eliyahu Peter
      wrote on last edited by
      #2

      SQL tables and their relationships/indexes/keys are part of the business logic - so you definitely have business rules on SQL. But, there is a certain level of what to put on SQL and what to save for your code... Triggers are very powerful to do other things while something happened to one of your tables, but it has a performance penalty. I saw databases where an insert that should take of a fraction of a millisecond took over 2 seconds because of the complex computations and data changes from within a trigger. Also if you got into the trigger things you can easily get a situation where trigger fires trigger that fires trigger...and so on. A very unpleasant situation to deal with when errors occur... So do not use triggers if you can avoid, try to create stored procedures for complex insert/update scenarios!

      I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

      "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

      G 1 Reply Last reply
      0
      • B Bastien Vandamme

        What is the cost of a trigger in SQL Server? Could we use them or should we avoid to use them? What are the best practice about triggers? Can we use them as business rules? I'm trying to check the status of a record. I have a table with job offers. Each job offer can have one or more application with differents status. For simplicity let says step 1, 2, 3, 4 and 5. When an application reach the step 5 I must check I have only one application with status 5 by job offer and I must set all other application to step 4. Am I clear? Can I do that or this kind of complex check with a trigger?

        J Offline
        J Offline
        Jorgen Andersson
        wrote on last edited by
        #3

        I second Kornfelds opinion that you should avoid triggers. •The trigger continues executing any remaining statements after the ROLLBACK statement. If any of these statements modify data, the modifications are not rolled back.[^]. They are hell to debug.

        Wrong is evil and must be defeated. - Jeff Ello[^]

        1 Reply Last reply
        0
        • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

          SQL tables and their relationships/indexes/keys are part of the business logic - so you definitely have business rules on SQL. But, there is a certain level of what to put on SQL and what to save for your code... Triggers are very powerful to do other things while something happened to one of your tables, but it has a performance penalty. I saw databases where an insert that should take of a fraction of a millisecond took over 2 seconds because of the complex computations and data changes from within a trigger. Also if you got into the trigger things you can easily get a situation where trigger fires trigger that fires trigger...and so on. A very unpleasant situation to deal with when errors occur... So do not use triggers if you can avoid, try to create stored procedures for complex insert/update scenarios!

          I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

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

          Kornfeld Eliyahu Peter wrote:

          trigger fires trigger that fires trigger...and so on.

          With SQL Server there is an inbuilt protection system where you will not get a recursive firing of triggers. I tested this recently as I was concerned about this sort of behaviour.

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

          ― Christopher Hitchens

          Kornfeld Eliyahu PeterK 1 Reply Last reply
          0
          • G GuyThiebaut

            Kornfeld Eliyahu Peter wrote:

            trigger fires trigger that fires trigger...and so on.

            With SQL Server there is an inbuilt protection system where you will not get a recursive firing of triggers. I tested this recently as I was concerned about this sort of behaviour.

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

            ― Christopher Hitchens

            Kornfeld Eliyahu PeterK Offline
            Kornfeld Eliyahu PeterK Offline
            Kornfeld Eliyahu Peter
            wrote on last edited by
            #5

            You are talking about recursive triggers. I'm about nested triggers... ;)

            I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

            "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

            J 1 Reply Last reply
            0
            • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

              You are talking about recursive triggers. I'm about nested triggers... ;)

              I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

              J Offline
              J Offline
              Jorgen Andersson
              wrote on last edited by
              #6

              Does it protect against recursion within your nesting?

              Wrong is evil and must be defeated. - Jeff Ello[^]

              Kornfeld Eliyahu PeterK 1 Reply Last reply
              0
              • J Jorgen Andersson

                Does it protect against recursion within your nesting?

                Wrong is evil and must be defeated. - Jeff Ello[^]

                Kornfeld Eliyahu PeterK Offline
                Kornfeld Eliyahu PeterK Offline
                Kornfeld Eliyahu Peter
                wrote on last edited by
                #7

                No.

                I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

                "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

                J 1 Reply Last reply
                0
                • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

                  No.

                  I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

                  J Offline
                  J Offline
                  Jorgen Andersson
                  wrote on last edited by
                  #8

                  Bugger. :) I'll keep staying clear from triggers then.

                  Wrong is evil and must be defeated. - Jeff Ello[^]

                  Kornfeld Eliyahu PeterK 1 Reply Last reply
                  0
                  • J Jorgen Andersson

                    Bugger. :) I'll keep staying clear from triggers then.

                    Wrong is evil and must be defeated. - Jeff Ello[^]

                    Kornfeld Eliyahu PeterK Offline
                    Kornfeld Eliyahu PeterK Offline
                    Kornfeld Eliyahu Peter
                    wrote on last edited by
                    #9

                    As far as you can!!!

                    I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

                    "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

                    1 Reply Last reply
                    0
                    • B Bastien Vandamme

                      What is the cost of a trigger in SQL Server? Could we use them or should we avoid to use them? What are the best practice about triggers? Can we use them as business rules? I'm trying to check the status of a record. I have a table with job offers. Each job offer can have one or more application with differents status. For simplicity let says step 1, 2, 3, 4 and 5. When an application reach the step 5 I must check I have only one application with status 5 by job offer and I must set all other application to step 4. Am I clear? Can I do that or this kind of complex check with a trigger?

                      M Offline
                      M Offline
                      Mycroft Holmes
                      wrote on last edited by
                      #10

                      I'm with the other 2, their arguments are perfectly valid, and here is another, triggers spit are a support nightmare, digging out an error in a nested trigger can drive you nuts. Triggers spit have their place, they are excellent for auditing and logging changes to your data ONLY.

                      Never underestimate the power of human stupidity RAH

                      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