Reputation: 553
I've searched all over and come up with a blank. I am trying to update one record and insert others in one table depending upon conditions in another table. Both tables have a common RecordID (an nvarchar as they include characters).
Easiest explained in In English:
There are other conditions where a record has a value in ColumnA AND ColumnB but not in ColumnC etc. in which case only one new record need be added, but they should come out in the wash.
Upvotes: 0
Views: 552
Reputation: 1710
You can insert values into a table by using insert with select query.
So the queries for your scenario will look like the below ones.
create table @RecordsUpdated(RecordID nvarchar(max))
update t
set ColumnBla = TableA.ColumnA
output
inserted.RecordID
into @RecordsUpdated
from
TableB
inner join TableA on TableB.RecordID = TableA.RecordID
where
TablesA.ColumnA is not null
and TablesB.ColumnA is not null
and TablesC.ColumnA is not null
insert into TableB(RecordID, ColumnBLa)
select TableA.RecordID + 'A', ColumnB
from
TableA
inner join @RecordsUpdated on @RecordsUpdated.RecordID = TableA.RecordID
insert into TableB(RecordID, ColumnBLa)
select TableA.RecordID + 'A', ColumnC
from
TableA
inner join @RecordsUpdated on @RecordsUpdated.RecordID = TableA.RecordID
Upvotes: 1