2

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.

Orangecrush
  • 1,970
  • 2
  • 15
  • 26

3 Answers3

3

Take a look at CDC - Change Data Capture

Or Paul Nielsen's AutoAudit.

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
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.

Aleksandr Fedorenko
  • 16,594
  • 6
  • 37
  • 44
minakshi
  • 315
  • 1
  • 3
  • 17
0

I've worked out something like this...I hope it answers your question.

  1. Create a few test tables for demonstration
  2. Create the SQL to create triggers on all the tables
  3. Simple insert statements.
   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')
Thato
  • 128
  • 1
  • 7
  • As far as I am concerned, this question is clear enough. Manoj needs to keep track of tables that are updated on a daily basis, and also be able to see the value of the primary key for the inserted/updated record. That's how I understood it. – Thato Feb 06 '13 at 08:07