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. How to prevent or allow a insert trigger ?

How to prevent or allow a insert trigger ?

Scheduled Pinned Locked Moved Database
databasehelptutorialquestionannouncement
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.
  • N Offline
    N Offline
    ngoloi
    wrote on last edited by
    #1

    I have a issue when I create a trigger for insert or update Create trigger In_Name on CheckStatus for insert, update as declare @id int Select @id=id from inserted where id=@id if exists (Select * from CheckStatus where id=@id) begin rollback transaction update CheckStatus Set levels=levels+1 where id=@id end else insert into CheckStatus (id,levels) values (@id,1) But the trigger do not execute my opinion (I want to update "levels" when id field exists, inversely inserting id and levels) Please, giving a hand, thanks a lot

    M G 2 Replies Last reply
    0
    • N ngoloi

      I have a issue when I create a trigger for insert or update Create trigger In_Name on CheckStatus for insert, update as declare @id int Select @id=id from inserted where id=@id if exists (Select * from CheckStatus where id=@id) begin rollback transaction update CheckStatus Set levels=levels+1 where id=@id end else insert into CheckStatus (id,levels) values (@id,1) But the trigger do not execute my opinion (I want to update "levels" when id field exists, inversely inserting id and levels) Please, giving a hand, thanks a lot

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

      If your database design needs you to update primary keys (ID Fields) you have bigger problems that trigger design. A primary field should never be modified, with the possible exception of deploying data across different systems.

      Never underestimate the power of human stupidity RAH

      N 1 Reply Last reply
      0
      • M Mycroft Holmes

        If your database design needs you to update primary keys (ID Fields) you have bigger problems that trigger design. A primary field should never be modified, with the possible exception of deploying data across different systems.

        Never underestimate the power of human stupidity RAH

        N Offline
        N Offline
        ngoloi
        wrote on last edited by
        #3

        thanks for answering me. My idea want to update or insert "Levels" fields, @id do not primary keys, it is a foreign keys form other table. I'm a new database studying, please help me.

        1 Reply Last reply
        0
        • N ngoloi

          I have a issue when I create a trigger for insert or update Create trigger In_Name on CheckStatus for insert, update as declare @id int Select @id=id from inserted where id=@id if exists (Select * from CheckStatus where id=@id) begin rollback transaction update CheckStatus Set levels=levels+1 where id=@id end else insert into CheckStatus (id,levels) values (@id,1) But the trigger do not execute my opinion (I want to update "levels" when id field exists, inversely inserting id and levels) Please, giving a hand, thanks a lot

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

          Select @id=id from inserted where id=@id

          There's your issue - @id is going to be null as you have only just declared it in the line above. The trigger does fire it just does not do anything as @id is always going to be null. Get rid of the

          where id=@id

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

          ― Christopher Hitchens

          N 1 Reply Last reply
          0
          • G GuyThiebaut

            Select @id=id from inserted where id=@id

            There's your issue - @id is going to be null as you have only just declared it in the line above. The trigger does fire it just does not do anything as @id is always going to be null. Get rid of the

            where id=@id

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

            ― Christopher Hitchens

            N Offline
            N Offline
            ngoloi
            wrote on last edited by
            #5

            ok, I show all code again after editing CREATE trigger In_LoaiDG1 on KhaoSat_LoaiDG1 for insert,update as declare @NgayDG smalldatetime declare @MaLoaiTT int Select @MaLoaiTT=MaLoaiTT,@NgayDG=NgayDG from inserted if exists (Select count(*) from KhaoSat_LoaiDG1 where KhaoSat_LoaiDG1.MaLoaiTT=@MaLoaiTT and KhaoSat_LoaiDG1.NgayDG=@NgayDG) begin rollback transaction update KhaoSat_LoaiDG1 set Muc1=Muc1+1 where MaLoaiTT=@MaLoaiTT and NgayDG=@NgayDG end else insert into KhaoSat_LoaiDG1(MaLoaiTT,Muc1,NgayDG) values (@MaLoaiTT,1,@NgayDG) Update is ok but not insert.I do not know why, please help me.

            G 1 Reply Last reply
            0
            • N ngoloi

              ok, I show all code again after editing CREATE trigger In_LoaiDG1 on KhaoSat_LoaiDG1 for insert,update as declare @NgayDG smalldatetime declare @MaLoaiTT int Select @MaLoaiTT=MaLoaiTT,@NgayDG=NgayDG from inserted if exists (Select count(*) from KhaoSat_LoaiDG1 where KhaoSat_LoaiDG1.MaLoaiTT=@MaLoaiTT and KhaoSat_LoaiDG1.NgayDG=@NgayDG) begin rollback transaction update KhaoSat_LoaiDG1 set Muc1=Muc1+1 where MaLoaiTT=@MaLoaiTT and NgayDG=@NgayDG end else insert into KhaoSat_LoaiDG1(MaLoaiTT,Muc1,NgayDG) values (@MaLoaiTT,1,@NgayDG) Update is ok but not insert.I do not know why, please help me.

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

              It looks like you are performing an insert within your trigger on the table the trigger is running from

              CREATE trigger In_LoaiDG1 on KhaoSat_LoaiDG1

              insert into KhaoSat_LoaiDG1

              Any clues as to why this may be a bad idea? Hint - infinity... Just remove:

              else
              insert into KhaoSat_LoaiDG1(MaLoaiTT,Muc1,NgayDG) values (@MaLoaiTT,1,@NgayDG)

              and see what happens.

              “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