運行這些腳本
腳本1
-- =============================================
-- Author: Carlos Aguilar
-- Create date: April 2016
-- Description: Massive Creation of Log Tables in a Database
-- =============================================
DECLARE @SCRIPT NVARCHAR(MAX)
DECLARE @TABLE NVARCHAR(MAX)
DECLARE @COLUMNS NVARCHAR(MAX)
DECLARE _LOGS_ CURSOR FOR
SELECT NAME FROM SYSOBJECTS WHERE TYPE = 'U' AND NAME NOT LIKE '__LOG_%'
OPEN _LOGS_
BEGIN TRY
BEGIN TRAN
FETCH NEXT FROM _LOGS_ INTO @TABLE
WHILE (@@FETCH_STATUS = 0)
BEGIN
SET @COLUMNS = ''
SELECT @COLUMNS = @COLUMNS + '[' + T1.NAME + '] ' + T2.NAME +
CASE
WHEN T1.XTYPE IN (106, 108) THEN
'(' + CONVERT(VARCHAR, T1.XPREC) + ', ' + CONVERT(VARCHAR, T1.XSCALE) + ')'
WHEN T1.XTYPE IN (173, 175, 42, 43, 239, 231, 41, 165, 167) THEN
'(' + CASE WHEN T1.LENGTH = -1 THEN 'MAX' ELSE CONVERT(VARCHAR, T1.LENGTH) END + ')'
ELSE
''
END
+ ' NULL, '
FROM SYSOBJECTS T0
INNER JOIN SYSCOLUMNS T1
ON T0.ID = T1.ID
INNER JOIN sys.TYPES T2
ON T1.XTYPE = T2.SYSTEM_TYPE_ID
WHERE T0.TYPE = 'U' AND
T0.NAME NOT LIKE '__LOG_%' AND
T0.NAME = @TABLE AND
T1.XTYPE NOT IN (34, 35, 99) AND
T2.NAME <> 'sysname'
SELECT @SCRIPT =
'CREATE TABLE __LOG_' + @TABLE + '(' + @COLUMNS + '
[__DB_USER] nvarchar(128) NULL,
[__APP_NAME] nvarchar(128) NULL,
[__HOST_NAME] nvarchar(128) NULL,
[__IP_ADD] sql_variant NULL,
[__EVENT] varchar(1) NULL,
[__TIME] datetime NULL,
[__PROTOCOL] sql_variant NULL) '
PRINT @SCRIPT
EXEC (@SCRIPT)
SELECT @SCRIPT = 'DELETE FROM __LOG_' + @TABLE
PRINT @SCRIPT
EXEC (@SCRIPT)
FETCH NEXT FROM _LOGS_ INTO @TABLE
END
COMMIT
END TRY
BEGIN CATCH
ROLLBACK
PRINT 'ERROR: ' + ERROR_MESSAGE()
END CATCH
CLOSE _LOGS_
DEALLOCATE _LOGS_
GO
腳本2
-- =============================================
-- Author: Carlos Aguilar
-- Create date: April 2016
-- Description: Massive Creation of Trigger for Log Tables in a Database
-- =============================================
DECLARE @COLUMNS NVARCHAR(MAX)
DECLARE @SCRIPT NVARCHAR(MAX)
DECLARE @TABLE NVARCHAR(MAX)
DECLARE _LOGS_ CURSOR FOR
SELECT NAME FROM SYSOBJECTS WHERE TYPE = 'U' AND NAME NOT LIKE '__LOG_%'
OPEN _LOGS_
BEGIN TRY
BEGIN TRAN
FETCH NEXT FROM _LOGS_ INTO @TABLE
WHILE (@@FETCH_STATUS = 0)
BEGIN
SET @COLUMNS = ''
SELECT @COLUMNS = @COLUMNS + '[' + T1.NAME + '], '
FROM SYSOBJECTS T0
INNER JOIN SYSCOLUMNS T1
ON T0.ID = T1.ID
WHERE T0.TYPE = 'U' AND
T0.NAME NOT LIKE '__LOG_%' AND
T0.NAME = @TABLE AND
T1.XTYPE NOT IN (34, 35, 99)
SELECT @SCRIPT = 'CREATE TRIGGER [dbo].[__LOG_INSERT_' + @TABLE + '] ON [dbo].[' + @TABLE + ']
WITH ENCRYPTION, EXECUTE AS CALLER
FOR INSERT
AS
BEGIN
INSERT INTO __LOG_' + @TABLE + '(' +
@COLUMNS + '
__DB_USER,
__APP_NAME,
__HOST_NAME,
__IP_ADD,
__EVENT,
__TIME,
__PROTOCOL)
SELECT ' + @COLUMNS + '
SUSER_SNAME() AS __DB_USER ,
APP_NAME () AS __APP_NAME,
HOST_NAME() AS __HOST_NAME,
CONNECTIONPROPERTY(''client_net_address'') __IP_ADD,
''I'' AS __EVENT,
GETDATE() AS __TIME,
CONNECTIONPROPERTY(''protocol_type'') AS __PROTOCOL
FROM INSERTED
END'
PRINT @SCRIPT
EXEC (@SCRIPT)
SELECT @SCRIPT = 'CREATE TRIGGER [dbo].[__LOG_UPDATE_' + @TABLE + '] ON [dbo].[' + @TABLE + ']
WITH ENCRYPTION, EXECUTE AS CALLER
FOR UPDATE
AS
BEGIN
INSERT INTO __LOG_' + @TABLE + '(' +
@COLUMNS + '
__DB_USER,
__APP_NAME,
__HOST_NAME,
__IP_ADD,
__EVENT,
__TIME,
__PROTOCOL)
SELECT ' + @COLUMNS + '
SUSER_SNAME() AS __DB_USER ,
APP_NAME () AS __APP_NAME,
HOST_NAME() AS __HOST_NAME,
CONNECTIONPROPERTY(''client_net_address'') __IP_ADD,
''U'' AS __EVENT,
GETDATE() AS __TIME,
CONNECTIONPROPERTY(''protocol_type'') AS __PROTOCOL
FROM INSERTED
END'
PRINT @SCRIPT
EXEC (@SCRIPT)
SELECT @SCRIPT = 'CREATE TRIGGER [dbo].[__LOG_DELETE_' + @TABLE + '] ON [dbo].[' + @TABLE + ']
WITH ENCRYPTION, EXECUTE AS CALLER
FOR DELETE
AS
BEGIN
INSERT INTO __LOG_' + @TABLE + '(' +
@COLUMNS + '
__DB_USER,
__APP_NAME,
__HOST_NAME,
__IP_ADD,
__EVENT,
__TIME,
__PROTOCOL)
SELECT ' + @COLUMNS + '
SUSER_SNAME() AS __DB_USER ,
APP_NAME () AS __APP_NAME,
HOST_NAME() AS __HOST_NAME,
CONNECTIONPROPERTY(''client_net_address'') __IP_ADD,
''D'' AS __EVENT,
GETDATE() AS __TIME,
CONNECTIONPROPERTY(''protocol_type'') AS __PROTOCOL
FROM DELETED
END'
PRINT @SCRIPT
EXEC (@SCRIPT)
FETCH NEXT FROM _LOGS_ INTO @TABLE
END
COMMIT
END TRY
BEGIN CATCH
ROLLBACK
PRINT 'ERROR: ' + ERROR_MESSAGE()
END CATCH
CLOSE _LOGS_
DEALLOCATE _LOGS_
GO
腳本3
-- =============================================
-- Author: Carlos Aguilar
-- Create date: April 2016
-- =============================================
CREATE PROCEDURE dbo.SelectLogByDate
(
@StartDate DATETIME,
@EndDate DATETIME
)
AS
BEGIN
CREATE TABLE #TRANSACTIONS
(
[TIMESTAMP] DATETIME,
STATION NVARCHAR(128),
[USER] NVARCHAR(128)
)
DECLARE @TABLE NVARCHAR(MAX)
DECLARE @SQL NVARCHAR(MAX)
DECLARE @ParmDefinition NVARCHAR(MAX);
SET @ParmDefinition = N'@StartDate DATETIME, @EndDate DATETIME'
DECLARE _LOGS_ CURSOR FOR
SELECT NAME FROM SYSOBJECTS WHERE TYPE = 'U' AND NAME LIKE '__LOG_%'
OPEN _LOGS_
BEGIN TRY
BEGIN TRAN
FETCH NEXT FROM _LOGS_ INTO @TABLE
WHILE (@@FETCH_STATUS = 0)
BEGIN
SELECT @SQL = 'INSERT INTO #TRANSACTIONS ([TIMESTAMP], STATION, [USER]) SELECT __TIME, __HOST_NAME, __DB_USER FROM ' +
@TABLE + ' ' +
'WHERE __TIME BETWEEN @StartDate AND @EndDate'
EXECUTE sp_executesql @SQL, @ParmDefinition,
@StartDate = @StartDate,
@EndDate = @EndDate
FETCH NEXT FROM _LOGS_ INTO @TABLE
END
COMMIT
END TRY
BEGIN CATCH
END CATCH
CLOSE _LOGS_
DEALLOCATE _LOGS_
SELECT STATION,
[USER],
MIN([TIMESTAMP]) AS "START TIME",
MAX([TIMESTAMP]) AS "END TIME",
DATEDIFF(MINUTE, MIN([TIMESTAMP]), MAX([TIMESTAMP])) AS DURATION,
COUNT(*) AS TRANSACTIONS
FROM #TRANSACTIONS
GROUP BY STATION,
[USER]
END
GO
等待1或2天的用戶的交易,並將它們運行
DECLARE @return_value int
EXEC @return_value = [dbo].[SelectLogByDate]
@StartDate = ?, --Param here
@EndDate = ? --Param here
SELECT 'Return Value' = @return_value
GO
我的SQL Server 2012年 – user173552
爲什麼有多個行的STATION1 \t 2014年10月2日15:00 \t User2? – FLICKER
我的要求是獲取用戶每次訪問該站的詳細信息......(在給定時間內,只有一個用戶可以在該站上)。 – user173552