【发布时间】:2023-03-14 16:00:01
【问题描述】:
我在访问 vba 中编写了以下 SQL 字符串,但使用
访问失败Error 3075 语法错误缺少操作符
我使用 debug.print 捕获 SQL,然后在 SSMS 中运行它,然后我得到“操作数类型冲突:日期与 int 不兼容”。
我使用Allen Brown's SQLDate function 尝试确保我的日期格式正确,但这不起作用。
如果我将日期括在单引号中,它会在 SSMS 中正确执行。我在字符串中添加了单引号(SQLDate 的每个用法的任一侧),但它仍然失败。我不知道如何编辑我的字符串,以便以正确的格式传递日期。
StartDate 和 EndDate 的数据类型是日期。
strSQLDeps = "UPDATE dbo_Production_Schedule_1 " & _
" SET dbo_Production_Schedule_1.StartDate = " & SQLDate(!EndDate) & _
" FROM (dbo_Production_Schedule INNER JOIN dbo_Production_Sched_Dependencies " & _
" ON dbo_Production_Schedule.ProductionScheduleID = dbo_Production_Sched_Dependencies.ProdSchedID)" & _
" INNER JOIN dbo_Production_Schedule AS dbo_Production_Schedule_1" & _
" ON (dbo_Production_Sched_Dependencies.DependentActivityID = dbo_Production_Schedule_1.ActivityID)" & _
" AND (dbo_Production_Schedule.ItemSerialNo = dbo_Production_Schedule_1.ItemSerialNo)" & _
" WHERE dbo_Production_Schedule_1.StartDate < " & SQLDate(!EndDate) & _
" AND dbo_Production_Schedule.ProductionScheduleID = " & !ProductionScheduleID & ";"
这是 debug.print -
UPDATE dbo_Production_Schedule_1
SET dbo_Production_Schedule_1.StartDate = #02/12/2018#
FROM (dbo_Production_Schedule INNER JOIN dbo_Production_Sched_Dependencies
ON dbo_Production_Schedule.ProductionScheduleID = dbo_Production_Sched_Dependencies.ProdSchedID)
INNER JOIN dbo_Production_Schedule AS dbo_Production_Schedule_1
ON (dbo_Production_Sched_Dependencies.DependentActivityID = dbo_Production_Schedule_1.ActivityID)
AND (dbo_Production_Schedule.ItemSerialNo = dbo_Production_Schedule_1.ItemSerialNo)
WHERE dbo_Production_Schedule_1.StartDate < #02/12/2018#
AND dbo_Production_Schedule.ProductionScheduleID = 5923;
【问题讨论】:
-
您应该使用参数化查询或存储过程,而不是构建字符串并执行。那么你就不必为此抗争了。
-
谢谢肖恩 - 我会尝试参数化查询方法 - 我已经有一个选择查询,但没有想过要创建它的更新版本。我对 access 和 sql 都很陌生,所以我每天都在学习!
-
感谢@SeanLange - 参数化查询运行良好。
-
很高兴你能成功。最大的好处是您找到了一种更好的查询方法,不仅可以避免像打架那样的麻烦,还可以保护您免受 sql 注入。干得好!!
-
您始终可以使用 ISO 格式,例如 "'" & Format(!endDate, "yyyy-mm-dd") & "'"
标签: sql-server vba