Hi,
Does anybody have an example of sql server trigger for an update?
Gil
Printable View
Hi,
Does anybody have an example of sql server trigger for an update?
Gil
Hi Gil
This is something I threw together, it checks to see if a value already exists, and raises an error if it does.
Code: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
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