SQL Server触发将新行中的值插入到具有多对多关系的另一个表中 [英] SQL Server trigger insert values from new row into another table with many-to-many relationship

查看:102
本文介绍了SQL Server触发将新行中的值插入到具有多对多关系的另一个表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个表:

  • tbl_profiles
  • tbl_options
  • tbl_profileOption

具有多对多关系

tbl_options上的插入触发器是什么,我需要用来将新选项添加​​到tbl_profiles上的每个配置文件,默认情况下,isChoose的值为0

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

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

换句话说:

如果我向tbl_option添加新的Option(4 .... E),则触发器必须在tbl_profileOption上插入两个新行:

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

  • 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)

  • 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

    这篇关于SQL Server触发将新行中的值插入到具有多对多关系的另一个表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆