【问题标题】:How to get the last identity value using sp_executesql in StoredProcedure?如何在存储过程中使用 sp_executesql 获取最后一个标识值?
【发布时间】:2018-05-15 05:55:09
【问题描述】:

我正在使用stored proceduresp_executesql 插入记录。一旦我使用sp_executesql 插入记录,我需要该会话中最后插入的identity 字段值。

ALTER proc [dbo].[spHoldTransaction] 


@RegisterNo int,
@StoreID int,
@Department varchar(50),
@TransactionDateFrom date,
@TransactionDateTo date,
@Comment Varchar(50)

AS
BEGIN

    DECLARE @RegisterID int;
    DECLARE @DatabaseName varchar(15);
    DECLARE @Batch int;


    SELECT @RegisterID=ID FROM Register WHERE Register.Number = @RegisterNo;

    SELECT @Batch = BatchNumber From Batch WHERE Status = 0 and RegisterID = @RegisterID


    SET @DatabaseName = 'xxx'

    SELECT  @Department=''''+REPLACE(@Department,',',''',''')+''''

    DECLARE @Qry nvarchar(MAX);

    DECLARE @ParamDefinition nvarchar(MAX);
    SET @ParamDefinition = N'@comment nvarchar(50),@StoreID int,@Batch int'

    SET @Qry = '
      INSERT INTO '+@DatabaseName+'.dbo.TransactionHold
     (
          [StoreID]       
          ,[HoldComment]          
          ,[BatchNumber]
          ,[ShippingNotes]
     )
     SELECT         
          @StoreID AS [StoreID]       
          ,@Comment AS [HoldComment]         
          ,@Batch AS [BatchNumber]
          ,'''' AS [ShippingNotes];       

     '

    EXECUTE sp_executesql @Qry, @ParamDefinition, @Comment, @StoreID, @Batch
    SELECT SCOPE_IDENTITY()

END

当我执行上面的存储过程时,它返回空。但是TransactionHold 有标识列Id

【问题讨论】:

  • 你试过@@identity
  • @AswaniMadhavan 是的,我尝试使用 Scope_Identity()Identity()@@IdentityIdent_Current()。一切都返回 null
  • 对我来说@@identity 工作..
  • @AswaniMadhavan 你能告诉我你的问题吗,让我看看。我现在试过了。它仍然返回 null
  • 该代码是特定于产品的。您使用的是哪个 dbms?

标签: sql-server tsql stored-procedures identity


【解决方案1】:

尝试在执行 sql 过程的同一范围内检索标识,并将值作为OUT 参数返回。做这些改变:

SET @ParamDefinition = N'@comment nvarchar(50),@StoreID int,@Batch int, @identity int out'

SET @Qry = '
      INSERT INTO '+@DatabaseName+'.dbo.TransactionHold
     (
          [StoreID]       
          ,[HoldComment]          
          ,[BatchNumber]
          ,[ShippingNotes]
     )
     SELECT         
          @StoreID AS [StoreID]       
          ,@Comment AS [HoldComment]         
          ,@Batch AS [BatchNumber]
          ,'''' AS [ShippingNotes];       

     SET @identity = @@IDENTITY

     '

DECLARE @identity INT

EXECUTE sp_executesql @Qry, @ParamDefinition, @Comment, @StoreID, @Batch, @identity OUT

SELECT @identity

【讨论】:

  • 我复制了您的整个查询并运行它。它仍然返回 null,但记录已插入。数据库中的标识值为 46
  • 你有链接到你插入表的触发器吗?使用SCOPE_IDENTITY() 而不是@@IDENTITY,还要确保您的表列确实是一个标识。作为最后的手段,您可以尝试使用MERGE OUTPUT
  • 我确认有Id 是身份列。我也试过Scope_Identity()。我再告诉你一件事。我正在插入不同的服务器,如[192.xxx.xxx.xxx].DatabaseName.dbo.TableName。这就是它不返回的原因?
  • @glennmaxwell 是的,您的 SQL Server 会话变量不会在整个链接服务器中工作,因为它将在远程服务器上创建不同的会话。请阅读这篇文章,了解如何在链接服务器上检索身份stackoverflow.com/questions/5708996/…
  • 是的,你是对的,我尝试使用本地服务器。它正在与本地合作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-26
  • 2017-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-10
相关资源
最近更新 更多