sql - Update and insert TSQL -
how can modify tsql update statement below insert partid
of source table not in partid field of item id ='500224' in destination table. if doesn't exist need insert.
use [dbname]; go update m set [partid] = a.[partid] select * [sourcetable] m inner join [desttable] on m.[bomitem] = a.[itemid] , m.bomentry = a.bomentry , m.bomrev = a.rev m.bomrev=a.rev , m.partid <> a.partid , m.linenbr = a.bomentry ; go
in source table
in destination table
for example in pictures above partid 100280
doesn't exist in destination table want add , keep others same.
you can insert missing partid destination table, using left join
null
check.
insert [desttable] (bomitem, bomrev, bomenty, linenbr, dtype, partid, revid, qty) select st.itemid, st.rev, st.bomenty, st.linenbr, 0 dtype, st.partid, null revid, st.qty [sourcetable] st left join [desttable] dt on dt.bomitem = st.itemid , dt.partid = st.partid , dt.bomrev = st.rev , dt.bomentry = st.bomentry -- remove these conditions if not need dt.partid null
since few of columns mismatching between 2 tables, have added proper column names in select
Comments
Post a Comment