Sql Server 触发器将新行中的值插入到另一个表中

发布于 2024-08-20 18:26:01 字数 153 浏览 2 评论 0原文

我有一个使用 asp.net 会员架构的网站。我想在 aspnet_users 表上设置一个触发器,将新行的 user_id 和 user_name 插入到另一个表中。

如何从上次插入中获取值?

我可以按最后创建的日期进行选择,但这看起来很臭。有更好的办法吗?

I have a site using the asp.net membership schema. I'd like to set up a trigger on the aspnet_users table that inserted the user_id and the user_name of the new row into another table.

How do I go about getting the values from the last insert?

I can select by the last date_created but that seems smelly. Is there a better way?

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

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

发布评论

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

评论(6

温柔戏命师 2024-08-27 18:26:01

对 sql server 试试这个

CREATE TRIGGER yourNewTrigger ON yourSourcetable
FOR INSERT
AS

INSERT INTO yourDestinationTable
        (col1, col2    , col3, user_id, user_name)
    SELECT
        'a'  , default , null, user_id, user_name
        FROM inserted

go

try this for sql server

CREATE TRIGGER yourNewTrigger ON yourSourcetable
FOR INSERT
AS

INSERT INTO yourDestinationTable
        (col1, col2    , col3, user_id, user_name)
    SELECT
        'a'  , default , null, user_id, user_name
        FROM inserted

go
玩物 2024-08-27 18:26:01

您使用插入触发器 - 在触发器内,插入的行项目将作为逻辑表 INSERTED 公开,该表具有与定义触发器的表相同的列布局。

删除触发器可以访问名为 DELETED 的类似逻辑表。

更新触发器可以访问包含更新值的 INSERTED 表和包含要更新值的 DELETED 表。

You use an insert trigger - inside the trigger, inserted row items will be exposed as a logical table INSERTED, which has the same column layout as the table the trigger is defined on.

Delete triggers have access to a similar logical table called DELETED.

Update triggers have access to both an INSERTED table that contains the updated values and a DELETED table that contains the values to be updated.

无悔心 2024-08-27 18:26:01

您可以在触发器中使用OLDNEW来访问该触发器中已更改的值。 Mysql 参考

You can use OLDand NEW in the trigger to access those values which had changed in that trigger. Mysql Ref

戴着白色围巾的女孩 2024-08-27 18:26:01

在 SQL Server 触发器中,您有两个可用的 psdeutables,称为插入和删除。它们包含记录的旧值和新值。

因此,在触发器中(您可以轻松查找创建触发器部分),您可以执行以下操作:

Insert table2 (user_id, user_name)
select user_id, user_name from inserted i
left join table2 t on i.user_id = t.userid
where t.user_id is null

编写触发器时,请记住它们对整批信息执行一次,它们不会逐行处理。因此,请考虑在代码中插入多行。

In a SQL Server trigger you have available two psdeuotables called inserted and deleted. These contain the old and new values of the record.

So within the trigger (you can look up the create trigger parts easily) you would do something like this:

Insert table2 (user_id, user_name)
select user_id, user_name from inserted i
left join table2 t on i.user_id = t.userid
where t.user_id is null

When writing triggers remember they act once on the whole batch of information, they do not process row-by-row. So account for multiple row inserts in your code.

国粹 2024-08-27 18:26:01

当您处于触发器上下文中时,您可以访问逻辑表 INSERTED,其中包含刚刚插入到表中的所有行。您可以根据“已插入”中的选择构建到另一个表的插入。

When you are in the context of a trigger you have access to the logical table INSERTED which contains all the rows that have just been inserted to the table. You can build your insert to the other table based on a select from Inserted.

我们的影子 2024-08-27 18:26:01
Create 
trigger `[dbo].[mytrigger]` on `[dbo].[Patients]` after update , insert as
begin
     --Sql logic
     print 'Hello world'     
 end 
Create 
trigger `[dbo].[mytrigger]` on `[dbo].[Patients]` after update , insert as
begin
     --Sql logic
     print 'Hello world'     
 end 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文