Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Probably the best solution for your situation is to use a trigger, as <a href="http://msdn.microsoft.com/en-us/library/ms189799.aspx" rel="nofollow">MSDN</a> defines: "<em>A trigger is a special kind of stored procedure that automatically executes when an event occurs in the database server.</em>".</p> <p>The following script creates two tables on tempdb: <code>mydatanow</code> and <code>mydatalog</code>. The trigger <code>makelog</code> runs every time a row is inserted or updated into <code>mydatanow</code> and its implementations copies all rows inserted into <code>mydatanow</code> to <code>mydatalog</code> -- if more than one row is affected by an update statement, all rows are copied.</p> <pre><code>USE tempdb; GO CREATE TABLE mydatanow (id SMALLINT, c1 VARCHAR(10), c2 VARCHAR(10)); CREATE TABLE mydatalog (id SMALLINT, c1 VARCHAR(10), c2 VARCHAR(10), moment DATETIME); GO CREATE TRIGGER makelog ON mydatanow AFTER INSERT, UPDATE AS BEGIN INSERT mydatalog(id, c1, c2, moment) SELECT id, c1, c2, GETDATE() FROM inserted END GO INSERT mydatanow VALUES (1, 'abe', 'apple'); INSERT mydatanow VALUES (2, 'beth', 'banana'); UPDATE mydatanow SET c1 = 'carl', c2 = 'no fruit'; GO SELECT * FROM mydatanow; -- id c1 c2 -- ------ ---------- ---------- -- 1 carl no fruit -- 2 carl no fruit SELECT * FROM mydatalog; -- id c1 c2 moment -- ------ ---------- ---------- ----------------------- -- 1 abe apple 2012-05-10 19:36:45.843 -- 2 beth banana 2012-05-10 19:36:45.843 -- 1 carl no fruit 2012-05-10 19:36:45.847 -- 2 carl no fruit 2012-05-10 19:36:45.847 </code></pre>
 

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