1

我的 SQL Anywhere 12.0.1 数据库有两个表:

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 语句?

4

1 回答 1

2

它告诉你问题是什么。:)

在您的语句的左侧UPDATE,没有相关名称,并且它不知道ListDetailId要更新哪个。这应该有效:

USING "ListDetailsLoad" AS src
ON dst.ListDetailId = src.ListDetailId
WHEN MATCHED THEN UPDATE 
    SET dst.ListDetailId    = src.ListDetailId,
        dst.ListId          = src.ListId,
        dst.CountryId       = src.CountryId,
        dst.LocaleCode      = src.LocalCode,
        dst.Plate           = src.Plate,
        dst.HashedPlate     = src.HashedPlate,
        dst.AlarmClassId    = src.AlarmClassId,
        dst.BeginDate       = src.BeginDate,
        dst.EndDate         = src.EndDate,
        dst.ListPriorityId  = src.ListPriorityId,
        dst.VehicleTypeId   = src.VehicleType,
        dst.PlateClassId    = src.PlateClassId,
        dst.MakeId          = src.MakeId,
        dst.ModelId         = src.ModelId,
        dst."Year"          = src."Year",
        dst.ColorId         = src.ColorId,
        dst.Notes           = src.Notes,
        dst.OfficerNotes    = src.OfficerNotes,
        dst.ModifyDate      = NOW(),
        dst.FromToVersion   = dbVersion.NEXTVAL,
        dst.FromVersion     = NULL,
        dst.Subscriber      = src.Subscriber,
        dst.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;
于 2012-09-10T16:18:19.027 回答