在下图中,'DodgyOldTable'和'MainTable'之间存在1:1的关系.表'选项'包含'OptionDesc'字段中带有'OptionVal1','OptionVal2'和'OptionVal3'的记录.我需要使用DodgyOldTable中的select来插入MainTable_Option.像这样的东西:
INSERT MainTable_Option ([MainTableID],[OptionID]) SELECT ID, (CASE WHEN OptionVal1 = 'y' THEN (SELECT OptionID FROM Option WHERE OptionDesc = 'OptionVal1') END FROM DodgyOldTable
如果可能,我想避免使用几个不同的select语句来执行插入操作.
alt text http://www.freeimagehosting.net/uploads/863f10bf5f.jpg
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')