Subject Re: Updating with inner join - noobie help needed
Author Svein Erling Tysvær
> Hi Adam;
>
> Thank you for taking the time to reply, however I'm
> more confused than ever.

Hi Lin, I'll add to your confusion!

I do not completely agree with Alan (Adam is also on this list, but it
was Alan who answered you), I do not see why you should have two
occurences of VT_TR_TABLE.

> How would your query work if I had to update more than
> one field, for example....
>
> SET VT_TR_TABLE.DISTRI = VT_TR_CABLE_TABLE.CA,
> VT_TR_TABLE.A = VT_TR_CABLE_TABLE.B and so on

Then you would simply have to add another subselect. It does add to
the length of your query, but I think Ann (Harrison, she has more than
20 years experience with Firebird and it's predecessors) once said
that it didn't matter much performancewise.

Another option would be to write a stored procedure, in complex cases
that may help a lot performancewise.

> How would I control the number of records updated.
> Normally I would want to update all records in
> VT_TR_TABLE that can be updated.

Yes, Alan's suggestion updates all records, and could possibly set
some fields to NULL. There's no statement like UPDATE FIRST 200, but
you can use a WHERE clause to limit what get updated.

> I'm not sure what you're getting at with the reference
> to duplicate records. If for some reason the
> MASTER_KEY in either table (they're unique) appeared
> more than once would it not just possibly overwrite
> existing information. I'm not sure how anything could
> be duplicated?
>
> What would SELECT FIRST 1 do?

SELECT FIRST 1 ascertains that only the first record is returned.
Firebird focusses on getting the correct value in each field and
trying to put two or more values into a single field is regarded as an
error. Though as long as VT_TR_CABLE_TABLE.MASTER_KEY is unique, you
don't have to worry about this.

Here's my suggestion for your query:

UPDATE VT_TR_TABLE VTT
SET VTT.DISTRI =
(SELECT VTCT.CA FROM VT_TR_CABLE_TABLE VTCT WHERE
VTT.MASTER_KEY = VTCT.MASTER_KEY),
SET VTT.A =
(SELECT VTCT2.B FROM VT_TR_CABLE_TABLE VTCT2 WHERE
VTT.MASTER_KEY = VTCT2.MASTER_KEY)
WHERE EXISTS (SELECT * FROM VT_TR_CABLE_TABLE VTCT3 WHERE
VTT.MASTER_KEY = VTCT3.MASTER_KEY)

HTH,
Set (that's what people not knowing Norwegian call me, I wasn't named
after any UPDATE statement originally ;o)