Trying to figure out how 2 use MERGE INTO.
To simplify the database, I want to combine the table tPostalCode with the table tAddress., with their relationship based on
Code: Select all
"tAddress"."FKPostalCodeID" = "tPostalCode"."PostalCodeID"
There is some knowledge on the internet, but nothing related to HSQLDB, that is useful.
Notes:
- During development it was decided that every person entered in the database will have an Address, even if the Address in 'unknown'. This leads to:
- tAddress holds a lot of Addresses for which there are no PostalCoses known, and therefore the value of KFPostalCodeID = NULL.
- tAddress was copied into tAddressPLUSPostalCode for development reasons
Code: Select all
INSERT INTO "tAddressPLUSPostalCode" ( "PostalCode" )
SELECT "PostalCode"
FROM "tPostalCode", "tAddressPLUSPostalCode"
WHERE "tAddressPLUSPostalCode"."FKPostalCodeID" = "tPostalCode"."PostalCodeID"
The latest one is: 1: user lacks privilege or object not found: tPostalCode.PostalCodeID
This is what I came up with so far:
Code: Select all
MERGE INTO "tAddressPLUSPostalCode" USING
(
SELECT
"PostalCodeID", "PostalCode"
FROM
"tPostalCode"
) AS vals(x,y) ON
(
"tAddressPLUSPostalCode"."FKPostalCodeID" = "tPostalCode"."PostalCodeID"
)
WHEN MATCHED THEN
UPDATE SET "tAddressPLUSPostalCode"."PostalCode" = "tPostalCode"."PostalCode"
WHERE "tAddressPLUSPostalCode"."FKPostaclCodeID" = "tPostalCode"."PostalCodeID"
WHEN NOT MATCHED INSERT VALUES ( NULL)
Your help is really appreciated,
Dream