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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SQL Server Trigger

SQL Server Trigger

Scheduled Pinned Locked Moved Database
databasesql-serversysadminhelpannouncement
5 Posts 4 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.
  • I Offline
    I Offline
    irusul
    wrote on last edited by
    #1

    I need help on creating a trigger for insert and update. My table looks like this: tblCandidates canID smallint PK identity ID int FK eleID smallint FK What I need to do is: During insert or update, if there are two records with the same ID and eleID, operation should be aborted and to return some string as output (ex: "operation impossible") Thank you

    G M T 3 Replies Last reply
    0
    • I irusul

      I need help on creating a trigger for insert and update. My table looks like this: tblCandidates canID smallint PK identity ID int FK eleID smallint FK What I need to do is: During insert or update, if there are two records with the same ID and eleID, operation should be aborted and to return some string as output (ex: "operation impossible") Thank you

      G Offline
      G Offline
      Giorgi Dalakishvili
      wrote on last edited by
      #2

      Why not include eleID in primary key?

      Giorgi Dalakishvili #region signature my articles #endregion

      1 Reply Last reply
      0
      • I irusul

        I need help on creating a trigger for insert and update. My table looks like this: tblCandidates canID smallint PK identity ID int FK eleID smallint FK What I need to do is: During insert or update, if there are two records with the same ID and eleID, operation should be aborted and to return some string as output (ex: "operation impossible") Thank you

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

        I don't think a trigger works like that. As I never use triggers this is conjecture Insert/Update causes trigger to execute, the trigger is a seperate stored procedure and cannot affect the outcome of whatever causes the insert/update. However: You really should put this check in the initial method, otherwise you are programming by error. IE try something if there is an error then it is wrong ahh do something else.

        Never underestimate the power of human stupidity RAH

        1 Reply Last reply
        0
        • I irusul

          I need help on creating a trigger for insert and update. My table looks like this: tblCandidates canID smallint PK identity ID int FK eleID smallint FK What I need to do is: During insert or update, if there are two records with the same ID and eleID, operation should be aborted and to return some string as output (ex: "operation impossible") Thank you

          T Offline
          T Offline
          TheFM234
          wrote on last edited by
          #4

          Easiest way to do this is by creating an unique non-clustered index (it could be clustered, but since there is a PK on the table a clustered index should have been created by default as the PK columns): Create unique nonclustered index IX_Id_eleID on ( Id ,eleId ) When duplicate rows are inserted, it will give the following error: Msg 2601, Level 14, State 1, Line 1 Cannot insert duplicate key row in object 'dbo.cTest' with unique index 'IX_Id_eleID'. Just put the name of the table in place of , and name the index how you want.

          M 1 Reply Last reply
          0
          • T TheFM234

            Easiest way to do this is by creating an unique non-clustered index (it could be clustered, but since there is a PK on the table a clustered index should have been created by default as the PK columns): Create unique nonclustered index IX_Id_eleID on ( Id ,eleId ) When duplicate rows are inserted, it will give the following error: Msg 2601, Level 14, State 1, Line 1 Cannot insert duplicate key row in object 'dbo.cTest' with unique index 'IX_Id_eleID'. Just put the name of the table in place of , and name the index how you want.

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

            Scott While this works and is probably the correct solution it is still "programming by error" and you have an additional constraint on the table to support sloppy development. I would do the check BEFORE attempting to insert/update the record, this may entail an additional index so it may nullify the constraint argument.

            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