Note that there are some explanatory texts on larger screens.

plurals
  1. POwhats happening in this trigger?
    text
    copied!<p>I was learning trigger in ms sql server and came across the following code on a website. I know that a trigger is made for insert, update and delete command but could not understand whats happening after that. The website also doesnt explain the script. Please explain me whats going on here.</p> <pre><code>select * from employee GO ID name salary start_date city region ----------- ---------- ----------- ----------------------- ---------- ------ 1 Jason 40420 1994-02-01 00:00:00.000 New York W 2 Robert 14420 1995-01-02 00:00:00.000 Vancouver N 3 Celia 24020 1996-12-03 00:00:00.000 Toronto W 4 Linda 40620 1997-11-04 00:00:00.000 New York N 5 David 80026 1998-10-05 00:00:00.000 Vancouver W 6 James 70060 1999-09-06 00:00:00.000 Toronto N 7 Alison 90620 2000-08-07 00:00:00.000 New York W 8 Chris 26020 2001-07-08 00:00:00.000 Vancouver N 9 Mary 60020 2002-06-09 00:00:00.000 Toronto W </code></pre> <p>(9 rows affected)</p> <pre><code>-- Creating and Using Triggers CREATE TRIGGER myTrigger ON employee FOR UPDATE, INSERT, DELETE AS SELECT * FROM inserted SELECT * FROM deleted GO SELECT 'Before INSERT' INSERT Employee (ID, Name) VALUES (31, 'Rick') GO </code></pre> <hr> <p>Before INSERT</p> <p>(1 rows affected)</p> <pre><code>ID name salary start_date city region ----------- ---------- ----------- ----------------------- ---------- ------ 31 Rick NULL NULL NULL NULL </code></pre> <p>(1 rows affected)</p> <pre><code>ID name salary start_date city region ----------- ---------- ----------- ----------------------- ---------- ------ </code></pre> <p>(0 rows affected)</p> <pre><code>SELECT 'Before UPDATE' UPDATE Employee SET Name = 'Rickie' WHERE ID = 3 GO </code></pre> <hr> <p>Before UPDATE</p> <p>(1 rows affected)</p> <pre><code>ID name salary start_date city region ----------- ---------- ----------- ----------------------- ---------- ------ 3 Rickie 24020 1996-12-03 00:00:00.000 Toronto W </code></pre> <p>(1 rows affected)</p> <pre><code>ID name salary start_date city region ----------- ---------- ----------- ----------------------- ---------- ------ 3 Celia 24020 1996-12-03 00:00:00.000 Toronto W </code></pre> <p>(1 rows affected)</p> <pre><code>SELECT 'Before DELETE' DELETE FROM Employee WHERE ID = 3 GO </code></pre> <hr> <p>Before DELETE</p> <p>(1 rows affected)</p> <pre><code>ID name salary start_date city region ----------- ---------- ----------- ----------------------- ---------- ------ </code></pre> <p>(0 rows affected)</p> <pre><code>ID name salary start_date city region ----------- ---------- ----------- ----------------------- ---------- ------ 3 Rickie 24020 1996-12-03 00:00:00.000 Toronto W </code></pre> <p>(1 rows affected)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload