I want to compare 3 SQL Server tables -


i created 3 tables table1, table2 , table3.

  • in table1 have 3 columns code, name & cityid
  • in table2 have 2 columns code & name
  • in table3 have 2 columns code & cityid

in these first have compare table1.name table2.name.

if condition satisfies, take table2.code , compare table3.code, take table3.cityid , compare table1.cityid.

if conditions satisfied, insert table2.code table1.code.

please me in writing query new sql.

thanks in advance.

please refer input sample data

table1 |name |code |cityid| |-------------|---------|------| |ersan |null | 1 | |plovdiv |null | 3 |

table2 |name |code | |-------------|---------| |ersan |401065 | |ersan |401065 | |plovdiv |143967 | |plovdiv |143967 | |plovdiv |143967 |

table3 |cityid |code | |---------|---------| |1 |401065 | |3 |143967 |

and output this:

table1 |name |code |cityid| |-------------|---------|------| |ersan |401065 | 1 | |plovdiv |143967 | 3 |

look have created sample data below

table__1

code    name    city 0001           p 002     b       c1 003     c       c2 004     d       c4 005     e       c5 006     f       c6 007     g       c7 008     h       c8 009           c9 

table__2 be

   code name    city     0001          p     002     b      c1     003     c      c2     004     d      c4     005     e      c5     006     f      c6     007 wrongname  c7   wrongcode h      c8     009       wrongcity 

now query insert record

insert table__3 select table__1.code,table__1.name,table__1.city  table__2 inner join table__1  on table__2.name=table__1.name , table__2.code=table__1.code , table__2.city=table__1.city 

if having issues comment below... best luck...


Comments