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. SQL Triggers (Before) [modified]

SQL Triggers (Before) [modified]

Scheduled Pinned Locked Moved Database
database
5 Posts 2 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.
  • A Offline
    A Offline
    Amit Kumar G
    wrote on last edited by
    #1

    I want to restrict user for not adding the new record if table already contains active record. (means Active=0) CREATE TRIGGER TR_TB_QuizMaster ON TB_QuizMaster FOR INSERT AS DECLARE @iCount int BEGIN SELECT @iCount=COUNT(*) FROM TB_QuizMaster WHERE Active=0 Print @iCount IF (@iCount>=1) BEGIN Print 'Active record already exists. Only one active record can exisits' Rollback END ELSE print 'Inserted' END But this does not allow me to enter a new record unless i change it to IF (@iCount>=2). This is because it fires after insert.....Is there any way by which we can fire trigger BEFORE actual DML operation.... Please suggest Thanks!! -- modified at 6:49 Friday 21st September, 2007

    P 1 Reply Last reply
    0
    • A Amit Kumar G

      I want to restrict user for not adding the new record if table already contains active record. (means Active=0) CREATE TRIGGER TR_TB_QuizMaster ON TB_QuizMaster FOR INSERT AS DECLARE @iCount int BEGIN SELECT @iCount=COUNT(*) FROM TB_QuizMaster WHERE Active=0 Print @iCount IF (@iCount>=1) BEGIN Print 'Active record already exists. Only one active record can exisits' Rollback END ELSE print 'Inserted' END But this does not allow me to enter a new record unless i change it to IF (@iCount>=2). This is because it fires after insert.....Is there any way by which we can fire trigger BEFORE actual DML operation.... Please suggest Thanks!! -- modified at 6:49 Friday 21st September, 2007

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      There is - it's called an instead of trigger and works by defining a trigger on a view which matches the table structure. What happens is you insert into the view which runs the trigger and then depending on your result, inserts into the appropriate table. From memory, it would look something like this:

      create trigger MyTableInsertCheck
      on MyView
      instead of insert
      as
      begin
      select @count = count(1) from mytable m inner join inserted i on m.keyfield = i.keyfield
      if @count = 0
      begin
        insert name, keyfield into mytable
        select name, keyfield from myview
      end
      end
      

      Deja View - the feeling that you've seen this post before.

      A 1 Reply Last reply
      0
      • P Pete OHanlon

        There is - it's called an instead of trigger and works by defining a trigger on a view which matches the table structure. What happens is you insert into the view which runs the trigger and then depending on your result, inserts into the appropriate table. From memory, it would look something like this:

        create trigger MyTableInsertCheck
        on MyView
        instead of insert
        as
        begin
        select @count = count(1) from mytable m inner join inserted i on m.keyfield = i.keyfield
        if @count = 0
        begin
          insert name, keyfield into mytable
          select name, keyfield from myview
        end
        end
        

        Deja View - the feeling that you've seen this post before.

        A Offline
        A Offline
        Amit Kumar G
        wrote on last edited by
        #3

        Thanks and if i don't want to create views....Is there any Before trigger in SQL 2005.....

        P 1 Reply Last reply
        0
        • A Amit Kumar G

          Thanks and if i don't want to create views....Is there any Before trigger in SQL 2005.....

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          Amit Kumar G wrote:

          Is there any Before trigger in SQL 2005.....

          No (except as in the example above).

          Deja View - the feeling that you've seen this post before.

          A 1 Reply Last reply
          0
          • P Pete OHanlon

            Amit Kumar G wrote:

            Is there any Before trigger in SQL 2005.....

            No (except as in the example above).

            Deja View - the feeling that you've seen this post before.

            A Offline
            A Offline
            Amit Kumar G
            wrote on last edited by
            #5

            thanks!! I just read trigger syntax and would really appreciate if you could help me to understand difference between FOR|After clause....

            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