当匹配的行存在于另一个表中时更新行 [英] UPDATE row when matching row exists in another table

查看:21
本文介绍了当匹配的行存在于另一个表中时更新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅当另一个表中存在匹配的行时,我才需要将表上的字段更新为真,对于主表中该列当前为空的所有行.

I need to update a field on a table to be true only if a matching row exists in another table, for all the rows where the column is currently null in the main table.

这是对我想要实现的目标的描述:

This is a description of what I want to achieve:

UPDATE [LenqReloaded].[dbo].[Enquiry] A 
SET [ResponseLetterSent] = 1
WHERE [ResponseLetterSent] IS NULL
   AND EXISTS
       (
           SELECT * FROM [LenqReloaded].[dbo].[Attachment] B 
           WHERE A.[EnquiryID] = B.[EnquiryID]
       )

这在语法上不正确.

我无法通过 IF EXISTS... 语句对其进行编码,因为我没有 [EnquiryID] 没有从表中读取数据.

I can't code it via an IF EXISTS... statement because I don't have the [EnquiryID] without reading the data from the table.

我应该如何格式化我的 UPDATE 语句?

How should I format my UPDATE statement?

推荐答案

离你不远了...

UPDATE A
SET A.[ResponseLetterSent] = 1 
FROM [LenqReloaded].[dbo].[Enquiry] A
WHERE A.[ResponseLetterSent] IS NULL 
    AND EXISTS ( SELECT * FROM [LenqReloaded].[dbo].[Attachment] B WHERE A.[EnquiryID] = B.[EnquiryID] )

这篇关于当匹配的行存在于另一个表中时更新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆