【问题标题】:Pass scope_identity into a stored procedure将 scope_identity 传递给存储过程
【发布时间】:2012-06-06 14:11:30
【问题描述】:

有很多关于将scope_identity 从子存储过程传递给父存储过程的讨论。我有一个相反的场景,即在父存储过程中插入行,我想将标识值传递给子存储过程。

但是,直接将 scope_identity 传递给子过程会在 SQL Server Management Studio 中产生解析错误。

create procedure parent
as
begin
   --  insert a record
   exec child scope_identity() -- this does not work
end

但是,使用局部变量可以解决问题。

create procedure parent
as
begin
   --  insert a record
   set @id = scope_identity()
   exec child @id -- this works
end

直接传入scope_identity()作为输入参数失败是什么原因?

【问题讨论】:

标签: sql-server tsql stored-procedures scope-identity


【解决方案1】:

您不能将函数结果作为存储过程参数传递。您需要执行后一种方法,将结果保存到变量中,然后传递变量。

【讨论】:

  • 感谢您提供答案。看来我必须忍受临时变量。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-17
  • 2012-10-13
  • 2016-04-27
  • 2016-04-05
相关资源
最近更新 更多