Perform INSERT with SELECT to insert multiple records

后端 未结 7 1294
情深已故
情深已故 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:18

    perhaps not the most efficient solution but by using a union, this should work.

    INSERT MainTable_Option ([MainTableID],[OptionID])
    SELECT ID, (SELECT OptionID FROM Option WHERE OptionDesc = 'OptionVal1')
    FROM DodgyOldTable dot
    WHERE OptionVal1 = 'y'
    UNION SELECT ID, (SELECT OptionID FROM Option WHERE OptionDesc = 'OptionVal2')
    FROM DodgyOldTable dot
    WHERE OptionVal2 = 'y'
    UNION SELECT ID, (SELECT OptionID FROM Option WHERE OptionDesc = 'OptionVal3')
    FROM DodgyOldTable dot
    WHERE OptionVal3 = 'y'
    

提交回复
热议问题