• 00
  • :
  • 00
  • :
  • 00

sql两张表关联更新字段

记录,推荐使用第三种,个人觉得比较好

--第一种写法,之前一直用,没有在意
    UPDATE sr_t_TCodeUserReciveCfg SET fUserID=a.fUserID,fImportFlag=NULL FROM dbo.vw_userinfo AS a INNER JOIN dbo.sr_t_TCodeUserReciveCfg AS b ON a.sLoginName=b.fImportFlag 
    WHERE b.fImportFlag IS NOT NULL
    

    --第二种写法,发现第一种写法有些不符合语法,这种比第一种好
    UPDATE b SET fUserID=a.fUserID,fImportFlag=NULL FROM dbo.vw_userinfo AS a INNER JOIN dbo.sr_t_TCodeUserReciveCfg AS b ON a.sLoginName=b.fImportFlag 
    WHERE b.fImportFlag IS NOT NULL 

    --第三种写法,这种方法更加简洁,是不是???
    UPDATE sr_t_TCodeUserReciveCfg SET fUserID=a.fUserID,fImportFlag=NULL FROM vw_userinfo a WHERE fImportFlag=a.sLoginName AND fImportFlag IS NOT NULL

 

posted @ 2017-10-12 15:22  Garson_Zhang  阅读(4502)  评论(0编辑  收藏  举报