0

我正在尝试从一台服务器上由外键链接的 2 个表中复制数据,然后将它们插入另一台服务器上的主数据库中。

第一个表将生成一个新IDENTITY列,然后我想将其存储在一个变量中并将其插入到第二个表中。

我已经尝试过使用内部插入的表,但也SCOPE_IDENTITY()收到了远程服务器上不允许的错误

    DECLARE 

    @VisitSourceId  int,
    @SiteDomainId   int,
    @trpUTMid   bigint,
    @FlightPlus bit,
    @StartDate  datetime,
    @CountryId  int,
    @ProvinceId int,
    @Locationid int,
    @PlaceId    int,
    @EstabId    int,
    @CheckInDate    smalldatetime,
    @CheckOutDate   smalldatetime,
    @Rooms  int,
    @Room1Adults    int,
    @Room1Children  int,
    @Room2Adults    int,
    @Room2Children  int,
    @Room3Adults    int,
    @Room3Children  int,
    @OutcomeDate    datetime,
    @OutcomeId  smallint,
    @HotelsFound    smallint,
    @Notes  nvarchar,
    @id bigint

    DECLARE c CURSOR STATIC FOR 

    SELECT t.VisitSourceId, t.SiteDomainId, t.trpUTMid, t.FlightPlus, t.StartDate, t.CountryId, t.ProvinceId, t.Locationid, 
    t.PlaceId, t.EstabId, t.CheckInDate, t.CheckOutDate, t.Rooms, t.Room1Adults, t.Room1Children, t.Room2Adults, t.Room2Children, t.Room3Adults, 
    t.Room3Children, tc.OutcomeDate, tc.OutcomeId, tc.HotelsFound, tc.Notes

    FROM [MLT_VisitTracking].[dbo].TrackingAcomSearches_tbl t
    INNER JOIN TrackingAcomSearchesOutcome_tbl tc
    ON t.trpUTMid = tc.trpUTMid


    open c
    FETCH FIRST FROM c INTO @VisitSourceId, @SiteDomainId, @trpUTMid, @FlightPlus, @StartDate, @CountryId, @ProvinceId, @Locationid, 
    @PlaceId, @EstabId, @CheckInDate, @CheckOutDate, @Rooms, @Room1Adults, @Room1Children, @Room2Adults, @Room2Children, @Room3Adults, 
    @Room3Children, @OutcomeDate, @OutcomeId, @HotelsFound, @Notes  

    while @@fetch_status=0
    begin

    DECLARE @TmpTable TABLE (ID BIGINT)

    INSERT INTO [YAZOO].[MLT_VisitTracking].[dbo].TrackingAcomSearches_tbl
    (VisitSourceId, SiteDomainId, trpUTMid, FlightPlus, StartDate, CountryId, ProvinceId, Locationid, 
    PlaceId, EstabId, CheckInDate, CheckOutDate, Rooms, Room1Adults, Room1Children, Room2Adults, Room2Children, Room3Adults, 
    Room3Children)

    OUTPUT inserted.searchid into @TmpTable

    SELECT @VisitSourceId, @SiteDomainId, @trpUTMid, @FlightPlus, @StartDate, @CountryId, @ProvinceId, @Locationid, 
    @PlaceId, @EstabId, @CheckInDate, @CheckOutDate, @Rooms, @Room1Adults, @Room1Children, @Room2Adults, @Room2Children, @Room3Adults, 
    @Room3Children


    select top 1 @id = searchid from @tmptable


    INSERT INTO [YAZOO].[MLT_VisitTracking].[dbo].TrackingAcomSearchesOutcome_tbl
    (SearchId,
    trpUTMid,
    OutcomeDate,
    OutcomeId,
    HotelsFound,
    Notes)

    SELECT @id,
    @trpUTMid,
    @OutcomeDate,
    @OutcomeId,
    @HotelsFound,
    @Notes


    DELETE FROM [MLT_VisitTracking].[dbo].TrackingAcomSearches_tbl WHERE trpUTMid=@trpUTMid
    DELETE FROM [MLT_VisitTracking].[dbo].TrackingAcomSearchesOutcome_tbl WHERE trpUTMid=@trpUTMid

    FETCH NEXT FROM c INTO @VisitSourceId, @SiteDomainId, @trpUTMid, @FlightPlus, @StartDate, @CountryId,
    @ProvinceId, @Locationid, @PlaceId, @EstabId, @CheckInDate, @CheckOutDate, @Rooms, @Room1Adults, @Room1Children,
    @Room2Adults, @Room2Children, @Room3Adults, @Room3Children, @OutcomeDate, @OutcomeId, @HotelsFound, @Notes  
    end

    close c
    deallocate c
4

2 回答 2

3

@@IDENTITY并且SCOPE_IDENTITY()是本地的

你在使用链接服务器吗?检查这个: 从另一台服务器(链接服务器)获取@@Identity

于 2012-11-27T17:08:49.737 回答
0

为了解决这个问题,我最终在目标服务器上创建了脚本作为存储过程,然后我可以使用范围标识。性能也大大提高,然后我配置了一个启用 RPC 输出的新链接服务器并远程调用该过程。这不是一个优雅的解决方案,但它只是暂时的

于 2012-11-27T22:46:18.103 回答