2012-06-06 149 views
3

有许多关于将scope_identity从子项传递给父存储过程的讨论。我有一个相反的场景,在父存储过程中插入行,并且我想将标识值传递给子存储过程。将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()作为输入参数失败?

+0

可能的重复[转换整型和连接到TSQL中的varchar](http://stackoverflow.com/questions/4936180/cast-integer-and-concatenate-to-varchar-in-tsql) –

+0

句法上允许你被允许传递一些函数(例如'@@ IDENTITY')。展开此列表[是Connect网站上的打开请求](https://connect.microsoft.com/SQLServer/feedback/details/352110/t-sql-use-scalar-functions-as-stored-procedure-parameters) –

+0

@Martin - 谢谢。这是我第一次了解到,经过这么多年后,我无法将表达作为参数。我有点震惊。 SQL2012已经出来,但不知道这是否得到改善。 – intangible02

回答

3

无法将函数结果作为存储过程参数传递。你需要做后一种方法,通过将结果保存到一个变量,然后传递变量。

+0

感谢您提供答案。看来我必须忍受这个临时变量。 – intangible02

相关问题