0

我不断收到有关此特定语句的语法错误。

SELECT tbl1.ProjectID, tbl1.EntryDate AS StartDate, tbl2.EntryDate AS EndDate, 
(tbl3.ChecklistDayMax - tbl3.ChecklistDayMin + 1) AS DaysAllotted, 
(SELECT ProjectPriority FROM project_master WHERE ProjectID = tbl1.ProjectID) AS Priority,
tbl3.MilestoneName,
IIF(Priority = 1, tbl3.BonusDaysFH, 
IIF(Priority = 2, tbl3.BonusDaysFM, 
IIF(Priority = 3, tbl3.BonusDaysFL))) AS BonusDaysAllotted
FROM (((checklist_entries AS tbl1
INNER JOIN checklist_entries AS tbl2 ON tbl1.ProjectID = tbl2.ProjectID)
INNER JOIN milestone_def AS tbl3 ON [@milestoneID] = milestone_def.MilestoneDefID)
INNER JOIN project_active_status AS tbl4 ON tbl1.ProjectID = project_active_status.ProjectID)
WHERE tbl1.ChecklistDay = tbl3.ChecklistDayMin
AND tbl2.ChecklistDay = tbl3.ChecklistDayMax
AND tbl4.ProjectIsOpen = FALSE;

该错误显示连接操作中的语法错误,然后在第二次 INNER JOIN 之后突出显示里程碑定义。有趣的是,如果我切换这条线......

INNER JOIN milestone_def AS tbl3 ON [@milestoneID] = milestone_def.MilestoneDefID)

用这条线...

INNER JOIN milestone_def AS tbl3 ON [@milestoneID] = tbl3.MilestoneDefID)

我收到错误Join Expression Not Supported然后它突出显示...

[@milestoneID] = tbl3.MilestoneDefID)

但正如你所看到的,在第一次加入...

INNER JOIN checklist_entries AS tbl2 ON tbl1.ProjectID = tbl2.ProjectID

我将其命名为 tbl2,然后使用 tbl2.ProjectID 并且表达式工作得很好。最终,我需要让它发挥作用,不管我如何命名这些东西。

[@milestoneID] 是传递给查询的参数以匹配milestone_def.MilestoneDefID

4

3 回答 3

1

由于问题出在连接上,因此明智的做法是使用更简单的查询来调查问题。

SELECT *
FROM
    ((checklist_entries AS tbl1
    INNER JOIN checklist_entries AS tbl2
    ON tbl1.ProjectID = tbl2.ProjectID)
    INNER JOIN milestone_def AS tbl3
    ON [@milestoneID] = milestone_def.MilestoneDefID)
    INNER JOIN project_active_status AS tbl4
    ON tbl1.ProjectID = project_active_status.ProjectID

请注意,您已为表名起别名。ON因此,您必须使用这些别名而不是子句 中的表名。

SELECT *
FROM
    ((checklist_entries AS tbl1
    INNER JOIN checklist_entries AS tbl2
    ON tbl1.ProjectID = tbl2.ProjectID)
    INNER JOIN milestone_def AS tbl3
    ON tbl1.[@milestoneID] = tbl3.MilestoneDefID)
    INNER JOIN project_active_status AS tbl4
    ON tbl1.ProjectID = tbl4.ProjectID

我不知道它是什么,也不知道[@milestoneID]它来自哪里。我最好的猜测是它是 中的一个字段checklist_entries,所以我用tbl1别名对其进行了限定。

于 2013-07-02T16:15:27.350 回答
1

[从评论扩展。]这只是一种预感,因为我无权访问 Access(哈哈),但您的查询当前指定了一个INNER JOIN实际上并没有将表与查询的其余部分相关联:

... 
INNER JOIN milestone_def AS tbl3
ON [@milestoneID] = milestone_def.MilestoneDefID
...

ON子句仅引用一个外部变量,因此与操作无关JOIN,使其有效地CROSS JOIN具有单独的WHERE条件:

... 
CROSS JOIN milestone_def AS tbl3
...
WHERE [@milestoneID] = milestone_def.MilestoneDefID
...

查看查询的底部,您在WHERE子句中有此表的实际连接条件;这些应该被交换到ON子句中,以便它实际指定INNER JOIN条件:

... 
INNER JOIN milestone_def AS tbl3
ON tbl1.ChecklistDay = tbl3.ChecklistDayMin
AND tbl2.ChecklistDay = tbl3.ChecklistDayMax
...
WHERE [@milestoneID] = milestone_def.MilestoneDefID
...

这样当然更合乎逻辑,它可能会解决 Access 的解析器理解您的查询的问题。

于 2013-07-02T16:31:39.610 回答
-1

我认为 HansUp 的回答为您指明了正确的方向。我注意到的另一件事是,您有一系列三个嵌套的 IIF,最后一个只有一个测试和一个 true,但缺少 false 参数。我认为这三个都是强制性的。

于 2013-07-02T16:49:39.750 回答