2012-08-06 87 views
0

嘿,我希望你能幫助我我已經嘗試了所有我能想到的事情,並一直告訴我,我的語法接近SELECT,而且我的語法附近AS是不正確dbo.StoredProcedure'SELECT'附近的SQL語法錯誤'AS'附近

CREATE PROCEDURE dbo.StoredProcedure2 
SELECT 
, Announcements.ID 
, Announcement.CreateDate 
, Announcements.Announcement 
,aspnet_Users.UserName 
,(SELECT Announcement_Read_State.Read_Date 
    FROM Announcement_Read_State 
    WHERE Announcement_Read_State.Announcement_ID = Announcements.ID 
    AND Announcement_Read_State.User_ID = 2) AS ReadState 
FROM Announcements INNER JOIN aspnet_User ON Announcements .Sender_User_ID = aspnet_User.UserName 
WHERE (Announcements.ID IN 

     (SELECT Max(Announcements.ID) 
     FROM Thread_Participant INNER JOIN Announcements ON 
     Thread_Participant.ThreadId = Announcements.Announcement_ThreadId 
     WHERE MessageThreadParticipant.UserID = 2 
     GROUP BY ThreadParticipant.AnnouncementThreadId 
     ) 

ORDER BY Message.CreateDate DESC; 

回答

5

它應該是:

CREATE PROCEDURE dbo.StoredProcedure2 
AS -- you were missing this 

SELECT -- you had an extra comma here 
    Announcements.ID 
, Announcement.CreateDate 
, Announcements.Announcement 
,aspnet_Users.UserName 
,(SELECT Announcement_Read_State.Read_Date 
    FROM Announcement_Read_State 
    WHERE Announcement_Read_State.Announcement_ID = Announcements.ID 
    AND Announcement_Read_State.User_ID = 2) AS ReadState 
FROM Announcements INNER JOIN aspnet_User ON Announcements .Sender_User_ID = aspnet_User.UserName 
WHERE (Announcements.ID IN 

     (SELECT Max(Announcements.ID) 
     FROM Thread_Participant INNER JOIN Announcements ON 
     Thread_Participant.ThreadId = Announcements.Announcement_ThreadId 
     WHERE MessageThreadParticipant.UserID = 2 
     GROUP BY ThreadParticipant.AnnouncementThreadId 
     ) 
)-- you were missing this one 

ORDER BY Message.CreateDate DESC;