如果 SQL 中列的值发生更改,如何将行从一个表移动到另一个表?

发布于 2024-12-07 12:31:52 字数 337 浏览 1 评论 0原文

我有两个表: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 技术交流群。

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

发布评论

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

评论(1

↙温凉少女 2024-12-14 12:31:52

SQL Server 中有两种主要的查询类型 - AFTER 和 INSTEAD OF。它们的工作原理就像听起来一样 - AFTER 执行您的原始查询,然后运行您的触发器。 INSTEAD OF 运行触发器来代替原始查询。在这种情况下,您可以使用其中任何一种,但方式不同。

之后:

create trigger hosts_unused
on Hosts
after UPDATE
as

insert into UnusedHosts
select h.<<your_columns>>...
from Hosts h
where h.unused = 1 --Or however else you may be denoting True


delete from Hosts
where unused = 0 --Or however else you may be denoting False
GO

代替:

create trigger hosts_unused
on Hosts
instead of UPDATE
as

insert into UnusedHosts
select i.<<your_columns>>...
from inserted i
where i.unused = 1 --Or however else you may be denoting True

delete h
from inserted i inner join
    Hosts h on i.host_id = h.host_id
where i.unused = 1 --Or however else you may be denoting True

update h
set hosts_column_1 = i.hosts_column_1,
    hosts_column_2 = i.hosts_column_2,
    etc
from inserted i inner join
    Hosts h on i.host_id = h.host_id
where i.unused = 0 --Or however else you may be denoting False
GO

在应用触发器时考虑性能始终很重要。如果您在 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:

create trigger hosts_unused
on Hosts
after UPDATE
as

insert into UnusedHosts
select h.<<your_columns>>...
from Hosts h
where h.unused = 1 --Or however else you may be denoting True


delete from Hosts
where unused = 0 --Or however else you may be denoting False
GO

INSTEAD OF:

create trigger hosts_unused
on Hosts
instead of UPDATE
as

insert into UnusedHosts
select i.<<your_columns>>...
from inserted i
where i.unused = 1 --Or however else you may be denoting True

delete h
from inserted i inner join
    Hosts h on i.host_id = h.host_id
where i.unused = 1 --Or however else you may be denoting True

update h
set hosts_column_1 = i.hosts_column_1,
    hosts_column_2 = i.hosts_column_2,
    etc
from inserted i inner join
    Hosts h on i.host_id = h.host_id
where i.unused = 0 --Or however else you may be denoting False
GO

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 the unused 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 the after UPDATE bit, and it'll work for inserts too.

Check out Books Online on the subject.

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