sql server - INSERT rows from MAPPING table if not exist -


i have these 2 tables:

table p   | b     --------- x  | 2   x  | 7   x  | 10  x  | 28 y  | 24  mapping m  c   | d ----------- 7   | 2136 28  | 786 24  | 4212 124 | 5311 935 | 6012 

if can find matching value of column b in table p value in column c of mapping m, need add value of column d table p well.

for example, 1st record of x , 2 of table p, can't find 2 in column c of mapping m, nothing added.

2nd record x , 7, can find 7 in column c, gonna add x , 2136 table p.

hence added table p:

x  | 2136 x  | 786 y  | 4212 

i find challenging insert these 3 rows.

i can this:

select p.*, m.d @tablep p     join @mappingtable m on (p.b = m.c) 

and put result temp table , insert, how do in 1 insert statement?

i realized it's quite straightforward:

insert @tablep select p.a, m.d @tablep p     join @mappingtable m on (p.b = m.c) 

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -