维护 DDL 日志数据的表
CREATE TABLE DDLAudit_All (
AuditID INT IDENTITY PRIMARY KEY,
DatabaseName varchar(256),
ActionOn varchar(100),
Event nvarchar(100),
ObjectName varchar(256),
TSQL nvarchar(2000),
Login varchar(256),
EventData XML NOT NULL,
CreatedDate datetime
);
GO
在数据库上创建 DDL 触发器
CREATE TRIGGER DDL_ALL_Events
ON DATABASE
FOR DDL_DATABASE_LEVEL_EVENTS
AS
DECLARE @ed XML
SET @ed = EVENTDATA()
INSERT INTO DDLAudit_All (DatabaseName, ActionOn, Event, ObjectName, TSQL, Login,EventData,CreatedDate)
VALUES
(
@ed.value('(/EVENT_INSTANCE/DatabaseName)[1]', 'varchar(256)'),
@ed.value('(/EVENT_INSTANCE/ObjectType)[1]', 'varchar(100)'),
@ed.value('(/EVENT_INSTANCE/EventType)[1]', 'nvarchar(100)'),
@ed.value('(/EVENT_INSTANCE/ObjectName)[1]', 'varchar(256)'),
@ed.value('(/EVENT_INSTANCE/TSQLCommand)[1]', 'nvarchar(2000)'),
@ed.value('(/EVENT_INSTANCE/LoginName)[1]', 'varchar(256)'),
EventData(),
GetDate()
) ;
创建表脚本
CREATE TABLE EmployeeTable_DDL
(
Emp_ID INT Identity,
Emp_name Varchar(100),
Emp_Sal Decimal (10,2)
)
在 DDLAudit_All 表中插入一条记录,如下所示
+--------------+----------+--------------+-------------------+----------------------------------------------------------------------------------------------------------------------+
| DatabaseName | ActionOn | Event | ObjectName | TSQL |
+--------------+----------+--------------+-------------------+----------------------------------------------------------------------------------------------------------------------+
| Pratice_DB | TABLE | CREATE_TABLE | EmployeeTable_DDL | CREATE TABLE EmployeeTable_DDL ( Emp_ID INT Identity, Emp_name Varchar(100), Emp_Sal Decimal (10,2) ) |
+--------------+----------+--------------+-------------------+----------------------------------------------------------------------------------------------------------------------+
执行 T-SQL 语句以更改数据类型长度,如下所示
alter table EmployeeTable_DDL alter column Emp_name varchar(200);
在执行上述 alter 语句后,一条记录插入到 DDLAudit_All 表中,如下所示
+--------------+----------+---------------------+--------------------+------------------------------------------------------------------+
| DatabaseName | ActionOn | Event | ObjectName | TSQL |
+--------------+----------+---------------------+--------------------+------------------------------------------------------------------+
| Pratice_DB | TABLE | ALTER_TABLE | EmployeeTable_DDL | alter table EmployeeTable_DDL alter column Emp_name varchar(200) |
+--------------+----------+---------------------+--------------------+------------------------------------------------------------------+
更改了类似列的数据类型长度右键单击表名称-> 选择设计-> 将数据长度从 200 更改为 300 并保存更改。
完成上述流程语句后,在 DDLAudit_All 表中插入 4 条记录,如下所示
+--------------+----------+--------------+-----------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+
| DatabaseName | ActionOn | Event | ObjectName | TSQL |
+--------------+----------+--------------+-----------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Pratice_DB | TABLE | CREATE_TABLE | Tmp_EmployeeTable_DDL | CREATE TABLE dbo.Tmp_EmployeeTable_DDL ( Emp_ID int NOT NULL IDENTITY (1, 1), Emp_name varchar(300) NULL, Emp_Sal decimal(10, 2) NULL ) ON [PRIMARY] |
| Pratice_DB | TABLE | ALTER_TABLE | Tmp_EmployeeTable_DDL | ALTER TABLE dbo.Tmp_EmployeeTable_DDL SET (LOCK_ESCALATION = TABLE) |
| Pratice_DB | TABLE | DROP_TABLE | EmployeeTable_DDL | DROP TABLE dbo.EmployeeTable_DDL |
| Pratice_DB | TABLE | RENAME | Tmp_EmployeeTable_DDL | EXECUTE sp_rename N'dbo.Tmp_EmployeeTable_DDL', N'EmployeeTable_DDL', 'OBJECT' |
+--------------+----------+--------------+-----------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------+
使用alter语句更改列的数据长度我们可以很容易地找到哪些列数据长度已更改,但使用设计窗口更改列的数据长度我们无法找到哪些列数据长度已更改。
当我们使用设计窗口更改数据长度时,如何知道哪一列数据长度发生了变化。