我有一个查询,只要这些记录在表中不存在,我就插入一些记录,但我真正想做的是更新该记录在另一个表中的字段(如果存在),否则将其插入现在正在做。我将不胜感激任何帮助或建议。可以做到吗?
-- Insert into my my Toys table if record not already there (but would like to also update one of its fields)
INSERT INTO Toys (Date, ToyId)
SELECT inv.Date, inv.Id
FROM Inventory inv
JOIN InventoryStats invSt ON inv.Id = invSt.InventoryId
WHERE (invSt.IsFixed = 1 AND invSt.IsSent = 0 AND invSt.Date >= '01/01/2012 12:00 PM') AND (inv.Id NOT IN (SELECT ToyId FROM Toys))
基本上,如果 inv.Id 已经在 Toys 表中,我不想再次插入它,但我想在这个过程中更新它的一个标志,比如
UPDATE Toys SET NewShipDate = inv.Date WHERE ToyId = Inv.Id