Log Entries that are hitting a table along with their IP and host name


CREATE TABLE dbo.LoggingTable
(
    LoggingID INT NOT NULL
        CONSTRAINT PK_LoggingTable
        PRIMARY KEY CLUSTERED
        IDENTITY(1,1)
    , AppName SYSNAME
    , HostName SYSNAME
    , ClientNetAddress VARCHAR(255)
    , LogDateTime DATETIME
);
GO

CREATE TRIGGER dbo.triggername
ON dbo.address_type /* name of the target table  */
FOR INSERT, UPDATE, DELETE
AS
BEGIN
  INSERT dbo.LoggingTable(AppName, HostName, ClientNetAddress, LogDateTime)
  SELECT app_name(), host_name(), client_net_address, SYSDATETIME()
  FROM sys.dm_exec_connections dec
  WHERE dec.session_id = @@SPID;
END
GO


Comments

Popular posts from this blog

Grouping Sets, With Rollup and With Cube example

Adding Page brake aganist group values

To Get All the Names of tables and Databases their data have changed in last 24 Hours And Backup those databases