update column from two table

User avatar
sal21
PlatinumLounger
Posts: 4357
Joined: 26 Apr 2010, 17:36

update column from two table

Post by sal21 »

Habe table1 and table2
in table1 have dt data_esc agg fields
in table2 have rdt rdata_esc agg fields

i need with a vb code or a query tu update agg into table2 with agg from table1 based the match of dt&data_esec and rdt&rdata_esc

how to?

Hans is for you?

User avatar
HansV
Administrator
Posts: 78512
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: update column from two table

Post by HansV »

This will probably only work if there is a unique index on the combination of dt and data_esc.

The SQL would be:

UPDATE table2 INNER JOIN table1 ON table2.rdt = table1.dt AND table2.rdata_esc = table1.data_esc SET table2.agg = table1.agg
Last edited by HansV on 19 Oct 2010, 21:37, edited 1 time in total.
Reason: to correct error
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4357
Joined: 26 Apr 2010, 17:36

Re: update column from two table

Post by sal21 »

HansV wrote:This will probably only work if there is a unique index on the combination of dt and data_esc.

The SQL would be:

UPDATE table2 INNER UNION table1 ON table2.rdt = table1.dt AND table2.rdata_esc = table1.data_esc SET table2.agg = table1.agg
error in bold value....

Note:
rdt and dt are text format data_esc and rdat_esc are date format, peraphs is this the prob?

User avatar
HansV
Administrator
Posts: 78512
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: update column from two table

Post by HansV »

Sorry, I posted "air code". It should have been

UPDATE table2 INNER JOIN table1 ON table2.rdt = table1.dt AND table2.rdata_esc = table1.data_esc SET table2.agg = table1.agg
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4357
Joined: 26 Apr 2010, 17:36

Re: update column from two table

Post by sal21 »

HansV wrote:Sorry, I posted "air code". It should have been

UPDATE table2 INNER JOIN table1 ON table2.rdt = table1.dt AND table2.rdata_esc = table1.data_esc SET table2.agg = table1.agg
no prob... for "air code"... i know, u are "artist ispired" :grin: :laugh: now the code work perfect!

Note:
during the query i recived error refered the two different format on date_esec and rdata_esc i have used the same format (date) in all two fields and code query run good without error!