2009-11-10 85 views
3

爲什麼會收到一個語法錯誤,因爲這樣的:「作爲」不正確的語法「爲」關鍵字附近

我使用Microsoft Visual Studio 2005和SQL Server 2005

string strSql = 
"SELECT a.MCode, a.NameOfModule, a.Mod_Abbreviation, dt.ModuleCode, 
    dt.Course, dt.Stage, dt.ModuleGrpFrom, dt.ModuleGrpTo, dt.GrpName, 
    dt.GrpType, dt.StaffID, dt.AcadYear, dt.AcadSemester, 
    dt.TotalHour, dt.WeeklyLectHr, dt.WeeklyPractHr, dt.WeeklyTutHr, 
    dt.ModuleLeader, 0 AS TotalTeach, '' AS ModuleGroups, '' AS ML, 0 AS L, 0 AS P, 0 AS T, 1 AS NofGrp, '' AS TotalTeachUnit" + 
"FROM (SELECT * FROM 
    (SELECT a.ModuleCode, a.Course, a.Stage, a.ModuleGrpFrom, 
    a.ModuleGrpTo, a.GrpName, a.GrpType, a.StaffID, b.AcadYear, 
    b.AcadSemester, b.TotalHour, b.WeeklyLectHr, b.WeeklyPractHr, 
    b.WeeklyTutHr, b.ModuleLeader 
    FROM ModuleStrGrp a 
    LEFT JOIN ModuleStr b ON a.AcadYear = b.AcadYear 
    AND a.AcadSemester = b.AcadSemester 
    AND a.Course = b.Course 
    AND a.ModuleCode = b.ModuleCode) AS MSG 
    INNER JOIN Parameter Para ON MSG.Course = Para.Paracode2) AS dt 
    LEFT JOIN Module a ON dt.ModuleCode = a.MCode" + 
"WHERE dt.AcadYear LIKE AcadYear AND dt.AcadSemester LIKE AcadSemester 
    AND dt.Course LIKE Course AND dt.Stage LIKE Stage AND dt.StaffID LIKE Staff 
    AND dt.SpecGrp LIKE Specialisation" + 
"ORDER BY dt.ModuleCode, dt.GrpType"; 
+0

你能成爲一個更具體一點?你是否嘗試過測試每個作業,或者我們必須爲你做這件事嗎? – 2009-11-10 07:48:43

回答

10
關鍵字附近有語法錯誤

缺少空格。
您的代碼創建的字符串

"... , '' AS TotalTeachUnitFROM (SELECT * ..." 

同去的其他線路,他們需要之間的空格。

4

使"FROM ...作爲" FROM ...

現在它是... AS TotalTeachUnitFROM ...

6

我不會回答你的問題,但我給你的策略:

的STRSQL字符串的內容複製到剪貼板,然後到SQL Management Studio中的查詢,然後在那裏運行它。

然後它會告訴你哪個「AS」導致了問題。很可能它是一個缺失的空間或appostraphe或逗號。

4

您缺少空格。

"...a ON dt.ModuleCode = a.MCode" + 
"FROM (SELECT * FROM ..." 

結果

"...a ON dt.ModuleCode = a.MCodeFROM (SELECT * FROM ..." 
           ^
          space missing 
4

而是用@代替+

string strSql = @"SELECT a.MCode, a.NameOfModule, a.Mod_Abbreviation, dt.ModuleCode, dt.Course, dt.Stage, dt.ModuleGrpFrom, dt.ModuleGrpTo, dt.GrpName, dt.GrpType, dt.StaffID, dt.AcadYear, dt.AcadSemester, dt.TotalHour, dt.WeeklyLectHr, dt.WeeklyPractHr, dt.WeeklyTutHr, dt.ModuleLeader, 0 AS TotalTeach, '' AS ModuleGroups, '' AS ML, 0 AS L, 0 AS P, 0 AS T, 1 AS NofGrp, '' AS TotalTeachUnit 
FROM (SELECT * FROM (SELECT a.ModuleCode, a.Course, a.Stage, a.ModuleGrpFrom, a.ModuleGrpTo, a.GrpName, a.GrpType, a.StaffID, b.AcadYear, b.AcadSemester, b.TotalHour, b.WeeklyLectHr, b.WeeklyPractHr, b.WeeklyTutHr, b.ModuleLeader FROM ModuleStrGrp a LEFT JOIN ModuleStr b ON a.AcadYear = b.AcadYear AND a.AcadSemester = b.AcadSemester AND a.Course = b.Course AND a.ModuleCode = b.ModuleCode) AS MSG INNER JOIN Parameter Para ON MSG.Course = Para.Paracode2) AS dt LEFT JOIN Module a ON dt.ModuleCode = a.MCode 
WHERE dt.AcadYear LIKE AcadYear AND dt.AcadSemester LIKE AcadSemester AND dt.Course LIKE Course AND dt.Stage LIKE Stage AND dt.StaffID LIKE Staff AND dt.SpecGrp LIKE Specialisation 
ORDER BY dt.ModuleCode, dt.GrpType"; 
相關問題