| Author |
Topic  |
|
|
axenos
Starting Member
2 Posts |
Posted - 02/28/2013 : 11:20:27
|
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
47173 Posts |
Posted - 02/28/2013 : 12:57:29
|
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/
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47173 Posts |
Posted - 02/28/2013 : 12:57:30
|
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/
|
 |
|
|
James K
Flowing Fount of Yak Knowledge
1527 Posts |
Posted - 02/28/2013 : 13:46:52
|
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. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47173 Posts |
Posted - 02/28/2013 : 23:16:36
|
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/
|
 |
|
|
axenos
Starting Member
2 Posts |
Posted - 03/01/2013 : 04:51:56
|
Thanks a lot guys (and also for the quick reply). It really helped.
Andreas |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47173 Posts |
Posted - 03/01/2013 : 05:05:11
|
you're welcome
------------------------------------------------------------------------------------------------------ SQL Server MVP http://visakhm.blogspot.com/
|
 |
|
| |
Topic  |
|