Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert Trigger

Status
Not open for further replies.

gaus

Programmer
Feb 5, 2001
92
US
Does anyone know how to create a BEFORE INSERT trigger? There is absolutely nothing about it in the Help (AFTER only). I cannot believe one does not exist - that would be nuts.
Thanks,
Gary
 
There is no BEFORE, but there is an INSTEAD trigger.

Hope this helps.
 
Hi,
Yes,Oracle has one too. But one of the nice things about a BEFORE insert trigger is the ability to modify data prior to inserting it. That is just one of the uses. This is what I am trying to do, replace the Null value passed on an INSERT with a calculated field from a lookup and a passed parm. Would not an INSTEAD of trigger simply 'overrule' the standard INSERT? Perhaps, I could figure out a way to use that, then do an INSERT within the trigger - I'll check it out. I'm new to SQL Server, but have already found things I prefer in Oracle, and others I prefer in SQL Server.
Thanks!
Gary
 
No problem. If you want to change the null value you can do it in the trigger. Example:

create table test
(idno int identity(1,1),
col1 varchar(25),
col2 varchar(25),
col3 varchar(25))

create trigger tr_test on test for Insert
as
update test
set col3 = 'inserted'
from test join inserted
on test.idno = inserted.idno
where inserted.col3 is null

insert test (col1)
values ('1st input')
insert test (col1,col3)
values ('1st input','test')

select * from test

Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top