链接服务器获得@@identity
@@IDENTITY 函数的作用域是执行该函数的本地服务器上的当前会话。此函数不能应用于远程或链接服务器。若要获得其他服务器上的标识值,请在远程服务器或链接服务器上执行存储过程,并使(在远程或链接服务器的环境中执行的)该存储过程收集标识值,并将其返回本地服务器上的发出调用的连接。
解决实例:
1.建立存储过程
存储过程位置:PWX_Web.pwx_b2c (PWX_Web是链接服务器,pwx_b2c是数据库名)
存储过程代码:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
Create PROCEDURE [dbo].[ExecuteSQLGetIdentity]
@StrSQL nvarchar(4000)
AS
BEGIN
execute(@StrSQL)
return @@identity
END
2.调用存储过程:
declare @NewID int;
declare @StrSQL nvarchar(4000);
set @StrSQL = \’insert into pwx_shop_product_stockbill(Companyid,Bcompanyid,DocCode,ProductId,TypeNo,InNum,OutNum,Memo,SalesPrice,BlueId,OptId,IsAdjust,IsWriteOff,AllOrderID,OrderID,Buyer,ProductBaseID,PurchasePrice,OtherType,OtherId,TyreCode,StockId,RelyTableName,RelyId,isAudit,AuditorId,AuditTime)values(3099,0,\’\’103099201111180006\’\’,0,201,50,0,\’\’\’\’,0.00,0,3335,0,0,0,0,\’\’\’\’,9990,0.00,1,3080,\’\’\’\’,294,\’\’0\’\’,\’\’0\’\’,1,3335,\’\’2011-11-18 14:17:05\’\’);\’
exec @NewID=PWX_Web.pwx_b2c.dbo.ExecuteSQLGetIdentity @StrSQL;
select @NewID;