简体   繁体   中英

SQL Server trigger insert values from new row into another table with many-to-many relationship

I have three tables:

  • tbl_profiles
  • tbl_options
  • tbl_profileOption

with many-to-many relationship

在此处输入图片说明

what the insert trigger on tbl_options I have to use to add the new options to each profile on tbl_profiles , by default value of isChoose is 0

and on the opposite side when insert a new profile binding it with all options on tbl_options

In other words :

If I add new Option (4....E) to tbl_option , the trigger must be insert two new rows on tbl_profileOption :

1....4.......0
2....4.......0

I hope that my question is clear,

thank for all who try to help me... I got the solution

  • first trigger on tbl_option

     go Create TRIGGER insertProfileToOption ON dbo.tbl_options AFTER INSERT AS insert into tbl_profileOption (profileOption_profileId, profileOption_optoinId) (select tbl_profiles.profile_id, @@IDENTITY from tbl_profiles) 
  • second trigger on tbl_profile

     go Create TRIGGER insertOptionToProfile ON dbo.tbl_profiles AFTER INSERT AS insert into tbl_profileOption (profileOption_profileId, profileOption_optoinId) (select @@IDENTITY, tbl_options.option_id from tbl_options) 

if there is another solution this will be good, thank you

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