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. Other Discussions
  3. The Weird and The Wonderful
  4. SQL trigger fun

SQL trigger fun

Scheduled Pinned Locked Moved The Weird and The Wonderful
databasequestionannouncement
6 Posts 3 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.
  • R Offline
    R Offline
    ruanr
    wrote on last edited by
    #1

    CREATE TRIGGER tmTaskChangeHist ON Task FOR UPDATE, INSERT NOT FOR REPLICATION
    as
    begin
    set nocount on
    if not exists (select * from sysobjects where name = 'TaskChangeHist')
    select * into TaskChangeHist from inserted

    else
    insert TaskChangeHist select * from inserted
    set nocount off
    end

    Oh my. On top of this we have strict annotation guidelines at my company, which weren't followed there (note lack of comments :P) Can someone please explain the thought process of checking whether TaskChangeHist exists, and then inserting anyway? (not to mention the use of select *, although i guess it could be justified in this context)

    A 1 Reply Last reply
    0
    • R ruanr

      CREATE TRIGGER tmTaskChangeHist ON Task FOR UPDATE, INSERT NOT FOR REPLICATION
      as
      begin
      set nocount on
      if not exists (select * from sysobjects where name = 'TaskChangeHist')
      select * into TaskChangeHist from inserted

      else
      insert TaskChangeHist select * from inserted
      set nocount off
      end

      Oh my. On top of this we have strict annotation guidelines at my company, which weren't followed there (note lack of comments :P) Can someone please explain the thought process of checking whether TaskChangeHist exists, and then inserting anyway? (not to mention the use of select *, although i guess it could be justified in this context)

      A Offline
      A Offline
      Ashfield
      wrote on last edited by
      #2

      Its fairly clear,

      ruanr wrote:

      if not exists (select * from sysobjects where name = 'TaskChangeHist') select * into TaskChangeHist from inserted

      creates TaskChangeHist if it doesn't exist (and inserts the record(s) from inserted), otherwise just insert the records. The use of * is entirely justified (in my opinion) in this case. My only caveat would be that it will break if the Task table definition changes. Not the best code to place in a trigger, but I can't see why you consider it a coding horror?

      Bob Ashfield Consultants Ltd

      R L 2 Replies Last reply
      0
      • A Ashfield

        Its fairly clear,

        ruanr wrote:

        if not exists (select * from sysobjects where name = 'TaskChangeHist') select * into TaskChangeHist from inserted

        creates TaskChangeHist if it doesn't exist (and inserts the record(s) from inserted), otherwise just insert the records. The use of * is entirely justified (in my opinion) in this case. My only caveat would be that it will break if the Task table definition changes. Not the best code to place in a trigger, but I can't see why you consider it a coding horror?

        Bob Ashfield Consultants Ltd

        R Offline
        R Offline
        ruanr
        wrote on last edited by
        #3

        Oh - I am a bit of a SQL novice, did not realize that 'select * into' will create a table. If that is the case, why is the 'ELSE' clause necessary? And yes, Task got some extra col's, and so caused problems.

        A 1 Reply Last reply
        0
        • R ruanr

          Oh - I am a bit of a SQL novice, did not realize that 'select * into' will create a table. If that is the case, why is the 'ELSE' clause necessary? And yes, Task got some extra col's, and so caused problems.

          A Offline
          A Offline
          Ashfield
          wrote on last edited by
          #4

          Well, if you don't need to create the table you do need to insert into it - hence the else.

          Bob Ashfield Consultants Ltd

          1 Reply Last reply
          0
          • A Ashfield

            Its fairly clear,

            ruanr wrote:

            if not exists (select * from sysobjects where name = 'TaskChangeHist') select * into TaskChangeHist from inserted

            creates TaskChangeHist if it doesn't exist (and inserts the record(s) from inserted), otherwise just insert the records. The use of * is entirely justified (in my opinion) in this case. My only caveat would be that it will break if the Task table definition changes. Not the best code to place in a trigger, but I can't see why you consider it a coding horror?

            Bob Ashfield Consultants Ltd

            L Offline
            L Offline
            Le centriste
            wrote on last edited by
            #5

            The use of a trigger is coding horror by itself.

            A 1 Reply Last reply
            0
            • L Le centriste

              The use of a trigger is coding horror by itself.

              A Offline
              A Offline
              Ashfield
              wrote on last edited by
              #6

              You forgot this :)

              Bob Ashfield Consultants Ltd

              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