SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 trigger after update
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

axenos
Starting Member

2 Posts

Posted - 02/28/2013 :  11:20:27  Show Profile  Reply with Quote
Hello all,

I am new to trigger in sqlserver and it would be very helpful to help convert this oracle sql trigger to a MS SQL one.

create or replace trigger "FIM_AGILR_AFTER_UPDATE"
after update on FIM_AGILE
for each row

declare
found boolean := false;
begin
for x in (SELECT internal_id FROM FIM_AGILE_CHANGES WHERE internal_id = :new.internal_id) loop
found := true;
exit;
end loop;
if not found then
insert into FIM_AGILE_CHANGES
values (:new.internal_id, 'Modify');
end if;
end;

Thank you in advance,
Andreas

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 02/28/2013 :  12:57:29  Show Profile  Reply with Quote
looks like you're using oracle

this is ms sql server forum

please try your luck at oracle forums like www.orafaq.com

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 02/28/2013 :  12:57:30  Show Profile  Reply with Quote
looks like you're using oracle

this is ms sql server forum

please try your luck at oracle forums like www.orafaq.com

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3706 Posts

Posted - 02/28/2013 :  13:46:52  Show Profile  Reply with Quote
Translation to T-SQL would be something like shown below. However, either I am not understanding the logic in the Oracle code (which is very likely), or the logic is not quite right. In any case, here is my attempt:
CREATE TRIGGER dbo.FIM_AGILR_AFTER_UPDATE
AFTER UPDATE ON FIM_AGILE
AS
	IF NOT EXISTS 
	( 
		SELECT * FROM FIM_AGILE_CHANGES f
		INNER JOIN INSERTED i ON i.internal_id = f.internal_id
	)
	INSERT INTO FIM_AGILE_CHANGES
	SELECT
		INSERTED.INTERNAL_ID,
		'Modify'
	FROM
		INSERTED;
GO
The concern I have relates to the fact that a single update can affect many rows, and some of the updated rows may be in FIM_AGILE_CHANGES and some may not be. In that case, the logic as I understood, and as I have translated it will not insert any rows into the FIM_AGILE_CHANGES table.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 02/28/2013 :  23:16:36  Show Profile  Reply with Quote
quote:
Originally posted by James K

Translation to T-SQL would be something like shown below. However, either I am not understanding the logic in the Oracle code (which is very likely), or the logic is not quite right. In any case, here is my attempt:
CREATE TRIGGER dbo.FIM_AGILR_AFTER_UPDATE
AFTER UPDATE ON FIM_AGILE
AS
	IF NOT EXISTS 
	( 
		SELECT * FROM FIM_AGILE_CHANGES f
		INNER JOIN INSERTED i ON i.internal_id = f.internal_id
	)
	INSERT INTO FIM_AGILE_CHANGES
	SELECT
		INSERTED.INTERNAL_ID,
		'Modify'
	FROM
		INSERTED;
GO
The concern I have relates to the fact that a single update can affect many rows, and some of the updated rows may be in FIM_AGILE_CHANGES and some may not be. In that case, the logic as I understood, and as I have translated it will not insert any rows into the FIM_AGILE_CHANGES table.


it should be


CREATE TRIGGER dbo.FIM_AGILR_AFTER_UPDATE
AFTER UPDATE ON FIM_AGILE
AFTER UPDATE
AS
	INSERT INTO FIM_AGILE_CHANGES
	SELECT
		INSERTED.INTERNAL_ID,
		'Modify'
	FROM
		INSERTED i
       WHERE NOT EXISTS (SELECT 1 FROM FIM_AGILE_CHANGES 
                         WHERE  internal_id = i.internal_id)
GO


This will ensure only rows not already there in CHANGES table will get populated

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

axenos
Starting Member

2 Posts

Posted - 03/01/2013 :  04:51:56  Show Profile  Reply with Quote
Thanks a lot guys (and also for the quick reply).
It really helped.

Andreas
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 03/01/2013 :  05:05:11  Show Profile  Reply with Quote
you're welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000