Click to See Complete Forum and Search --> : Sql Server trigger for update
Gil Ben-Yosef
Jan 21st, 2000, 04:10 PM
Hi,
Does anybody have an example of sql server trigger for an update?
Gil
Clunietp
Jan 22nd, 2000, 11:11 AM
Hi Gil
This is something I threw together, it checks to see if a value already exists, and raises an error if it does.
CREATE TRIGGER tr_test ON [Names]
FOR INSERT
AS
-- Declare variables
Declare @NewName varchar(50)
Declare @MyCount int
-- Get Name of inserted value
Select @NewName = inserted.[name] from inserted
-- Get Count
Select @MyCount = count(*) from Names where [Name] = @NewName
-- Check to see if it already exists
if @MyCount > 1
BEGIN
-- roll back change, raise error
ROLLBACK
RAISERROR ('NAME ALREADY EXISTS!', 16, 1)
END
Gil Ben-Yosef
Jan 22nd, 2000, 05:04 PM
Thanks,
It wasn't exactly what I need but it gave me a starting point.
I am adding an example of update trigger for everybody.
Code:
-------------------------------------------
CREATE TRIGGER IntUpdate ON appIntD
FOR update
AS
declare @newkod int
declare @oldkod int
select @oldkod = deleted.kod from deleted
select @newkod = inserted.kod from inserted
if update(kod)
begin
update appIntLinD
set intr=@newkod
where intr= @oldkod
end
-------------------------------------------
gil
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.