Fadi
Fadi

Reputation: 191

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

I have three tables:

with many-to-many relationship

enter image description here

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,

Upvotes: 1

Views: 3196

Answers (1)

Fadi
Fadi

Reputation: 191

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

Upvotes: 1

Related Questions