Answers:
是的,有办法
该表存储触发器的结果
USE [SOME_DATABASE]
GO
CREATE TABLE [dbo].[ddl_objects_log](
[date] [datetime] NULL DEFAULT (getdate()),
[login_name] [nvarchar](128) NULL,
[nt_user_name] [nvarchar](128) NULL,
[program_name] [nvarchar](128) NULL,
[host_name] [nvarchar](128) NULL,
[text] [xml] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
触发
USE [SOME_DATABASE]
GO
/****** Object: DdlTrigger [ddl_db_trigger] Script Date: 22/01/2015 13:41:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER [ddl_db_trigger]
ON DATABASE
FOR CREATE_FUNCTION,
CREATE_PROCEDURE,
CREATE_TABLE,
CREATE_TRIGGER,
CREATE_VIEW,
ALTER_FUNCTION,
ALTER_PROCEDURE,
ALTER_TABLE,
ALTER_TRIGGER,
ALTER_VIEW,
DROP_FUNCTION,
DROP_PROCEDURE,
DROP_TABLE,
DROP_TRIGGER,
DROP_VIEW,
CREATE_INDEX,
ALTER_INDEX,
DROP_INDEX
AS
BEGIN
set nocount ON
insert into ddl_objects_log(login_name, nt_user_name, program_name, host_name, text)
select login_name, nt_user_name, program_name, host_name, EVENTDATA() from sys.dm_exec_sessions WITH(NOLOCK) where session_id=@@SPID
set nocount OFF
END
GO
ENABLE TRIGGER [ddl_db_trigger] ON DATABASE
GO
查询审计触发器的结果
USE [SOME_DATABASE]
GO
SELECT top 10
REPLACE(CONVERT(VARCHAR(250), text.query('data(/EVENT_INSTANCE/PostTime)')),'T', ' ') as modify_datetime,
CONVERT(VARCHAR(215), text.query('data(/EVENT_INSTANCE/EventType)')) as event_type,
CONVERT(VARCHAR(225), text.query('data(/EVENT_INSTANCE/ServerName)')) as server_name,
CONVERT(VARCHAR(225), text.query('data(/EVENT_INSTANCE/DatabaseName)')) as database_name,
CONVERT(VARCHAR(225), text.query('data(/EVENT_INSTANCE/ObjectType)')) as object_type,
CONVERT(VARCHAR(225), text.query('data(/EVENT_INSTANCE/ObjectName)')) as object_name,
CONVERT(VARCHAR(215), text.query('data(/EVENT_INSTANCE/UserName)')) as user_name,
Login_name,
CONVERT(VARCHAR(MAX), text.query('data(/EVENT_INSTANCE/TSQLCommand/CommandText)')) as command_text
FROM [SOME_DATABASE].[dbo].[ddl_objects_log]
where CONVERT(VARCHAR(225), text.query('data(/EVENT_INSTANCE/ObjectName)')) like '%SOME_STORED_PROCEDURE%'
order by date desc
GO
另一位DBA可能有不同的看法,但我认为让这些信息对于DBA而言很重要。
FROM [SOME_DATABASE].[dbo].[ddl_objects_log]
...我应该创建表格吗?
如果您启用了默认跟踪并且尚未进行追溯,则可以使用它来查找谁创建了SP。
/*
Object Altered
Object Created
Object Deleted
*/
SELECT TE.name ,
v.subclass_name ,
DB_NAME(t.DatabaseId) AS DBName ,
T.NTDomainName ,
t.NTUserName ,
t.HostName ,
t.ApplicationName ,
t.LoginName ,
t.Duration ,
t.StartTime ,
t.ObjectName ,
CASE t.ObjectType
WHEN 8259 THEN 'Check Constraint'
WHEN 8260 THEN 'Default (constraint or standalone)'
WHEN 8262 THEN 'Foreign-key Constraint'
WHEN 8272 THEN 'Stored Procedure'
WHEN 8274 THEN 'Rule'
WHEN 8275 THEN 'System Table'
WHEN 8276 THEN 'Trigger on Server'
WHEN 8277 THEN '(User-defined) Table'
WHEN 8278 THEN 'View'
WHEN 8280 THEN 'Extended Stored Procedure'
WHEN 16724 THEN 'CLR Trigger'
WHEN 16964 THEN 'Database'
WHEN 16975 THEN 'Object'
WHEN 17222 THEN 'FullText Catalog'
WHEN 17232 THEN 'CLR Stored Procedure'
WHEN 17235 THEN 'Schema'
WHEN 17475 THEN 'Credential'
WHEN 17491 THEN 'DDL Event'
WHEN 17741 THEN 'Management Event'
WHEN 17747 THEN 'Security Event'
WHEN 17749 THEN 'User Event'
WHEN 17985 THEN 'CLR Aggregate Function'
WHEN 17993 THEN 'Inline Table-valued SQL Function'
WHEN 18000 THEN 'Partition Function'
WHEN 18002 THEN 'Replication Filter Procedure'
WHEN 18004 THEN 'Table-valued SQL Function'
WHEN 18259 THEN 'Server Role'
WHEN 18263 THEN 'Microsoft Windows Group'
WHEN 19265 THEN 'Asymmetric Key'
WHEN 19277 THEN 'Master Key'
WHEN 19280 THEN 'Primary Key'
WHEN 19283 THEN 'ObfusKey'
WHEN 19521 THEN 'Asymmetric Key Login'
WHEN 19523 THEN 'Certificate Login'
WHEN 19538 THEN 'Role'
WHEN 19539 THEN 'SQL Login'
WHEN 19543 THEN 'Windows Login'
WHEN 20034 THEN 'Remote Service Binding'
WHEN 20036 THEN 'Event Notification on Database'
WHEN 20037 THEN 'Event Notification'
WHEN 20038 THEN 'Scalar SQL Function'
WHEN 20047 THEN 'Event Notification on Object'
WHEN 20051 THEN 'Synonym'
WHEN 20549 THEN 'End Point'
WHEN 20801 THEN 'Adhoc Queries which may be cached'
WHEN 20816 THEN 'Prepared Queries which may be cached'
WHEN 20819 THEN 'Service Broker Service Queue'
WHEN 20821 THEN 'Unique Constraint'
WHEN 21057 THEN 'Application Role'
WHEN 21059 THEN 'Certificate'
WHEN 21075 THEN 'Server'
WHEN 21076 THEN 'Transact-SQL Trigger'
WHEN 21313 THEN 'Assembly'
WHEN 21318 THEN 'CLR Scalar Function'
WHEN 21321 THEN 'Inline scalar SQL Function'
WHEN 21328 THEN 'Partition Scheme'
WHEN 21333 THEN 'User'
WHEN 21571 THEN 'Service Broker Service Contract'
WHEN 21572 THEN 'Trigger on Database'
WHEN 21574 THEN 'CLR Table-valued Function'
WHEN 21577
THEN 'Internal Table (For example, XML Node Table, Queue Table.)'
WHEN 21581 THEN 'Service Broker Message Type'
WHEN 21586 THEN 'Service Broker Route'
WHEN 21587 THEN 'Statistics'
WHEN 21825 THEN 'User'
WHEN 21827 THEN 'User'
WHEN 21831 THEN 'User'
WHEN 21843 THEN 'User'
WHEN 21847 THEN 'User'
WHEN 22099 THEN 'Service Broker Service'
WHEN 22601 THEN 'Index'
WHEN 22604 THEN 'Certificate Login'
WHEN 22611 THEN 'XMLSchema'
WHEN 22868 THEN 'Type'
ELSE 'Hmmm???'
END AS ObjectType
FROM [fn_trace_gettable](CONVERT(VARCHAR(150), ( SELECT TOP 1
value
FROM [fn_trace_getinfo](NULL)
WHERE [property] = 2
)), DEFAULT) T
JOIN sys.trace_events TE ON T.EventClass = TE.trace_event_id
JOIN sys.trace_subclass_values v ON v.trace_event_id = TE.trace_event_id
AND v.subclass_value = t.EventSubClass
WHERE TE.name IN ( 'Object:Created', 'Object:Deleted', 'Object:Altered' )
-- filter statistics created by SQL server
AND t.ObjectType NOT IN ( 21587 )
-- filter tempdb objects
AND DatabaseID <> 2
-- get only events in the past 24 hours
AND StartTime > DATEADD(HH, -24, GETDATE())
ORDER BY t.StartTime DESC ;
这是dba的工作吗?知道谁在sql中创建了什么?
这取决于您为什么想知道对象是创建/更改还是删除。如果您认为对象是由未授权用户创建/删除或更改的,则可以使用事件通知来记录并通知您。确保已安装适当的过滤器。
为了完整回答此问题,我想提及一下- 默认情况下,我可以从SQL Server获取哪些事件信息?来自Aaron Bertrand。
DATEADD(HH, -24, GETDATE())
如果要获得超过24小时的结果,还需要进行修改。
select * from sys.procedures where type = 'P' order by create_date desc
AND DatabaseID= 'the_Database_I_Want
,即使有DATEADD(HH, -24, GETDATE())
评论,它也不会显示任何内容。难道我做错了什么?
我来晚了,但是我“做”安全和管理事务。
这是dba的工作吗?知道谁在sql中创建了什么?
是的。尽可能拥有或创建这些日志很重要。但是,我认为,直到它变得很重要时,才“打开此框”也是您的责任。换句话说-再次,我认为,您的工作是提供数据存储库,确保数据安全,确保已调优...然后远离内部数据,除非您被要求专门看看它,或者除非达到目标需要它。
我的观点和观点,但是多年来我一直在处理敏感和机密数据,并指出几乎没有[1]“打开盒子”,因为这会降低对用户的信任。
现在让我走一条不同的道路:如果没有人能快速弄清楚该程序如何响应边缘情况,该怎么办?可能需要您或我花一个小时才能思考的问题可能要花几分钟给作者:“啊,对,当...时,这件事失败了。”