sql server - How to find Match Records in SQL and if found then update -
i looking way loop through sql table , compare table , if 2 of columns values match update record in 1 table.
i have 2 tables hplcdata
, deschedule
, deschedule
has future records hplcdata
table have. wanting sql query in hplcdata
matching records in deschedule
table , if there update record in deschedule
data hplcdata
, mark in done. match defined having same batchid
, sampleage
.
how can acomplish this?
hplc data batchid | sampleage | ethanol | glucose 7254 20 7.8 4.5 deschedule batchid | sampleage | ethanol | glucose | samplecompleted 7254 20 null null null 7254 30 null null null
so of right have trigger need way reconcile data if trigger misses record , running query windows job.
this query use trigger:
alter trigger [dbo].[trigger_hplc_update_details_deschedule] on [dbo].[hplcdata] after insert begin declare @batchid int, @ethanol varchar(10), @glucose varchar(10), @sampleage varchar(10); select @batchid = bd.[batchid],@ethanol = [ethanol], @glucose= [dp1glucose], @sampleage = bd.sampleage inserted bd update [dbo].[deschedule] set [ethanol] = @ethanol, [glucose] = @glucose, [samplecompleted] = 1 [batchid] = @batchid , [sampleage] = @sampleage end
you can use merge statement.
something along:
merge dbo.deschedule target using (dbo.hplcdata) source on source.batchid = target.batchid , source.sampleage = target.sampleage when matched update ethanol = source.ethanol, glucose = source.glucose, samplecompleted = 1;
Comments
Post a Comment