如果 SQL 中列的值发生更改,如何将行从一个表移动到另一个表?
我有两个表:Hosts 和 UnusedHosts。 Hosts 有 17 列,UnusedHosts 有 14 列,其中前 12 列与 Hosts 中的相同,第 13 列是用户名,他将主机移至 UnusedHosts,第 14 列是日期,即他执行此操作的日期。在 Hosts 中有一个列 Unused
,其值为 False
。我想做以下事情。如果我在 Hosts 中将此值更改为 True,那么它应该会自动删除到 UnusedHosts。
我该怎么做?有人可以提供一些例子吗?
PS:我的SQL知识很少,我只能使用非常简单的选择、更新、插入和删除命令。
谢谢!
I have two tables, Hosts, and UnusedHosts. Hosts has 17 columns, and UnusedHosts has 14 columns, where the first 12 is the same as in Hosts, and the 13th is a UserName, who moved a host to UnusedHosts, and the 14th is a date, when he did it. In Hosts there is a Column Unused
which is False
. I want do the following. If i change in Hosts this value to True
, then it should automatically removed to UnusedHosts.
How can i do this? Could someone provide some example?
P.S.: My SQL knowledge is very small, i can use only very simple selects, updates, inserts, and delete commands.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SQL Server 中有两种主要的查询类型 - AFTER 和 INSTEAD OF。它们的工作原理就像听起来一样 - AFTER 执行您的原始查询,然后运行您的触发器。 INSTEAD OF 运行触发器来代替原始查询。在这种情况下,您可以使用其中任何一种,但方式不同。
之后:
代替:
在应用触发器时考虑性能始终很重要。如果您在
Hosts
表上有很多更新,但只有少数更新设置了unused
列,那么 AFTER 触发器可能会给您带来更好的性能。 AFTER 触发器还有一个好处,您可以简单地将, INSERT
放在after UPDATE
位之后,并且它也适用于插入。查看有关该主题的联机丛书。
There's two main types of query in SQL Server - the AFTER and the INSTEAD OF. They work, much as they sound - the AFTER performs your original query, and then runs your trigger. The INSTEAD OF runs your trigger in place of the original query. You can use either in this case, though in different ways.
AFTER:
INSTEAD OF:
It's always important to think of performance when applying triggers. If you have a lot of updates on the
Hosts
table, but only a few of them are setting theunused
column, then the AFTER trigger is probably going to give you better performance. The AFTER trigger also has the benefit that you can simply put in, INSERT
after theafter UPDATE
bit, and it'll work for inserts too.Check out Books Online on the subject.