SQL ExecuteQuery 3061 错误消息

Posted

技术标签:

【中文标题】SQL ExecuteQuery 3061 错误消息【英文标题】:SQL ExecuteQuery 3061 error message 【发布时间】:2017-02-23 19:27:02 【问题描述】:

我有这个:

sql = "INSERT INTO instroom ( " & _
        "team_id, " & _
        "proces_id, " & _
        "datum, " & _
        "aantal_instroom, " & _
        "ctime, " & _
        "cuser, " & _
        "mtime, " & _
        "muser " & _
        ") "
    sql = sql & "SELECT " & _
        "team_id, " & _
        "proces_id, " & _
        "datum, " & _
        "SUM(aantal_instroom), " & _
        "#" & Format(MTime, "yyyy-mm-dd hh:mm:ss") & "#, " & _
        "" & mod_global.RealUserID & ", " & _
        "#" & Format(MTime, "yyyy-mm-dd hh:mm:ss") & "#, " & _
        "" & mod_global.RealUserID & " " & _
        "FROM tmp_import_instroom " & _
        "WHERE userid = '" & EscapeString(LCase(mod_global.RealUser)) & "' " & _
        "AND team_id <> 0 " & _
        "AND proces_id <> 0 "
    sql = sql & "GROUP BY team_id, proces_id, datum " & _
        "HAVING SUM(aantal_cases) > 0 "

当它通过时:

-- Execute Query
Private Function executeSQL(ByVal sql As String, Optional ByVal autoCommit As Boolean = False) As Boolean
On Error GoTo executeSQLError

executeSQL = False
If mod_global.DevStart Then QueryNum = QueryNum + 1

If mod_global.DevStart Then Call saveQueryToFile(sql)

' Check if database is open
If Not testConn Then
    Call openDB
End If

If startTrans Then
    db.Execute sql
    executeSQL = True

    If autoCommit Then
        executeSQL = commitDB
    End If
End If

DoEvents

Exit Function
executeSQLError:
Debug.Print ("executeSQL - " & Err.Number & " : " & Err.Description)
Call writeToLog("executeSQL - " & Err.Number & " : " & Err.Description)
End Function

我收到错误消息

“运行时错误 3061:参数太少。应为 1。”。

我错过了什么?我做了debug.print 仍然找不到问题。

这是来自 tmp_import_instroom 的列名:

debug.print的结果

insert into instroom (
   team_id
 , proces_id
 , datum
 , aantal_instroom
 , ctime
 , cuser
 , mtime
 , muser
 )
select 
   team_id
 , proces_id
 , datum
 , SUM(aantal_instroom)
 , #2017-02-23 20:22:33#
 , 310
 , #2017-02-23 20:22:33#
 , 310
from tmp_import_instroom
where userid = 'xg30222' 
  and team_id <> 0 
  and proces_id <> 0
group by team_id
 , proces_id
 , datum
having SUM(aantal_cases) > 0

【问题讨论】:

您是否检查过您的列名是否与表中的列名匹配? ***.com/questions/5445062/… 您确定 userid 是 tmp_import_instroom 表的一部分吗?它找不到您在 sql 中引用的变量之一,因此期望它作为参数传递。 我添加了一个屏幕截图,其中包含表格中的列名称 【参考方案1】:

除非它没有显示,否则您的表中没有 ctime、cuser、mtime 或 muser。因此,您需要为计算字段设置别名。

sql = sql & "SELECT " & _
    "team_id, " & _
    "proces_id, " & _
    "datum, " & _
    "SUM(aantal_instroom) as aantal_instroom, " & _
    "#" & Format(MTime, "yyyy-mm-dd hh:mm:ss") & "# as ctime, " & _
    "" & mod_global.RealUserID & " as cuser, " & _
    "#" & Format(MTime, "yyyy-mm-dd hh:mm:ss") & "# as mtime, " & _
    "" & mod_global.RealUserID & " as muser " & _
    "FROM tmp_import_instroom " & _
    "WHERE userid = '" & EscapeString(LCase(mod_global.RealUser)) & "' " & _
    "AND team_id <> 0 " & _
    "AND proces_id <> 0 "

【讨论】:

以上是关于SQL ExecuteQuery 3061 错误消息的主要内容,如果未能解决你的问题,请参考以下文章

无法使用 executeQuery 发出数据操作语句

运行时错误 3061 - 毫秒访问

运行时错误 3061 帮助(毫秒访问)

在 Database.Execute 中传递值返回错误 3061

没有结果时,executeQuery(String sql) 返回啥? [复制]

java.sql.SQLException:无法使用 executeQuery() 发出数据操作语句。 23