使用许多列审计 Microsoft SQL 服务器 table

Audit Microsoft SQL Server table with many columns

我无法在我的 table 上执行此审核。

我有一个包含 197 列的 table,我想对其进行审计。我们以前成功地使用过这个解决方案,但是 tables 总是有较少的列。

我实施的解决方案来自底部的url。

好像和我的table有这么多栏目有关。有人对此有任何意见吗?

Pop Rivett SQL Server Audit

我有两个想法 vs 错误的序数位置。

link 中的代码来自 2006 年。因此它是为 SQL Server 2005 编写的。基于 COLUMNS_UPDATED():

The ORDINAL_POSITION column of the INFORMATION_SCHEMA.COLUMNS view is not compatible with the bit pattern of columns returned by COLUMNS_UPDATED.

To obtain a bit pattern compatible with COLUMNS_UPDATED, reference the ColumnID property of the COLUMNPROPERTY system function when you query the INFORMATION_SCHEMA.COLUMNS view.

CREATE TABLE tab(id INT, col1 INT, col2 INT, col3 INT,
                 col4 INT, col5 INT, col6 INT, col7 INT, col8 INT);

ALTER TABLE tab DROP COLUMN col1;
ALTER TABLE tab DROP COLUMN col5;

ALTER TABLE tab ADD col9 INT;
ALTER TABLE tab ADD col1 INT;

SELECT column_name,
  ordinal_position,
  COLUMNPROPERTY(OBJECT_ID(TABLE_SCHEMA + '.' + TABLE_NAME),      
                 COLUMN_NAME, 'ColumnID') AS pos
FROM INFORMATION_SCHEMA.columns
 WHERE [TABLE_NAME] like '%tab%'
ORDER BY ordinal_position;

SqlFiddleDemo

输出:

╔══════════════╦═══════════════════╦═════╗
║ column_name  ║ ordinal_position  ║ pos ║
╠══════════════╬═══════════════════╬═════╣
║ id           ║                1  ║   1 ║
║ col2         ║                2  ║   3 ║
║ col3         ║                3  ║   4 ║
║ col4         ║                4  ║   5 ║
║ col6         ║                5  ║   7 ║
║ col7         ║                6  ║   8 ║
║ col8         ║                7  ║   9 ║
║ col9         ║                8  ║  10 ║
║ col1         ║                9  ║  11 ║
╚══════════════╩═══════════════════╩═════╝

结果:

ORIDINAL_POSITION -> no gaps, range 1-9
pos(aka ColumnId) -> gaps, range 1-11, 2 and 6 skipped

或者检查列是否已更改,您可以使用 UPDATE:

UPDATE ( column )

Returns a Boolean value that indicates whether an INSERT or UPDATE attempt was made on a specified column of a table or view. UPDATE() is used anywhere inside the body of a Transact-SQL INSERT or UPDATE trigger to test whether the trigger should execute certain actions.