【发布时间】: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()作为输入参数失败是什么原因?
【问题讨论】:
-
从语法上讲,您可以传递一些函数(例如
@@IDENTITY)。扩展此列表is an open request on the Connect site -
@Martin - 谢谢。这么多年后,我第一次知道我不能将表达式作为参数传递。我有点震惊。 SQL2012出来了,不知道有没有改进。
标签: sql-server tsql stored-procedures scope-identity