COMPARE TWO TABLES COPY TABLEA VALUES TO TABLEB

  • I HAVE TWO TABLES LIKE..

    TABLE_A

    SNO NAME MOBILE EMPID

    1 RAJU 9014494876 A12345

    2 VAMSHI 9765432123 B12367

    3 KARTHIK 986623456 C34567

    TABLE_B

    SNO NAME MOBILE EMPID

    1 SRINU 970123454 D123489

    2 RAJENDER 'NULL' B12367

    3 KARTHIK 934642389 C34567

    COMPARE TWO TABLE A & B EMPID AND IF TABLE B MOBILE FIELD 'NULL' UPADATE TABLE A MOBILE TO TABLE B

    i want o/p like table_b

    SNO NAME MOBILE EMPID

    1 SRINU 970123454 D123489

    2 RAJENDER 9765432123 B12367

    3 KARTHIK 934642389 C34567

    plz write quarie ...................

  • Join the two tables and use ISNULL/COALESCE to replace NULLs

    😎

    DECLARE @TABLE_A TABLE

    (

    SNO INT NULL

    ,NAME VARCHAR(15) NULL

    ,MOBILE VARCHAR(15) NULL

    ,EMPID VARCHAR(15) NULL

    );

    DECLARE @TABLE_B TABLE

    (

    SNO INT NULL

    ,NAME VARCHAR(15) NULL

    ,MOBILE VARCHAR(15) NULL

    ,EMPID VARCHAR(15) NULL

    );

    INSERT INTO @TABLE_A (SNO,NAME,MOBILE,EMPID)

    VALUES

    (1, 'RAJU' ,'9014494876' ,'A12345')

    ,(2, 'VAMSHI' ,'9765432123' ,'B12367')

    ,(3, 'KARTHIK' ,'986623456' ,'C34567')

    INSERT INTO @TABLE_B (SNO,NAME,MOBILE,EMPID)

    VALUES

    (1, 'SRINU' ,'970123454' ,'D123489')

    ,(2, 'RAJENDER' ,NULL ,'B12367' )

    ,(3, 'KARTHIK' ,'934642389' ,'C34567' )

    /* Any null values in table B are replaced by corresponding

    values from table A

    */

    SELECT

    B.SNO

    ,ISNULL(B.NAME ,A.NAME ) AS NAME

    ,ISNULL(B.MOBILE,A.MOBILE ) AS MOBILE

    ,ISNULL(B.EMPID ,A.EMPID ) AS EMPID

    FROM @TABLE_B B

    INNER JOIN @TABLE_A A

    ON B.SNO = A.SNO

  • use MERGE

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply