简体   繁体   中英

SQL Server INSERT trigger how to get data of current row

I've never created a trigger before and I'm trying to read online but am a little confused.

I want to create a trigger on a table that on insert, it will grab some data from different columns and insert it into a few different other tables.

I'm not sure how to write the T-SQL to get the data from the columns..

insert into [othetTable] 
values ([col1 from row that was inserted], [col5 from row that was inserted])

What would the syntax be to get those values?

thanks

Use the inserted virtual table that is available to triggers. Note that there could be multiple rows in this table - your trigger could be processing multiple inserts at once.

Therefore, you need to use something like the following syntax:

insert into othertable
select col1, col5
from inserted

This will insert a row into othertable for each inserted row.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM