我的SQL Anywhere的12.0.1數據庫有兩個表:需要幫忙MERGE語句工作
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
);
和:
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;
我的程序(用C#編寫)進行批量複製成ListDetailsLoad表,然後運行存儲過程。存儲過程的工作是將數據移動到ListDetails表中。這裏的存儲過程:
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;
這個編譯好。問題是,當我運行它,我得到以下錯誤信息:
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.
如何解決MERGE語句?
可以請你告訴我你是怎麼格式化,創造語句,以便nicesly> –
我用空格和製表符在原來,排隊的一切了,因爲我想?當我創建帖子時,我將代碼複製並粘貼到記事本中,並用4個空格替換了所有的選項卡,然後將所有內容都備份。帖子中的對齊方式並不完全是原來的,但是沒關係。 –
多麼痛苦.i認爲你有自動化解決方案 –