My SQL Anywhere 12.0.1 database has two tables:
CREATE TABLE "ListDetails" (
ListDetailId UNIQUEIDENTIFIER NOT NULL PRIMARY KEY,
ListId UNIQUEIDENTIFIER NOT NULL,
CountryId VARCHAR(3) NULL REFERENCES "Countries" ( CountryId ) DEFAULT 'USA',
LocaleCode VARCHAR(3) NULL REFERENCES "Locales" ( LocaleCode ),
Plate VARCHAR(50) NULL,
HashedPlate UNIQUEIDENTIFIER NULL,
AlarmClassId INT NULL REFERENCES "AlarmClasses" ( AlarmClassId ),
BeginDate DATETIME NULL,
EndDate DATETIME NULL,
ListPriorityId INT NULL REFERENCES "ListPriorities" ( ListPriorityId ),
VehicleTypeId INT NULL REFERENCES "VehicleTypes" ( VehicleTypeId ),
PlateClassId INT NULL REFERENCES "PlateClasses" ( PlateClassId ),
MakeId INT NULL REFERENCES "VehicleBrands" ( VehicleBrandId ),
ModelId INT NULL REFERENCES "VehicleModels" ( VehicleModelId ),
"Year" INT NULL,
ColorId INT NULL REFERENCES "VehicleColors" ( VehicleColorId ),
Notes VARCHAR(8000) NULL,
OfficerNotes VARCHAR(8000) NULL,
IsNewRow BIT NOT NULL DEFAULT '1',
CreatedDate DATETIME NOT NULL DEFAULT NOW(),
i_active SMALLINT NOT NULL DEFAULT 1,
ModifyDate DATETIME NULL,
Subscriber UNIQUEIDENTIFIER NULL,
FromToVersion BIGINT NOT NULL,
FromVersion BIGINT,
InstanceId UNIQUEIDENTIFIER NOT NULL UNIQUE
);
And:
CREATE GLOBAL TEMPORARY TABLE "ListDetailsLoad" (
ListDetailId UNIQUEIDENTIFIER NOT NULL,
ListId UNIQUEIDENTIFIER NOT NULL,
CountryId VARCHAR(3) NULL,
LocaleCode VARCHAR(3) NULL,
Plate VARCHAR(50) NULL,
HashedPlate UNIQUEIDENTIFIER NULL,
AlarmClassId INT NULL,
BeginDate DATETIME NULL,
EndDate DATETIME NULL,
ListPriorityId INT NULL,
VehicleTypeId INT NULL,
PlateClassId INT NULL,
MakeId INT NULL,
ModelId INT NULL,
"Year" INT NULL,
ColorId INT NULL,
Notes VARCHAR(8000) NULL,
OfficerNotes VARCHAR(8000) NULL,
Subscriber UNIQUEIDENTIFIER NULL,
InstanceId UNIQUEIDENTIFIER NOT NULL
) NOT TRANSACTIONAL;
My program (written in C#) performs a bulk copy into the ListDetailsLoad table and then runs a stored procedure. The stored procedure’s job is to move the data into the ListDetails table. Here’s the stored procedure:
MERGE INTO "ListDetails" AS dst
USING "ListDetailsLoad" AS src
ON dst.ListDetailId = src.ListDetailId
WHEN MATCHED THEN UPDATE
SET ListDetailId = src.ListDetailId,
ListId = src.ListId,
CountryId = src.CountryId,
LocaleCode = src.LocalCode,
Plate = src.Plate,
HashedPlate = src.HashedPlate,
AlarmClassId = src.AlarmClassId,
BeginDate = src.BeginDate,
EndDate = src.EndDate,
ListPriorityId = src.ListPriorityId,
VehicleTypeId = src.VehicleType,
PlateClassId = src.PlateClassId,
MakeId = src.MakeId,
ModelId = src.ModelId,
"Year" = src."Year",
ColorId = src.ColorId,
Notes = src.Notes,
OfficerNotes = src.OfficerNotes,
ModifyDate = NOW(),
FromToVersion = dbVersion.NEXTVAL,
FromVersion = NULL,
Subscriber = src.Subscriber,
InstanceId = src.InstanceId
WHEN NOT MATCHED THEN INSERT
( ListDetailId, ListId, CountryId, LocaleCode, Plate, HashedPlate,
AlarmClassId, BeginDate, EndDate, ListPriorityId, VehicleTypeId, PlateClassId,
MakeId, ModelId, "Year", ColorId, Notes, OfficerNotes,
ModifyDate, FromToVersion, Subscriber, FromVersion, InstanceId )
VALUES
( src.ListDetailId, src.ListId, src.CountryId, src.LocaleCode, src.Plate, src.HashedPlate,
src.AlarmClassId, src.BeginDate, src.EndDate, src.ListPriorityId, src.VehicleTypeId, src.PlateClassId,
src.MakeId, src.ModelId, src."Year", src.ColorId, src.Notes, src.OfficerNotes,
NOW(), dbVersion.NEXTVAL, NULL, src.Subscriber, src.InstanceId );
TRUNCATE TABLE "ListDetailsLoad";
END;
This compiles fine. The problem is when I run it, I get the following error message:
Could not execute statement. Column "ListDetailId" found in more than one table or it is used more than once in the SELECT list -- it needs a correlation name.
How do I fix the MERGE statement?
It tells you what the problem is. 🙂
On the left side of your
UPDATEstatement, there are no correlation names, and it doesn’t know whichListDetailIdto update. This should work: