DEFINITIONS

Definitions More Info.
Definition ID14.117
TitleSQL
CategoryNOTES
DefinitionAudit table and store procedure - Audit tabloda log tutma
Definition DescriptionUSE [DPAudit]
GO

/****** Object: Table [dbo].[DOCaudit] Script Date: 21.03.2024 13:20:03 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[DOCaudit](
[RowID] [int] IDENTITY(1,1) NOT NULL,
[CompanyID] [int] NULL,
[InstanceID] [int] NULL,
[DateKey] [int] NULL,
[CreateDate] [datetime2](0) NULL,
[ObjectName] [nvarchar](256) NULL,
[EventTime] [datetime2](0) NULL,
[Statement] [nvarchar](max) NULL,
[DatabaseName] [nvarchar](250) NULL,
[ClassType] [varchar](2) NULL,
[SessionServerPrincipalName] [nvarchar](256) NULL,
[ServerPrincipalName] [nvarchar](256) NULL,
[ActionID] [varchar](4) NULL,
[Succeeded] [bit] NULL,
[SessionID] [smallint] NULL,
[SendTo] [tinyint] NULL,
PRIMARY KEY CLUSTERED
(
[RowID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

ALTER TABLE [dbo].[DOCaudit] ADD CONSTRAINT [DF_DOCaudit_sendTo] DEFAULT ((0)) FOR [SendTo]
GO


USE [DPAudit]
GO

/****** Object: StoredProcedure [dbo].[spKariyerAudit_MB] Script Date: 21.03.2024 13:19:41 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE proc [dbo].[spKariyerAudit_MB]
as
DECLARE @auditpath as nvarchar(100)


SET @auditpath = (select distinct log_file_path from sys.server_file_audits where [name] like 'KariyerAudit%')+'*.sqlaudit'
declare @lasteventtime as [datetime2](0)

if not exists (SELECT TOP 1 EventTime FROM DOCaudit ORDER BY EventTime DESC)
BEGIN
SET @lasteventtime = DATEADD(minute,-30,GETUTCDATE())
END
ELSE
BEGIN
DECLARE @a [datetime2](0) = (SELECT MAX(EventTime) FROM DOCaudit)
SET @lasteventtime = convert([datetime2](0),SWITCHOFFSET(convert(datetimeoffset, @a),'-03:00'))
SET @lasteventtime = DATEADD(SECOND,+1,@lasteventtime)
END


INSERT INTO [DPAudit].[dbo].[DOCaudit] ([ObjectName],[EventTime],[Statement],[DatabaseName],[ClassType],[SessionServerPrincipalName],[ServerPrincipalName],[ActionID],[Succeeded],[SessionID])
SELECT
[object_name]
,convert([datetime2](0),SWITCHOFFSET(convert(datetimeoffset, [event_time]),'+03:00'))
,[statement]
,[database_name]
,[class_type]
,[session_server_principal_name]
,[server_principal_name]
,[action_id]
,[succeeded]
,[session_id]
FROM fn_get_audit_file (@auditpath,NULL,NULL)
WHERE [event_time] > @lasteventtime AND [statement] NOT LIKE 'INSERT INTO @myperms%SELECT%FROM sys.fn_builtin_permissions%'
UPDATE [DPAudit].[dbo].[DOCaudit] SET [ObjectName] = 'xxxxx' where [ObjectName] like '' OR [ObjectName] is NULL
UPDATE [DPAudit].[dbo].[DOCaudit] SET [statement] = '-' where [statement] like '' OR [statement] is NULL
UPDATE [DPAudit].[dbo].[DOCaudit] SET [statement] = REPLACE([statement],'''',' ')
UPDATE [DPAudit].[dbo].[DOCaudit] SET [DatabaseName] = '-' where [DatabaseName] like '' OR [DatabaseName] is NULL


--select * from #audittable



GO
RecordBycunay
Record Date21-03-2024 17:31:19
Düzenle
Kopyala
Sil