2013-01-20 34 views
1

爲什麼從下面的代碼中得到錯誤「關鍵字'AS'附近的語法錯誤?分配結果時,「AS @variable」附近的語法錯誤?

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

這不是你如何分配給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 
+0

完美的感謝,你不會相信我有多長時間的代碼,有點搞亂奇怪,爲什麼這是行不通的。 – gallie

+0

@gallie - 出於好奇,你用什麼理由接受答案? –

+0

我的代碼現在按我希望的方式工作。這不是唯一的理由嗎? :) – gallie