关键字'AS'附近的语法不正确

时间:2009-11-10 07:46:30

标签: sql-server-2005 visual-studio-2005 syntax

为什么我会收到语法错误:关键字“AS”附近的语法不正确

我正在使用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";

5 个答案:

答案 0 :(得分:10)

缺少空格。
您的代码正在创建字符串

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

对于其他行也一样,它们之间需要空格。

答案 1 :(得分:6)

我不会回答你的问题,但我会给你一个策略:

将strSql字符串的内容复制到剪贴板,然后复制到SQL Management Studio中的查询,然后在那里运行。

然后它实际上会告诉你哪个“AS”导致问题。很可能是缺少空间或appostraphe或逗号。

答案 2 :(得分:4)

"FROM ...设为" FROM ...

现在是... AS TotalTeachUnitFROM ...

答案 3 :(得分:4)

你缺少空格。

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

结果

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

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