SQL UPDATE在SQL Server 2008中生成错误

时间:2014-07-16 16:54:13

标签: sql-server-2008

我有两张桌子。两个表都有“SchoolCode”和“SchoolName”列。但是在一张表中,一些“SchoolCode”是Null。我想在比较SchoolName的基础上用其他表的相应SchoolCode更新Null“SchoolCode”。

我的查询是:

update DSDClusters
set dsdclusters.EMISCodeofSchool = Censusgm.emiscode
where DSDClusters.NameofSchool = Censusgm.school_name  
  and DSDClusters.EMISCodeofSchool is null

但它会产生错误

  

无法绑定多部分标识符“Censusgm.school_name”。

从两个表中提取的数据样本

emiscode    school_name     dist_nm     teh_nm    EMISCodeofSchool  NameofSchool

38410806    GPS DERA NOOR   SARGODHA    KOT MOMIN NULL              GPS DERA NOOR
38420682    GGPS ABBAS PUR  SARGODHA    SARGODHA  NULL              GGPS ABBAS PUR
38430199    GPS ISLAM PURA  SARGODHA    SHAHPUR   NULL              GPS ISLAM PURA

请建议任何帮助

1 个答案:

答案 0 :(得分:0)

尝试将其作为选择,如果它不错,请运行更新。

SELECT *
--UPDATE DS SET EMISCodeofSchool = C.emiscode
FROM DSDClusters DS
LEFT OUTER JOIN Censusgm C
   ON DS.NameofSchool = C.school_name  
WHERE DS.EMISCodeofSchool is null