简体   繁体   English

Sql Server触发器将值从新行插入另一个表

[英]Sql Server trigger insert values from new row into another table

I have a site using the asp.net membership schema. 我有一个使用asp.net成员资格模式的网站。 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. 我想在aspnet_users表上设置一个触发器,它将user_id和新行的user_name插入到另一个表中。

How do I go about getting the values from the last insert? 如何从最后一次插入中获取值?

I can select by the last date_created but that seems smelly. 我可以选择最后一个date_created,但这看起来很臭。 Is there a better way? 有没有更好的办法?

try this for sql server 试试这个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

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. 您使用插入触发器 - 在触发器内,插入的行项将作为逻辑表INSERTED公开,其具有与定义触发器的表相同的列布局。

Delete triggers have access to a similar logical table called DELETED . 删除触发器可以访问名为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. 更新触发器可以访问包含更新值的INSERTED表和包含要更新的值的DELETED表。

You can use OLD and NEW in the trigger to access those values which had changed in that trigger. 您可以在触发器中使用OLDNEW来访问在该触发器中已更改的那些值。 Mysql Ref Mysql参考

In a SQL Server trigger you have available two psdeuotables called inserted and deleted. 在SQL Server触发器中,您有两个名为inserted和deleted的psdeuotable。 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. 因此,在代码中考虑多行插入。

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. 当您处于触发器的上下文中时,您可以访问逻辑表INSERTED,其中包含刚刚插入表中的所有行。 You can build your insert to the other table based on a select from Inserted. 您可以根据从Inserted中选择的内容将插入内容构建到另一个表。

Create 
trigger `[dbo].[mytrigger]` on `[dbo].[Patients]` after update , insert as
begin
     --Sql logic
     print 'Hello world'     
 end 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL Server触发将新行中的值插入到具有多对多关系的另一个表中 - SQL Server trigger insert values from new row into another table with many-to-many relationship SQL Server 2008-在插入/更新时触发将值复制到另一个表的触发器吗? - SQL Server 2008 - On Insert/Update Trigger that copies values to another table? 使用来自另一个表 mysql 的值将新行插入到表中 - Insert new row into a table with values from another table mysql SQL SERVER为现有表的每一行插入新列值 - SQL SERVER Insert New Column Values for every row of existing table PL / SQL触发器将值插入到另一个表 - PL/SQL trigger to insert values to another table 从表中复制一行并在SQL-SERVER中插入一个新值 - Copy a row from a table and insert a new value in SQL-SERVER SQL 如何根据相同的行值从另一个表插入? - SQL how to insert from another table based on same row values? 从SQL Server 2008中的另一个表插入值 - Insert values from another Table in sql server 2008 触发器:将新插入的列中的 null 值替换为同一行中的另一列值 - Trigger: Replace null values in column from new insert with another column value from SAME row 如果相同的值,则从另一个表更新 Postgres 表的列值,否则为不匹配的值插入新行 - Update a Postgres table columns values from another table if same values otherwise insert a new row for unmatched value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM