SQL Server SCOPE_IDENTITY不运行链接服务器

时间:2016-08-19 13:35:14

标签: sql-server sql-server-2008 sql-server-2008-r2

我有两台使用链接服务器连接的服务器。当我运行这个程序时,它会将记录一个插入到另一个,那个时候我想从我的第二个服务器获取ID - 这是怎么做的?

  Alter PROCEDURE [dbo].[stp_TransferJob]  
(  
  @JOB_id AS bigint,
  @PartyId as int,
  @Commission as decimal(18,2),
  @myid as varchar(max),
  @ident_out as bigint output
)  
AS
BEGIN  


INSERT INTO [111.163.103.122].Taxi.dbo.booking (FromLocTypeId,ToLocTypeId,FromLocId,ToLocId,VehicleTypeId,DriverId,ReturnDriverId,CustomerId,CustomerName,CustomerEmail,CustomerPhoneNo,CustomerMobileNo,JourneyTypeId,BookingNo,BookingDate,NoofPassengers,NoofLuggages,NoofHandLuggages,PickupDateTime,ReturnPickupDateTime,IsCompanyWise,CompanyId,FareRate,PaymentTypeId,SpecialRequirements,FromAddress,ToAddress,FromPostCode,ToPostCode,FromDoorNo,ToDoorNo,FromStreet,ToStreet,FromFlightNo,FromComing,BookingStatusId,DistanceString,AutoDespatch,AutoDespatchTime,AddOn,AddBy,AddLog,EditOn,EditBy,EditLog,OrderNo,PupilNo,ParkingCharges,WaitingCharges,ExtraDropCharges,MeetAndGreetCharges,CongtionCharges,TotalCharges,DepartmentId,ReturnFareRate  
,ArrivalDateTime,MasterJobId,DisablePassengerSMS,DisableDriverSMS,IsCommissionWise,DriverCommission,DespatchDateTime,JobOfferDateTime,BookingTypeId,DriverCommissionType,IsBidding,IsQuotation,CostCenterId,CashRate,AccountRate,WaitingMins  
,ExtraMile,AcceptedDateTime,POBDateTime,STCDateTime,ClearedDateTime,CancelReason,TotalTravelledMiles,CompanyPrice,SubCompanyId,PartyId,
FromOther,ToOther,TransferJobId,TransferJobCommission,BookingTypeId,ViaString) 

SELECT   
FromLocTypeId,ToLocTypeId,FromLocId,ToLocId,VehicleTypeId,null,ReturnDriverId,CustomerId,CustomerName,CustomerEmail,CustomerPhoneNo,CustomerMobileNo,JourneyTypeId,BookingNo,BookingDate,NoofPassengers,NoofLuggages,NoofHandLuggages,PickupDateTime,ReturnPickupDateTime,IsCompanyWise,null,FareRate,PaymentTypeId,SpecialRequirements,FromAddress,ToAddress,FromPostCode,ToPostCode,FromDoorNo,ToDoorNo,FromStreet,ToStreet,FromFlightNo,FromComing,BookingStatusId,DistanceString,AutoDespatch,AutoDespatchTime,AddOn,AddBy,AddLog,EditOn,EditBy,EditLog,OrderNo,PupilNo,ParkingCharges,WaitingCharges,ExtraDropCharges,MeetAndGreetCharges,CongtionCharges,TotalCharges,DepartmentId,ReturnFareRate
,ArrivalDateTime,MasterJobId,DisablePassengerSMS,DisableDriverSMS,IsCommissionWise,DriverCommission,DespatchDateTime,JobOfferDateTime,BookingTypeId,DriverCommissionType,IsBidding,IsQuotation,CostCenterId,CashRate,AccountRate,WaitingMins  
,ExtraMile,AcceptedDateTime,POBDateTime,STCDateTime,ClearedDateTime,CancelReason,TotalTravelledMiles,CompanyPrice,1,@PartyId
,FromOther,ToOther,@JOB_id,@Commission,10,ViaString

FROM Booking  
WHERE Id = @JOB_id  ;
select SCOPE_IDENTITY() 
SET @ident_out = @@IDENTITY

-------------------------------------------------------------------------  
INSERT INTO [111.163.103.122].Taxi.dbo.booking_ViaLocations (  
BookingId,ViaLocTypeId,ViaLocTypeLabel,ViaLocTypeValue,ViaLocId,ViaLocValue,ViaLocLabel  ) 



SELECT   
@ident_out,ViaLocTypeId,ViaLocTypeLabel,ViaLocTypeValue,ViaLocId,ViaLocValue,ViaLocLabel  
FROM Booking_ViaLocations  
WHERE BookingId = @JOB_id  

END
return @ident_out

GO

SCOPE_IDENTITY()和@@ IDENTITY显示空值如何获取ID请帮帮我

1 个答案:

答案 0 :(得分:0)

您可以使用存储过程返回int值

create PROCEDURE [dbo].[stp_TransferJob]  
(  
  @JOB_id AS bigint,
  @PartyId as int,
  @Commission as decimal(18,2)
)  
AS
BEGIN  



    INSERT INTO [111.163.103.122].Taxi.dbo.booking (FromLocTypeId,ToLocTypeId,FromLocId,ToLocId,VehicleTypeId,DriverId,ReturnDriverId,CustomerId,CustomerName,CustomerEmail,CustomerPhoneNo,CustomerMobileNo,JourneyTypeId,BookingNo,
    FromOther,ToOther,TransferJobId,TransferJobCommission,BookingTypeId,ViaString)  

    SELECT   
    FromLocTypeId,ToLocTypeId,FromLocId,ToLocId,VehicleTypeId,null,ReturnDriverId,CustomerId,CustomerName,CustomerEmail,CustomerPhoneNo,CustomerMobileNo,JourneyTypeId,BookingNo,FromOther,ToOther,@JOB_id,@Commission,10,ViaString

    FROM Booking  
    WHERE Id = @JOB_id 

RETURN (@@IDENTITY)

END

然后当您在其他服务器上调用您的程序时:

DECLARE @ident INT;
EXECUTE @ident = stp_TransferJob (plus other parameters)

由于使用错误代码更常见,您可能更喜欢输出参数:

create PROCEDURE [dbo].[stp_TransferJob]  
(  
  @JOB_id AS bigint,
  @PartyId as int,
  @Commission as decimal(18,2),
  @ident_out as int output
)  
AS
BEGIN  



    INSERT INTO [111.163.103.122].Taxi.dbo.booking (FromLocTypeId,ToLocTypeId,FromLocId,ToLocId,VehicleTypeId,DriverId,ReturnDriverId,CustomerId,CustomerName,CustomerEmail,CustomerPhoneNo,CustomerMobileNo,JourneyTypeId,BookingNo,
    FromOther,ToOther,TransferJobId,TransferJobCommission,BookingTypeId,ViaString)  

    SELECT   
    FromLocTypeId,ToLocTypeId,FromLocId,ToLocId,VehicleTypeId,null,ReturnDriverId,CustomerId,CustomerName,CustomerEmail,CustomerPhoneNo,CustomerMobileNo,JourneyTypeId,BookingNo,FromOther,ToOther,@JOB_id,@Commission,10,ViaString

    FROM Booking  
    WHERE Id = @JOB_id 

SET @ident_out = @@IDENTITY

END

然后,当您调用该过程时,您将使用output关键字添加声明的参数。请参阅msdn