日期:2014-05-17 浏览次数:20629 次
USE test
GO
IF object_id('tempdb..#')IS NOT NULL
DROP TABLE #
SELECT 1 AS id,CAST('a' AS NVARCHAR(50)) AS val INTO #
IF object_id('temp_#_record')IS NOT NULL
DROP TABLE temp_#_record
Create table temp_#_record (Table_Name sysname,Deleted_ID INT,Deleted_val NVARCHAR(50),Inserted_ID INT,Inserted_val NVARCHAR(50),modifydate DATETIME)
GO
INSERT INTO #(ID,val)
OUTPUT N'#',INSERTED.ID,INSERTED.val,getdate()
INTO temp_#_record(Table_Name,Inserted_ID,Inserted_val,modifydate)
SELECT 2,'b'
DELETE #
OUTPUT N'#',DELETED.ID,DELETED.val,getdate()
INTO temp_#_record(Table_Name,Deleted_ID,Deleted_val,modifydate)
FROM # WHERE id=1
UPDATE #
SET val='bb'
OUTPUT N'#',DELETED.ID,DELETED.val,INSERTED.ID,INSERTED.val,GETDATE()
INTO temp_#_record(Table_Name,Deleted_ID,Deleted_val,Inserted_ID,Inserted_val,modifydate)
WHERE id=2
SELECT * FROM #
/*
id val
--- ----
2 bb
*/
SELECT * FROM temp_#_record
/*
Table_Name Deleted_ID Deleted_val Inserted_ID Inserted_val modifydate
----------- ----------- ------------ ----------- ------------- -----------------------
# NULL NULL 2 b 2013-01-25 10:13:19.003
# 1 a NULL NULL 2013-01-25 10:13:19.003
# 2 b 2 bb 2013-01-25 10:13:19.003
*/