简体   繁体   中英

SQL Server 2008 - Update Multiple Values With Matched Record

I like to do a matched on one column and update the value for another column between two tables. let's me demo how I like it to get updated.

Example:

-- I have Table1 and Table2 below

Table1 :

Name  Number
--------------
abc   1111
abc   2222
abc   3333
xyz   4444
xyz   5555
xyz   6666

Table2 :

Name  Number
-------------
abc   9999 (already exists, before updated)
abc   NULL
abc   NULL
abc   NULL
abc   NULL
abc   NULL
abc   NULL
xyz   NULL
xyz   NULL
xyz   NULL
xyz   NULL
xyz   NULL
xyz   8888 (already exists, before updated)

I want to do a match between Table1 and Table2, and update Table2 for matched names that are blank (NULL), and just the first matched records in Table1. As you can see, "abc" has only 3 records need to be updated, and you see only the first 3 blank (NULL) in Table2 get updated.

Table2 (after updated)

Name  Number
--------------
abc   9999 (already exists, before updated)
abc   1111
abc   2222
abc   3333
abc   NULL
abc   NULL
abc   NULL
xyz   4444
xyz   5555
xyz   6666
xyz   NULL
xyz   NULL
xyz   8888 (already exists, before updated)

I am not sure if this is possible. Please help.

Thanks,

I cannot remember exactly how to use it, and I don't have an example until I get to work again.. But you can achieve this by using a cursor and selecting out a single row at a time and updating it based on the data from a specific row in table 1..

See the help site on msdn: http://msdn.microsoft.com/en-us/library/ms180169.aspx

UPDATE:

I have now constructed a small piece of code that will do what you are asking ;-) See below.

What the cursor does, is to retrieve both name and number from table1, one by one. The when you have fetched this, it will then update table two where the name matches the fetched name from table one AND where the number in table two is NULL.

    DECLARE @name nvarchar(max)
    DECLARE @number int

    DECLARE name_cursor CURSOR FOR 
        SELECT name, number
        FROM #table1

    OPEN name_cursor

    FETCH NEXT FROM name_cursor
    INTO @name, @number

    WHILE @@FETCH_STATUS = 0
    BEGIN

        UPDATE top(1) #table2
        SET number = @number
        FROM #table2
        WHERE
            name = @name
            and number is null

        FETCH NEXT FROM name_cursor
        INTO @name, @number

    END
    CLOSE name_cursor
    DEALLOCATE name_cursor
; WITH tbl1 as (
SELECT Name, Number, ROW_NUMBER() OVER (PARTITION BY Name ORDER BY Number) rn
FROM Table1)
,tbl2 as (
SELECT Name, Number, ROW_NUMBER() OVER (PARTITION BY Name ORDER BY Number) rn
FROM Table2
WHERE Number IS NULL)

UPDATE tbl2
    SET Number = tbl1.Number
FROM tbl2
    INNER JOIN tbl1 ON tbl2.Name = tbl1.Name AND tbl2.rn = tbl1.rn

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM