Click here to Skip to main content
15,911,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have create a trigger for update table, also it works properly..
.
now i want same conditions, for insert trigger for same table,
.
is there any way that i should not write same code, and not create separate trigger for insert


i have
Alter TRIGGER [dbo].[TgrLog] ON [dbo].[tblLotAssign]
FOR UPDATE
AS
insert into tblLotAssignLog values(@LotAssignNo,@SerialNo,@LotName,@tlaid,@LotID,@status,@assigndt,@ToppingDate,@RackingDate,getdate())

where i got parameters by virtual tabel(inserted i) and some by fetching data from // another tables..
it works, as i want..
but i want same insert query for insert trigger as

 Alter TRIGGER [dbo].[TgrLog] ON [dbo].[tblLotAssign]
FOR Insert
AS
insert into tblLotAssignLog values(@LotAssignNo,@SerialNo,@LotName,@tlaid,@LotID,@status,@assigndt,@ToppingDate,@RackingDate,getdate())



without creating another trigger
Posted

Hi,
Hope this will help you.
SQL
CREATE TRIGGER [dbo].[TgrLog] ON [dbo].[tblLotAssign]
FOR INSERT, UPDATE
AS
BEGIN

Check this links for details :
http://stackoverflow.com/questions/741414/insert-update-trigger-how-to-determine-if-insert-or-update[^]


http://stackoverflow.com/questions/6620042/how-do-i-create-insert-update-trigger-on-sql-server-2008[^]
 
Share this answer
 
Comments
Manas Bhardwaj 5-Nov-14 3:38am    
ditto +5!
Madhuri Gamane 5-Nov-14 3:38am    
Thank you very much, it works... :-))
You could do something like this:

SQL
CREATE TRIGGER [dbo].[TgrLog] ON [dbo].[tblLotAssign]
FOR UPDATE, INSERT
AS
    INSERT INTO
        tblLotAssignLog
    VALUES(@LotAssignNo,@SerialNo,@LotName,@tlaid,@LotID,@status,@assigndt,@ToppingDate,@RackingDate,getdate())
 
Share this answer
 
SQL
CREATE TRIGGER [dbo].[TgrLog] ON [dbo].[tblLotAssign]
FOR UPDATE, INSERT
AS
INSERT INTO
        tblLotAssignLog
select * from inserted


How would you get

@LotAssignNo,@SerialNo,@LotName,@tlaid,@LotID,@status,@assigndt,@ToppingDate,@RackingDat parameters in trigger.

You could use below to identify to get the values

SELECT * FROM INSERTED
SELECT * FROM DELETED


If record exist in both Inserted and deleted table then its a update, and if only exists in inserted then Insert and Only in deleted then its a Delete.

Based on that you can use a flag identifier also to check that what was the action performed on you table.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900