Keywords: SQL Server | Triggers | INSERTED Table
Abstract: This article explores how to use the INSERTED logical table in SQL Server triggers to extract data from newly inserted rows and insert it into another table. Through a case study of the asp.net membership schema's aspnet_users table, it details trigger creation, the workings of the INSERTED table, code implementation, and best practices, comparing alternatives like using last date_created. With code examples, it aids developers in efficiently handling data synchronization tasks.
Introduction
In database development, triggers are powerful tools for automatically executing specific logic upon data operations such as insert, update, or delete. This article is based on a practical case: setting up a trigger on the aspnet_users table in the asp.net membership schema to insert user_id and user_name values from newly inserted rows into another table. The user initially considered selecting data via last date_created, but this approach has performance and consistency issues, prompting a search for better solutions.
Core Concept: The INSERTED Logical Table
SQL Server triggers access affected data rows through logical tables INSERTED and DELETED. For INSERT operations, the INSERTED table contains all newly inserted rows, with the same column structure as the original table. Similarly, DELETE triggers use the DELETED table to access deleted rows, while UPDATE triggers can access both INSERTED (updated values) and DELETED (pre-update values) tables. This avoids relying on "smelly" methods like timestamps or maximum IDs, ensuring accurate and efficient data extraction.
Implementation Steps and Code Example
The following code demonstrates creating an INSERT trigger on yourSourcetable to insert data into yourDestinationTable:
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
goCode breakdown:
CREATE TRIGGER yourNewTrigger ON yourSourcetable: Creates a trigger namedyourNewTriggeron the source table.FOR INSERT: Specifies that the trigger fires after insert operations.INSERT INTO yourDestinationTable ... SELECT ... FROM inserted: Selectsuser_idanduser_namecolumns from theINSERTEDlogical table and inserts them into corresponding columns in the destination table, while populating other columns likecol1with a constant'a'.
In the asp.net membership case, replace yourSourcetable with aspnet_users, yourDestinationTable with the target table name, and adjust column mappings accordingly. This method is direct and efficient, requiring no additional queries.
Comparative Analysis and Best Practices
The user's initial approach using last date_created is flawed: in high-concurrency environments, it may select incorrect rows or cause race conditions. Using the INSERTED table ensures atomicity and consistency, as the trigger executes within the same transaction, accessing the exact inserted data.
Other answers supplement the use of the DELETED table in delete and update triggers, but this article focuses on insert scenarios. Best practices include:
- Keeping trigger logic simple to avoid performance impacts.
- Defining appropriate indexes and constraints in the destination table to maintain data integrity.
- Testing trigger behavior with multi-row inserts, as the
INSERTEDtable may contain multiple rows.
Conclusion
By leveraging SQL Server's INSERTED logical table, developers can efficiently and reliably extract data from newly inserted rows to other tables. The code examples and principle analysis in this article provide a practical guide for handling data synchronization needs like those in asp.net membership, highlighting the critical role of triggers in automating data flows.