Hello
Im am a sql newbie who has a question concerning triggers.
Ive made this trigger:
create trigger check_if_already_exists on
users
for insert ,update
as
begin
begin transaction
Declare @Username varchar(20)
Declare @Username_exists varchar(20)
select @Username = user_id
from inserted
set @Register = cursor scroll dynamic
for select user_id,
from users
order by user_id
open @Register
fetch next from @Register into @Username_exists
while @@fetch_status = 0
begin
if(@Username = @Username_exists)
begin
rollback transaction
print'Transaction rollback'
end
fetch next from @Register into @Username_exists
end
close @Register
deallocate @Register
commit transaction
end
which takes a variable from inserted and checks it with the existing one
in the database through a cursor.
My questions are
1)
to start and end the trigger should i use:
begin transaction ---- end transaction | commit transaction
or
begin ----- end
2)
Im using a cursor here , is this a good use of cursors
Mike