Getting Values from Last Insert for Trigger Insertion
When creating a trigger in SQL Server to insert values from a new row into another table, it's crucial to efficiently obtain the values from the last insert. While selecting by the latest date_created may seem like a workaround, it lacks precision and introduces potential issues.
Solution:
To accurately capture the values from the last insert and populate the destination table, consider using the following trigger syntax in SQL Server:
CREATE TRIGGER yourNewTrigger ON yourSourcetable FOR INSERT AS INSERT INTO yourDestinationTable (col1, col2, col3, user_id, user_name) SELECT 'a', default, null, user_id, user_name FROM inserted
inserted Table:
Sample Values:
By leveraging the inserted table, this trigger ensures that the values from the latest insert are seamlessly captured and inserted into the specified destination table.
The above is the detailed content of How to Efficiently Get Values from the Last Insert in a SQL Server Trigger?. For more information, please follow other related articles on the PHP Chinese website!