分配结果时,“.. AS @variable”附近的语法错误?

时间:2013-01-20 21:21:59

标签: sql sql-server tsql

为什么我从以下代码中得到错误“关键字'AS'附近的语法不正确?”

DECLARE @FirstHole INTEGER

SELECT MIN(HoleId) FROM dbo.Holes WHERE CourseId = (
SELECT MAX(dbo.Holes.CourseId) FROM dbo.Registration 
LEFT JOIN dbo.Competitions ON dbo.Registration.CompetitionId = dbo.Competitions.CompetitionId 
LEFT JOIN dbo.Holes ON dbo.Competitions.CourseId = dbo.Holes.CourseId WHERE RegistrationId = 102
) AND dbo.Holes.MarkerId = 2 AS @FirstHole

1 个答案:

答案 0 :(得分:1)

这不是您在TSQL中分配给变量的方式。

我认为你打算这样做

DECLARE @FirstHole INTEGER

SELECT  @FirstHole = MIN(HoleId) /* <-- Assignment here*/
FROM   dbo.Holes
WHERE  CourseId = (SELECT MAX(dbo.Holes.CourseId)
                   FROM   dbo.Registration
                          LEFT JOIN dbo.Competitions
                            ON dbo.Registration.CompetitionId = dbo.Competitions.CompetitionId
                          LEFT JOIN dbo.Holes
                            ON dbo.Competitions.CourseId = dbo.Holes.CourseId
                   WHERE  RegistrationId = 102)
       AND dbo.Holes.MarkerId = 2