How can you update fields across tables in SQL Server?

To update a field across tables in SQL Server, you can use an inner join subquery, a self-join, or the MERGE statement.

Option 1: Method 1 involves using an inline subquery.

UPDATE1
SET 字段 = (SELECT 字段 FROM2 WHERE 条件)
WHERE 条件;

Option 1: Method 2: Utilizing self-connection.

UPDATE1
SET 字段 =2.字段
FROM1
INNER JOIN2 ON1.条件 =2.条件;

Option 3: Utilize the MERGE statement.

MERGE INTO1
USING2
ON 条件
WHEN MATCHED THEN
    UPDATE SET1.字段 =2.字段;

In the three mentioned methods, it is necessary to set appropriate conditions based on the actual situation to ensure the accuracy of cross-table updates.

bannerAds