VBA - INNER加入更新SQL

时间:2018-01-22 23:01:06

标签: sql-server access-vba

我的应用程序目前有以下SQL代码,用于标记调查,如果已发送电子邮件,然后使用日期字段标记作业。

CurrentProject.Connection.Execute "UPDATE J SET J.PrewarnFranDate = DATE() FROM (dbo_Franchises AS F INNER JOIN dbo_Customers AS C ON F.FranchiseNo=C.FranchiseNo) INNER JOIN dbo_Jobs AS J ON C.CustomerId=J.CustomerId WHERE (F.SortOrder<98) AND (C.NeverSurveyCust=0) AND (J.NotProceedingDate IS NULL) AND (J.HandoverDate < " & Date() - 28 & ") AND (J.SendFlag = 0) AND (J.SurveyId IS NULL);"

目前,当代码运行时,它会产生以下错误,我不能在生命中找出原因,因为语法看起来是正确的。

screenshot of syntax error

3 个答案:

答案 0 :(得分:0)

先试试这个。

UPDATE J
SET PrewarnFranDate = DATE()
FROM dbo_Franchises AS F
INNER JOIN dbo_Customers AS C
  ON F.FranchiseNo = C.FranchiseNo
INNER JOIN dbo_Jobs AS J
  ON C.CustomerId = J.CustomerId
WHERE F.SortOrder < 98
AND C.NeverSurveyCust = 0
AND J.NotProceedingDate IS NULL
AND J.HandoverDate < " & Date() - 28 & "
AND J.SendFlag = 0
AND J.SurveyId IS NULL

答案 1 :(得分:0)

以同样的方式将第二个date()函数放在查询字符串之外,您必须对第一个date()函数执行相同的操作。

CurrentProject.Connection.Execute "UPDATE J SET J.PrewarnFranDate =#" & DATE() & "# FROM...

您可能还需要将第二个日期函数括在#个符号中。

"AND (J.HandoverDate < #" & Date() - 28 & "#) AND... 

答案 2 :(得分:0)

感谢您的建议,但我的问题最终变得非常愚蠢。最初的问题是访问以有趣的方式处理多个内部联接的更新。我不得不重新格式化查询,如下所示。

"UPDATE (dbo_jobs AS J Inner Join DBO_Customers as C on C.customerid = j.customerid) INNER JOIN dbo_franchises as F on F.FranchiseNO = C.franchiseno set j.prewarnfrandate =# " & Date & "# where F.sortorder < 98 and c.neversurveycust = 0  and j.notproceedingdate is null and J.handoverdate < #" & Date - 28 & "# and j.sendflag = 0 and j.surveyid is null;"