SQL Server 2008 - 将 AuditLog table 迁移到每个 table
SQL Server 2008 - Migrating AuditLog table to each table
我正在尝试将数据从许多 table 引用的 AuditLog table 迁移到 table 本身。我有一个 table、TablesWithAuditLogs,其中包含每个需要迁移的 table 的物理 table 名称。我很难确定仅 SQL 是否可行。
假设我有这五个 table,其中 3 个需要从 AuditLog.CreatedOn 列迁移到 self.CreatedOn 列(实际上更像是 80 table小号):
AuditLog
--------
ID: CreatedOn:
1 2015-01-02
2 2015-03-04
3 2015-05-06
4 2014-02-01
5 2010-01-01
6 2011-03-04
TablesWithAuditLogs
-------------------
ID: TableName:
41 'Contacts'
42 'Addresses'
43 'Items'
Contacts
--------
ID: AuditLogId: CreatedOn:
10 1 null
11 3 null
Addresses
---------
ID: AuditLogId: CreatedOn:
20 4 null
21 5 null
Items
-----
ID: AuditLogId: CreatedOn:
30 2 null
31 6 null
SQL 语句的预期结果为:
Contacts
--------
ID: AuditLogId: CreatedOn:
10 1 2015-01-02
11 3 2015-05-06
Addresses
---------
ID: AuditLogId: CreatedOn:
20 4 2014-02-01
21 5 2010-01-01
Items
-----
ID: AuditLogId: CreatedOn:
30 2 2015-03-04
31 6 2011-03-04
然后我可以从 80 个 table 中的每一个中删除 AuditLogId 列,并完全删除 AuditLog table。任何建议将不胜感激!
您可以使用 CURSOR
和 Dynamic SQL
:
DECLARE @sql VARCHAR(MAX) = ''
DECLARE @tableName VARCHAR(100)
DECLARE cur CURSOR LOCAL FORWARD_ONLY FOR
SELECT TableName FROM TablesWithAuditLogs
OPEN cur
FETCH FROM cur INTO @tableName
WHILE @@FETCH_STATUS = 0 BEGIN
SELECT @sql ='
UPDATE t
SET t.CreatedOn = a.CreatedOn
FROM [' + @tableName + '] t
INNER JOIN AuditLog a
ON a.ID = t.AuditLogID'
EXEC(@sql)
FETCH FROM cur INTO @tableName
END
CLOSE cur
DEALLOCATE cur
结果
Contacts
----------------------------------
ID AuditLogID CreatedOn
----------- ----------- ----------
10 1 2015-01-02
11 3 2015-05-06
Addresses
----------------------------------
ID AuditLogID CreatedOn
----------- ----------- ----------
20 4 2014-02-01
21 5 2010-01-01
Items
----------------------------------
ID AuditLogID CreatedOn
----------- ----------- ----------
30 2 2015-03-04
31 6 2011-03-04
我正在尝试将数据从许多 table 引用的 AuditLog table 迁移到 table 本身。我有一个 table、TablesWithAuditLogs,其中包含每个需要迁移的 table 的物理 table 名称。我很难确定仅 SQL 是否可行。
假设我有这五个 table,其中 3 个需要从 AuditLog.CreatedOn 列迁移到 self.CreatedOn 列(实际上更像是 80 table小号):
AuditLog
--------
ID: CreatedOn:
1 2015-01-02
2 2015-03-04
3 2015-05-06
4 2014-02-01
5 2010-01-01
6 2011-03-04
TablesWithAuditLogs
-------------------
ID: TableName:
41 'Contacts'
42 'Addresses'
43 'Items'
Contacts
--------
ID: AuditLogId: CreatedOn:
10 1 null
11 3 null
Addresses
---------
ID: AuditLogId: CreatedOn:
20 4 null
21 5 null
Items
-----
ID: AuditLogId: CreatedOn:
30 2 null
31 6 null
SQL 语句的预期结果为:
Contacts
--------
ID: AuditLogId: CreatedOn:
10 1 2015-01-02
11 3 2015-05-06
Addresses
---------
ID: AuditLogId: CreatedOn:
20 4 2014-02-01
21 5 2010-01-01
Items
-----
ID: AuditLogId: CreatedOn:
30 2 2015-03-04
31 6 2011-03-04
然后我可以从 80 个 table 中的每一个中删除 AuditLogId 列,并完全删除 AuditLog table。任何建议将不胜感激!
您可以使用 CURSOR
和 Dynamic SQL
:
DECLARE @sql VARCHAR(MAX) = ''
DECLARE @tableName VARCHAR(100)
DECLARE cur CURSOR LOCAL FORWARD_ONLY FOR
SELECT TableName FROM TablesWithAuditLogs
OPEN cur
FETCH FROM cur INTO @tableName
WHILE @@FETCH_STATUS = 0 BEGIN
SELECT @sql ='
UPDATE t
SET t.CreatedOn = a.CreatedOn
FROM [' + @tableName + '] t
INNER JOIN AuditLog a
ON a.ID = t.AuditLogID'
EXEC(@sql)
FETCH FROM cur INTO @tableName
END
CLOSE cur
DEALLOCATE cur
结果
Contacts
----------------------------------
ID AuditLogID CreatedOn
----------- ----------- ----------
10 1 2015-01-02
11 3 2015-05-06
Addresses
----------------------------------
ID AuditLogID CreatedOn
----------- ----------- ----------
20 4 2014-02-01
21 5 2010-01-01
Items
----------------------------------
ID AuditLogID CreatedOn
----------- ----------- ----------
30 2 2015-03-04
31 6 2011-03-04