あるサーバーで外部キーによってリンクされた 2 つのテーブルからデータをコピーし、それらを別のサーバーのマスター データベースに挿入しようとしています。
最初のテーブルは新しいIDENTITY
列を生成し、これを変数に格納して 2 番目のテーブルに挿入します。
内部に挿入されたテーブルを使用してみ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