为什么我不能在OUTER APPLY
块中使用存储过程?我需要从存储过程中获取 int 值dbo.GetTeacherId
并在WHERE
子句中使用它。这是我的代码:
USE [StudentsDb]
DECLARE @teacherIdOut int;
SELECT StudentLastName, StudentFirstName, StudentMiddleName, LessonName, Score, TLastName, TFirstName, TMiddleName
FROM Scores
JOIN Students
ON Scores.StudentId=Students.StudentId
JOIN Lessons
ON Scores.LessonId=Lessons.LessonId
OUTER APPLY
(
EXECUTE dbo.GetTeacherId 0, 0, @teacherId=@teacherIdOut -- here I get error
SELECT Teachers.TeacherLastName, Teachers.TeacherFirstName, Teachers.TeacherMiddleName
FROM Teachers
WHERE Teachers.TeacherId=@teacherIdOut
)T(TLastName, TFirstName, TMiddleName)
WHERE Score <=3
还有其他方法可以从存储过程中获取值吗?这是我的存储过程dbo.GetTeacherId
:
USE [StudentsDb]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[GetTeacherId] @lessonId int, @groupId int, @teacherId int OUTPUT
AS
BEGIN
SET NOCOUNT ON;
SELECT @teacherId=GroupTeachers.TeacherId
FROM GroupTeachers
WHERE GroupTeachers.LessonId=@lessonId AND GroupTeachers.GroupId=@groupId
END