SQL update from one Table to another based on a ID match

后端 未结 22 1333
太阳男子
太阳男子 2020-11-21 22:49

I have a database with account numbers and card numbers. I match these to a file to update any card numbers to the account number, so

22条回答
  •  遥遥无期
    2020-11-21 23:17

    I thought this is a simple example might someone get it easier,

            DECLARE @TB1 TABLE
            (
                No Int
                ,Name NVarchar(50)
            )
    
            DECLARE @TB2 TABLE
            (
                No Int
                ,Name NVarchar(50)
            )
    
            INSERT INTO @TB1 VALUES(1,'asdf');
            INSERT INTO @TB1 VALUES(2,'awerq');
    
    
            INSERT INTO @TB2 VALUES(1,';oiup');
            INSERT INTO @TB2 VALUES(2,'lkjhj');
    
            SELECT * FROM @TB1
    
            UPDATE @TB1 SET Name =S.Name
            FROM @TB1 T
            INNER JOIN @TB2 S
                    ON S.No = T.No
    
            SELECT * FROM @TB1
    

提交回复
热议问题