When we insert or update data in a table, at time I want the primary key and table name to be inserted in another table.
I need this to reconcile at the end of day, all tables that are updated today.
当我们在表中插入或更新数据时,我希望主键和表名插入另一个表中。我需要在一天结束时协调所有今天更新的表格。
Please suggest. Thank you.
请建议。谢谢。
3 个解决方案
#1
3
Take a look at CDC - Change Data Capture
看看CDC - Change Data Capture
Or Paul Nielsen's AutoAudit.
或保罗尼尔森的AutoAudit。
#2
0
SELECT name
FROM sys.objects
WHERE DATEDIFF(D,modify_date, GETDATE()) < 1
By using this query you can able to get all the modification of your database. In place of 1 you can able to give no of days you want to see the change.
通过使用此查询,您可以获得数据库的所有修改。代替1,你可以不给你想看到变化的日子。
#3
0
I've worked out something like this...I hope it answers your question.
我已经找到了这样的东西......我希望它能回答你的问题。
- Create a few test tables for demonstration
- Create the SQL to create triggers on all the tables
- Simple insert statements.
创建一些测试表以进行演示
创建SQL以在所有表上创建触发器
简单的插入语句。
CREATE TABLE [dbo].[log_table]( [table_name] [varchar](1000) NULL, [column_name] [varchar](1000) NULL, [column_value] [varchar](1000) NULL, [datetime] [datetime] NULL ) GO
CREATE TABLE [dbo].[Animals](
[AnimalID] [int] IDENTITY(1,1) NOT NULL,
[AnimalName] [varchar](50) NULL,
CONSTRAINT [PK_Animals] PRIMARY KEY CLUSTERED
([AnimalID] ASC)
)
GO
CREATE TABLE [dbo].[Countries](
[CountryID] [uniqueidentifier] NOT NULL,
[CountryName] [varchar](50) NULL,
CONSTRAINT [PK_Countries] PRIMARY KEY CLUSTERED
([CountryID] ASC))
ALTER TABLE [dbo].[Countries] ADD CONSTRAINT [DF_Countries_countryID] DEFAULT (newid()) FOR [CountryID]
GO
SELECT ' IF OBJECT_ID ('+ CHAR(39) + SCHEMA_NAME(tbl.SCHEMA_ID) + '.trg_' + tbl.name + CHAR(39) + ',''TR'') IS NOT NULL ' + ' DROP TRIGGER ' + SCHEMA_NAME(tbl.SCHEMA_ID) + '.trg_'+ tbl.name + ';' + ' EXEC (' + CHAR(39) + ' CREATE TRIGGER ' + SCHEMA_NAME(tbl.SCHEMA_ID) + '.trg_'+ tbl.name + ' ON '+ SCHEMA_NAME(tbl.SCHEMA_ID) + '.' + tbl.name + ' FOR INSERT,UPDATE AS ' + ' DECLARE @newval varchar(1000) ' + ' SELECT @newval ='+ COL_NAME(tbl.object_id, ic.column_id) + ' FROM INSERTED' + ' INSERT INTO log_table(table_name,column_name,column_value,datetime) VALUES('+ CHAR(39)+ CHAR(39) + SCHEMA_NAME(tbl.SCHEMA_ID) +'.'+ tbl.name + CHAR(39)+ CHAR(39) + ' ,'+ CHAR(39)+ CHAR(39) + COL_NAME(tbl.object_id, ic.column_id) + CHAR(39)+ CHAR(39) + ' ,'+ '@newval,getdate()'+ ')' + CHAR(39)+ ')' FROM sys.tables AS tbl INNER JOIN sys.key_constraints AS kc ON kc.parent_object_id = tbl.object_id INNER JOIN sys.indexes AS i ON kc.unique_index_id = i.index_id AND kc.parent_object_id = i.object_id INNER JOIN sys.index_columns AS ic ON i.object_id = ic.object_id AND i.index_id = ic.index_id WHERE tbl.type = 'U' AND ic.is_included_column = 0 and tbl.name NOT IN ('sysdiagrams','log_table') ORDER BY tbl.object_id, ic.key_ordinal;
Now insert into our tables
现在插入我们的表格
INSERT INTO Countries(CountryName) VALUES('South Africa') INSERT INTO Countries(CountryName) VALUES('USA') INSERT INTO Countries(CountryName) VALUES('Brazil') INSERT INTO Countries(CountryName) VALUES('Zimbabwe') INSERT INTO Countries(CountryName) VALUES('Africa is not a country') INSERT INTO Animals(AnimalName) VALUES ('Lion') INSERT INTO Animals(AnimalName) VALUES ('African Elephant') INSERT INTO Animals(AnimalName) VALUES ('Rhinoceros') INSERT INTO Animals(AnimalName) VALUES ('Leopard') INSERT INTO Animals(AnimalName) VALUES ('Cape Buffalo')
#1
3
Take a look at CDC - Change Data Capture
看看CDC - Change Data Capture
Or Paul Nielsen's AutoAudit.
或保罗尼尔森的AutoAudit。
#2
0
SELECT name
FROM sys.objects
WHERE DATEDIFF(D,modify_date, GETDATE()) < 1
By using this query you can able to get all the modification of your database. In place of 1 you can able to give no of days you want to see the change.
通过使用此查询,您可以获得数据库的所有修改。代替1,你可以不给你想看到变化的日子。
#3
0
I've worked out something like this...I hope it answers your question.
我已经找到了这样的东西......我希望它能回答你的问题。
- Create a few test tables for demonstration
- Create the SQL to create triggers on all the tables
- Simple insert statements.
创建一些测试表以进行演示
创建SQL以在所有表上创建触发器
简单的插入语句。
CREATE TABLE [dbo].[log_table]( [table_name] [varchar](1000) NULL, [column_name] [varchar](1000) NULL, [column_value] [varchar](1000) NULL, [datetime] [datetime] NULL ) GO
CREATE TABLE [dbo].[Animals](
[AnimalID] [int] IDENTITY(1,1) NOT NULL,
[AnimalName] [varchar](50) NULL,
CONSTRAINT [PK_Animals] PRIMARY KEY CLUSTERED
([AnimalID] ASC)
)
GO
CREATE TABLE [dbo].[Countries](
[CountryID] [uniqueidentifier] NOT NULL,
[CountryName] [varchar](50) NULL,
CONSTRAINT [PK_Countries] PRIMARY KEY CLUSTERED
([CountryID] ASC))
ALTER TABLE [dbo].[Countries] ADD CONSTRAINT [DF_Countries_countryID] DEFAULT (newid()) FOR [CountryID]
GO
SELECT ' IF OBJECT_ID ('+ CHAR(39) + SCHEMA_NAME(tbl.SCHEMA_ID) + '.trg_' + tbl.name + CHAR(39) + ',''TR'') IS NOT NULL ' + ' DROP TRIGGER ' + SCHEMA_NAME(tbl.SCHEMA_ID) + '.trg_'+ tbl.name + ';' + ' EXEC (' + CHAR(39) + ' CREATE TRIGGER ' + SCHEMA_NAME(tbl.SCHEMA_ID) + '.trg_'+ tbl.name + ' ON '+ SCHEMA_NAME(tbl.SCHEMA_ID) + '.' + tbl.name + ' FOR INSERT,UPDATE AS ' + ' DECLARE @newval varchar(1000) ' + ' SELECT @newval ='+ COL_NAME(tbl.object_id, ic.column_id) + ' FROM INSERTED' + ' INSERT INTO log_table(table_name,column_name,column_value,datetime) VALUES('+ CHAR(39)+ CHAR(39) + SCHEMA_NAME(tbl.SCHEMA_ID) +'.'+ tbl.name + CHAR(39)+ CHAR(39) + ' ,'+ CHAR(39)+ CHAR(39) + COL_NAME(tbl.object_id, ic.column_id) + CHAR(39)+ CHAR(39) + ' ,'+ '@newval,getdate()'+ ')' + CHAR(39)+ ')' FROM sys.tables AS tbl INNER JOIN sys.key_constraints AS kc ON kc.parent_object_id = tbl.object_id INNER JOIN sys.indexes AS i ON kc.unique_index_id = i.index_id AND kc.parent_object_id = i.object_id INNER JOIN sys.index_columns AS ic ON i.object_id = ic.object_id AND i.index_id = ic.index_id WHERE tbl.type = 'U' AND ic.is_included_column = 0 and tbl.name NOT IN ('sysdiagrams','log_table') ORDER BY tbl.object_id, ic.key_ordinal;
Now insert into our tables
现在插入我们的表格
INSERT INTO Countries(CountryName) VALUES('South Africa') INSERT INTO Countries(CountryName) VALUES('USA') INSERT INTO Countries(CountryName) VALUES('Brazil') INSERT INTO Countries(CountryName) VALUES('Zimbabwe') INSERT INTO Countries(CountryName) VALUES('Africa is not a country') INSERT INTO Animals(AnimalName) VALUES ('Lion') INSERT INTO Animals(AnimalName) VALUES ('African Elephant') INSERT INTO Animals(AnimalName) VALUES ('Rhinoceros') INSERT INTO Animals(AnimalName) VALUES ('Leopard') INSERT INTO Animals(AnimalName) VALUES ('Cape Buffalo')