SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2005
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    help needed with trigger

    hello please i need help with this trigger, i have a trigger that stops a record in a table from being duplicated. each time i insert a record, it fires and raises an error not minding if the condition in the trigger was true or false.

    pls find trigger below

    Code:
    alter trigger TRG_CHECK_PROGRAMME_COURSE_DUPLICATE
    On dbo.Institution_Programme_Courses
    for Insert
    as
    
    declare @ProgrammeId	int,
    	@CourseId	int,
    	@CourseTypeId	int,
    	@CourseUnit	int,	
    	@SemesterId	int,
    	@LevelId	int
    
    
    select	@ProgrammeId=ProgrammeId,
    	@CourseId=CourseId,
    	@CourseTypeId=CourseTypeId,
    	@CourseUnit=CourseUnit,	
    	@SemesterId=SemesterId,
    	@LevelId=LevelId
    from	Inserted
    
    
    if exists(	select	ProgrammeCourseId
    			from	dbo.Institution_Programme_Courses
    			where	ProgrammeId=@ProgrammeId
    			AND		CourseId=@CourseId
    			AND		CourseTypeId=@CourseTypeId
    			AND		CourseUnit=@CourseUnit	
    			AND		SemesterId=@SemesterId
    			AND		LevelId=@LevelId	
             )
    		begin
    			RAISERROR ('This Course SetUp Already Exist, Add New Operation Aborted', 16, 1)
    			rollback tran
    			return		
    		end
    from this trigger, i am trying to prevent the following record below from repeating twice. (i am avoiding duplicate records)

    @ProgrammeId,
    @CourseId,
    @CourseTypeId,
    @CourseUnit,
    @SemesterId,
    @LevelId


    please what am i doing wrong?

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,336
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by nellyihu View Post
    please what am i doing wrong?
    for starters, you're re-inventing the wheel, trying to do with a trigger something that the database can do for you automatically with a unique constraint

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2005
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, pls how do i go about it using constraints?

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,336
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    did you read the page i linked to?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2005
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yea.. still on it, thx


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •