UPDATE语句SQL

时间:2015-02-03 20:35:15

标签: sql ms-access

在下面的图片中,我有一个表,我执行了INSERT查询。此SQL查询将信息添加为新记录。我想将它们添加到已经存在的记录中(如图所示)。这可能吗?也许有UPDATE声明?任何帮助将不胜感激。

table data view

当前SQL语句:

INSERT INTO ShouldImportMetricsIDsTable
            (FormularyName,ClientName)
SELECT [Formulary Name],
       [Client Name]
FROM   FN_DataDump_ALL_02012015 as F
WHERE  EXISTS (SELECT 1
               FROM   mytable as M
               WHERE  F.formularyId = M.formularyId) 

1 个答案:

答案 0 :(得分:2)

这样的事情可以解决问题:

UPDATE ShouldImportMetricsIDsTable AS Imp
INNER JOIN FN_DataDump_ALL-02012015 AS F
    ON Imp.FormularyId = F.FormularyId
SET Imp.FormularyName = F.FormularyName,
    Imp.ClientName = F.ClientName
WHERE Imp.FormularyId = F.FormularyId

我不认为需要加入myTable,但如果您需要在SET之前添加此内容:

INNER JOIN myTable AS M
    ON F.formularyId = M.formularyId

如果你这样做,你可能需要使用Access SQL中那些不完整的括号,如下所示:

UPDATE ((ShouldImportMetricsIDsTable AS Imp
INNER JOIN FN_DataDump_ALL-02012015 AS F
    ON Imp.FormularyId = F.FormularyId)
INNER JOIN myTable AS M
    ON F.formularyId = M.formularyId)
SET Imp.FormularyName = F.FormularyName,
    Imp.ClientName = F.ClientName
WHERE Imp.FormularyId = F.FormularyId