Subject | Re: Updating with inner join - noobie help needed |
---|---|
Author | Svein Erling Tysvær |
Post date | 2006-04-05T08:14:13Z |
> Hi Adam;Hi Lin, I'll add to your confusion!
>
> Thank you for taking the time to reply, however I'm
> more confused than ever.
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 thanThen you would simply have to add another subselect. It does add to
> 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
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.Yes, Alan's suggestion updates all records, and could possibly set
> Normally I would want to update all records in
> VT_TR_TABLE that can be updated.
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 referenceSELECT FIRST 1 ascertains that only the first record is returned.
> 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?
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)