开发者

update records where null from subquery

I have been tryin开发者_运维百科g to get this query to work for the longest and I figure maybe I am not looking at it right. Basically I have records in a table where I am trying to locate where their is data conflicts. Example I have a field that is not a primary key, but I want to find all occurrences where the other columns data are not the same example: So basically if I have two johns and they both have different addresses I want to update my table to make them have the same, even if one John has a null address. I came up with

UPDATE Table
SET Address =Address 
SELECT * FROM Table WHERE Address != Address

but this doesnt seem to work


UPDATE t1
    SET address = t2.address
    FROM YourTable t1
        INNER JOIN YourTable t2
            ON t1.Name = t2.Name
    WHERE t1.PrimaryId <> t2.PrimaryId
        AND t1.address IS NULL
        AND t2.address IS NOT NULL


Assuming you want to update Table2 with Table1 values and that there exists a column called ID on both tables that links the data:

UPDATE t2
SET t2.Address = t1.Address
FROM Table2 as t2
INNER JOIN table1 t1
ON t1.Id = t2.Id

UPDATE:

Based on your comment, if you only want to update the null values in table2 then use this:

UPDATE t2
SET t2.Address = t1.Address
FROM Table2 as t2
INNER JOIN table1 t1
ON t1.Id = t2.Id
WHERE t2.Address is null
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜