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



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?

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 [B]unique constraint[/B]

:slight_smile:

ok, pls how do i go about it using constraints?

did you read the page i linked to?

Yea… still on it, thx