|
-
Oct 24th, 2024, 08:24 AM
#1
Trigger for DELETED?
Hi.
Mssql
Just making sure I understand this correctly.
I'm viewing a trigger created by someone else that reads:
FOR INSERT, UPDATE
And the I have this:
-- only update if the password has actually been changed
IF EXISTS(SELECT 1 FROM INSERTED i JOIN DELETED d ON i.person_id = d.person_id WHERE i.person_password <> ISNULL(d.person_password, ''))
So the question is the DELETED. Why the comment writes only update if changed? What it looks like is only update if a row is bound to be deleted. Mind you I have never used Deleted and searching does not reveal any article. IS DELETED actually a trigger command?
Thanks
Edit: I guess Deleted is the changing column? They could have picked a better name :P
Last edited by sapator; Oct 24th, 2024 at 08:32 AM.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Oct 24th, 2024, 08:34 AM
#2
Re: Trigger for DELETED?
In MSSQL INSERTED and DELETED is something like a TableName (for a "virtual" Table, which only has values during the operation)
In other DBMS they are called NEW and OLD
Look closely at the SQL:
SELECT 1 FROM INSERTED As i
JOIN DELETED As d
ON....
But i don't understand why even using a SELECT with a JOIN
A simple IF INSERTED.person_password<>COALESCE(DELETED.person_password, '') should be enough, provided person_password is NOT NULL (must have an Entry)
Since this is a "combined" Trigger for INSERT and UPDATE
On INSERT --> INSERTED.person_password IS NOT NULL and DELETED.person_password IS NULL --> My IF above returns True, since the COALESCE translates the NULL to empty string
On UPDATE --> INSERTED.person_password is the NEW value, DELETED.person_password is the OLD value, both NOT NULL --> My IF above returns only True, if the new password differs from the old one, since COALESCE returns DELETED.person_password since it's NOT NULL
Last edited by Zvoni; Oct 24th, 2024 at 08:43 AM.
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Oct 24th, 2024, 09:02 AM
#3
Re: Trigger for DELETED?
Thanks.
Actually this: IF INSERTED.person_password<>COALESCE(DELETED.person_password, '') should be enough, provided person_password is NOT NULL
Will not compile it says column cannot be found, I have to go like set @i = (SELECT i.person_password FROM INSERTED i)
Don't know if it works for other database types.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Oct 24th, 2024, 03:19 PM
#4
Re: Trigger for DELETED?
 Originally Posted by sapator
Thanks.
Actually this: IF INSERTED.person_password<>COALESCE(DELETED.person_password, '') should be enough, provided person_password is NOT NULL
Will not compile it says column cannot be found, I have to go like set @i = (SELECT i.person_password FROM INSERTED i)
Don't know if it works for other database types.
Ah, ok. Might be.
i‘m not that familiar with MSSQL
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|