如何通过使用sql server调用另一个过程将值初始化为两个或多个变量?

如何通过使用sql server调用另一个过程将值初始化为两个或多个变量?,sql,stored-procedures,Sql,Stored Procedures,假设必须调用此过程 select @className = Name from dbo.ClassNames where id=@classId select @sectionName = SectionName from dbo.ClassSections where id=@sectionId select @className as 'Class Name',@sectionName as 'Section Name' 另一个程序是: declare @className nva

假设必须调用此过程

select @className = Name 
from dbo.ClassNames
where id=@classId

select @sectionName = SectionName 
from dbo.ClassSections
where id=@sectionId

select @className as 'Class Name',@sectionName as 'Section Name'
另一个程序是:

declare @className nvarchar(50),
@sectionName nvarchar(50)

EXEC    [dbo].[testAll]
        @regNo=@regNo

那么,如何通过调用上述过程将值分配给@className和@classSection呢?

使用输出创建过程

声明变量

declare @param1 int
declare @param2 varchar(20)
使用输出的exec过程

exec spOne @param1 OUTPUT, @param2 OUTPUT
现在,这些变量保存在spOne内部生成的值

exec spTwo @param1, @param2

我想你错过了什么。实际上,您不能像那样使用proc的返回,也不能在另一个过程中分配变量,因为它们与上下文无关。 我将通过以下两种方法之一解决此问题:

  • 定义一个返回所需2个字段的视图,这样您就可以通过@regNo筛选结果,并在另一个过程中返回其结果,就像普通表一样
  • 在过程中将返回用作输出字段。这可以是一个表或简单的1个字段

  • 希望这对…有帮助。

    thanx bro..它奏效了:)。。。我用输出差一点就输了parameters@Abdullah我很高兴这有帮助!你知道如何正式接受答案吗?对不起。。。我是新来的。我的意思是我不知道Hi @阿卜杜拉。如果这个或任何答案已经解决了你的问题,请考虑点击复选标记。这向更广泛的社区表明,你已经找到了一个解决方案,并给回答者和你自己带来了一些声誉。没有义务这样做。
    exec spTwo @param1, @param2