Perform INSERT with SELECT to insert multiple records

后端 未结 7 1307
情深已故
情深已故 2021-02-05 08:13

In the diagram below there is a 1:1 relationship between \'DodgyOldTable\' and \'MainTable\'. Table \'Option\' contains records with \'OptionVal1\', \'OptionVal2\' and \'OptionV

7条回答
  •  庸人自扰
    2021-02-05 08:21

    INSERT 
      MainTable_Option 
      (
      MainTableID, 
      OptionID
      )
    SELECT
      d.ID, 
      o.OptionId
    FROM
      DodgyOldTable d
      INNER JOIN Option o ON
        (d.OptionVal1 = 'Y' AND o.OptionDesc = 'OptionVal1') OR
        (d.OptionVal2 = 'Y' AND o.OptionDesc = 'OptionVal2') OR
        (d.OptionVal3 = 'Y' AND o.OptionDesc = 'OptionVal3')
    

提交回复
热议问题