如何在某些数据更新时自动生成sql更新脚本?

发布于 2024-08-31 05:25:51 字数 700 浏览 2 评论 0原文

我想在每次修改数据库中的值时自动生成更新脚本。换句话说,如果存储过程、查询或任何内容更新表 c 中的值 b 的列 a(作为 pk 列 (i,j...k),我想生成此:

update c set a=b where i=... and j=... and k=...

并将其存储在某处(例如作为表中的原始字符串)。为了使事情复杂化,我希望仅在特定用户进行更新时才生成脚本。 好消息是我已经为所有表定义了主键。

我可以看到如何使用触发器来执行此操作,但我需要为每个表生成特定的触发器,并在每次架构更改时更新它们。

我想有一些内置的方法可以做到这一点,因为 SQL Server 有时需要存储这种东西(例如使用事务复制时),但到目前为止找不到任何东西......有什么想法吗?

我也对自动生成触发器的方法感兴趣(可能使用触发器 - 元触发器,嗯? - 因为当架构更改时我需要自动更新触发器)


这就是我想这样做的原因。我有一个生产数据库 A 和一个数据仓库数据库 B。我使用事务复制来保持数据库同步。我需要能够更新数据仓库中的某些值而不更新生产数据库。这对于事务复制来说工作得很好,但是引发了两个问题,从而导致了上述问题:

  • 如何跟踪我所做的手动更改? (我计划对生成的脚本进行源代码控制。)
  • 如果需要,如何恢复此数据库,因为执行新快照会丢失我的所有更新? (应用自动生成的脚本解决了问题)

I'd like to automatically generate an update script each time a value is modified in my database. In other words, if a stored procedure, or a query, or whatever updates column a with value b in table c (which as a pk column (i,j...k), I want to generate this :

update c set a=b where i=... and j=... and k=...

and store it somewhere (for example as a raw string in a table). To complicate things, I want the script to be generated only if the update has been made by a specific user.
Good news is that I've got a primary key defined for all my tables.

I can see how to do this using a trigger, but I would need to generate specific triggers for each table, and to update them each and every-time my schema changes.

I guess there are some built-in ways to do this as SQL server sometimes need to store this kind of things (while using transactional replication for example), but couldn't find anything so far ... any ideas ?

I'm also interested in ways to automatically generate triggers (probably using triggers - meta triggers, huh? - since I will need to update triggers automatically when the schema change)


Here's why I want to do this. I've got a production database A and a datawarehouse database B. I'm using transactional replication to keep the databases in sync. I need to be able to update some values in the datawarehouse without updating the production database. This works fine with transactional replication, but raises two issues, which lead to the above question :

  • How do I keep track of the manual changes I've made ? (I plan on source-controlling the generated script.)
  • How do I restore this database if needed, as doing a new snapshot would loose all my updates ? (applying the auto-generated script solves the problem)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空名 2024-09-07 05:25:51

与其考虑用脚本来更新数据,为什么不存储已更改的数据呢?

由触发器填充的历史表对此很有用,因为每次更改都会向历史表写入一个新行,记录更改的日期/时间以及执行更改的用户。

历史表将与原始表相同,但添加了以下列(当然,使用您自己的命名约定):

update_date datetime DEFAULT CURRENT_TIMESTAMP
update_by_user varchar(100) DEFAULT SYSTEM_USER

然后您在表上创建一个触发器,以便在基表上进行 UPDATE:

CREATE TRIGGER MyTable_Record_Changes
   ON MyTable
   AFTER UPDATE
AS 
BEGIN
    INSERT [MyTable_History] ([col_1], [col_2], ...)
    SELECT [col_1], [col_2], ...
    FROM deleted
END

现在您拥有完整的历史记录对您的数据进行的更改、更改时间和更改人。
然后,您可以根据需要进行这些更改并比较/更新其他数据库。

Rather than thinking in terms of a script to update the data, why not store the data which has changed?

History tables populated by a trigger are good for this as each change writes a new row to the history table recording the date/time of the change as well as the user performing the change.

The history table will be the same as the original table with the addition of the following columns (use your own naming convention, of course):

update_date datetime DEFAULT CURRENT_TIMESTAMP
update_by_user varchar(100) DEFAULT SYSTEM_USER

Then you create a trigger on your table for UPDATE on your base table:

CREATE TRIGGER MyTable_Record_Changes
   ON MyTable
   AFTER UPDATE
AS 
BEGIN
    INSERT [MyTable_History] ([col_1], [col_2], ...)
    SELECT [col_1], [col_2], ...
    FROM deleted
END

Now you have a full history of the changes made to your data, when and by whom.
You can then take those changes and compare/update the other database as necessary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文