'SELECT'附近和'AS'附近的dbo.StoredProcedure SQL语法错误

时间:2012-08-06 22:07:13

标签: asp.net sql syntax

嘿伙计们,我希望你能帮助我,我已经尝试了所有我能想到的东西,它一直告诉我SELECT附近的语法和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;

1 个答案:

答案 0 :(得分: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;