1

我有一个在运行 SQL Server CE 的 Windows Mobile 上运行的现有应用程序,并且正在使用 C# 语言的内联查询。这是我在 Management Studio 中正常工作的查询:

command = @"SELECT  
                P_InspectionStep.InspectionStepID,
                P_InspectionStep.Sequence AS Sequence,
                P_InspectionStep.Title AS Title,
                P_InspectionStep.Body AS Body,
                P_InspectionStep.Photo AS Photo,
                P_Inspection.RevisionID
            FROM
                P_InspectionStep
            LEFT JOIN 
                P_Inspection ON P_Inspection.InspectionID = P_InspectionStep.InspectionID
            WHERE 
                P_Inspection.InspectionID = '" + mInspectionID + "'" +
                @"AND P_Inspection.CreateDate = (SELECT MAX(P_Inspection.CreateDate) FROM P_Inspection)

            UNION ALL

            SELECT
                C_InspectionStep.InspectionStepID,
                C_InspectionStep.Sequence AS Sequence,
                C_InspectionStep.Title AS Title,
                C_InspectionStep.Body AS Body,
                C_InspectionStep.Photo AS Photo,
                C_Inspection.RevisionID
            FROM 
                C_InspectionStep
            LEFT JOIN 
                C_Inspection ON C_Inspection.RevisionID = C_InspectionStep.RevisionID
            WHERE 
                C_Inspection.InspectionID = '" + mInspectionID + "'" +
                @"AND C_Inspection.CreateDate = (SELECT MAX(C_Inspection.CreateDate) FROM C_Inspection)
            ORDER BY 2;";

但是在应用程序中运行它会返回SqlCeException

解析查询时出错。[令牌行号= 10,令牌行偏移量= 132,错误令牌= SELECT]

我已经做了很多搜索,但无法找到如何解决这个问题。

4

2 回答 2

0

提供价值

(SELECT MAX(P_Inspection.CreateDate) FROM P_Inspection)

从另一个查询提前运行。

于 2013-07-02T12:34:53.750 回答
0

你不能有一个返回标量值的子查询,但你可以返回一个集合,所以使用IN而不是相等测试:

... C_Inspection.CreateDate IN (SELECT MAX(C_Inspection.CreateDate) FROM C_Inspection)
于 2013-07-02T12:56:47.883 回答