简体   繁体   English

SQL Server触发将新行中的值插入到具有多对多关系的另一个表中

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

I have three tables: 我有三个表:

  • tbl_profiles tbl_profiles
  • tbl_options tbl_options
  • tbl_profileOption 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 我必须使用tbl_options上的插入触发器将新选项添加​​到tbl_profiles上的每个配置文件的情况,默认情况下isChoose值为0

and on the opposite side when insert a new profile binding it with all options on tbl_options 在另一侧,当插入新配置文件并将其与tbl_options上的所有选项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 : 如果我向tbl_option添加新的Option(4 .... E),则触发器必须在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 第一次触发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 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 如果有其他解决方案,那将是很好,谢谢

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

相关问题 SQL行插入到一个表中+在一个语句内的链接表中插入键(多对多关系) - SQL row insert into a table + keys into its linking table within one statement (many-to-many relationship) SQL用于从多对多关系返回值 - SQL for returning values from a many-to-many relationship 更新多对多关系表上的值 - Updating the values on Many-to-Many relationship table 如何编写SQL查询以检查一个表上的多对多关系是否是另一个表上多对多关系的子集? - How do I write a SQL query to check if a many-to-many relationship on one table is a subset of a many-to-many relationship on another table? 从另一个多对多表中的多对多表引用一行是不是一个坏主意 - Is it a bad idea to reference a row from a many-to-many table in another many-to-many table 多对多关系INSERT - Many-to-many relationship INSERT MySQL - 如何插入具有多对多关系的表中 - MySQL - How to insert into table that has many-to-many relationship 使用Spring Boot将数据插入到关系表中(多对多) - Insert data into relationship table (many-to-many) with Spring boot 将外键从多对多关系导出到另一个表sql - Exporting foreign key from many to many relationship to another table sql 在多对多表关系中查找最小和最大链接值 - Find smallest and largest linked values in a many-to-many table relationship
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM