INSERT INTO Test.Items
(Name, Type, Number)
SELECT 'Pork', 'Sausage', COALESCE(l.Number, 6)
FROM Lookup l
WHERE l.Type = 'Sausage'
РЕДАКТИРОВАТЬ : Дополнительные образцы на основе комментариев ниже.
Использование UNION ALL
для объединения строк:
INSERT INTO Test.Items
(Name, Type, Number)
SELECT 'Pork', 'Sausage', COALESCE(l.Number, 6)
FROM Lookup l
WHERE l.Type = 'Sausage'
UNION ALL
SELECT 'Halibut', 'Fish', COALESCE(l.Number, 7)
FROM Lookup l
WHERE l.Type = 'Fish'
Использование временной таблицы:
CREATE TEMPORARY TABLE tmpItems (
Name VARCHAR(255),
Type VARCHAR(255),
Number INT
)
INSERT INTO tmpItems
(Name, Type, Number)
VALUES ('Pork', 'Sausage', 6)
INSERT INTO tmpItems
(Name, Type, Number)
VALUES ('Halibut', 'Fish', 7)
INSERT INTO Test.Items
(Name, Type, Number)
SELECT t.Name, t.Type, COALESCE(l.Number, t.Number)
FROM tmpItems t
LEFT JOIN Lookup l
ON t.Type = l.Type