繁体   English   中英

MS Access查询帮助INSERT

[英]MS Access query assistance INSERT

这个查询一直告诉我在SQL语句结束时我缺少一个分号,但是当我添加它时,它告诉我在SQL语句的末尾找到了一个字符,这里有什么问题? 我习惯使用SQL Server,所以这对我来说简直太困惑了。 我不确定为什么Access需要使用“;” 关闭查询。 我只是不确定在哪里加入它。

strSQL = "Insert Into [tempCaseMgmt]Values([planID],[EdId],[OrID],[TheDate],  [TypeID],[UserID],[TimeStart],[TimeEnd],[Unitsled],[Unitsid],[ClientID],  [GenderID])" _
 & " Select * from [dbo_tempDetail] where [userid]= " & [Forms]![frmx]! [txtClientID] & ";"

我只是想让这项工作成功。 我选择和插入的值是相同的。

正如@Martin在其回答的一条评论中所建议的那样,你正在混合两种形式的INSERT INTO ,具体来说,

INSERT INTO ... VALUES ...

INSERT INTO ... SELECT ...

在我自己的简化示例中,这失败了“运行时错误'3137':在SQL语句结束时缺少分号(;)。”

Dim strSQL As String
strSQL = "Insert Into [tempCaseMgmt]Values([planID],[UserID])" _
        & " Select * from [dbo_tempDetail] where [userid]=1" & ";"
Dim cdb As DAO.Database
Set cdb = CurrentDb
cdb.Execute strSQL, dbFailOnError

这有效

Dim strSQL As String
strSQL = "Insert Into [tempCaseMgmt] ([planID],[UserID])" _
        & " Select * from [dbo_tempDetail] where [userid]=1" & ";"
Dim cdb As DAO.Database
Set cdb = CurrentDb
cdb.Execute strSQL, dbFailOnError

请注意, Values关键字已被省略。

如果不深入研究,我会说,您在select语句前面缺少一个空格。

更新:

您错过了“Values”关键字前面的第二个空格。 您是否复制粘贴此查询,或者您刚刚将其写入?

我会说,您尝试使用Insert Into Statement的混合语句语法。 值用于单个记录追加。 这意味着你应该在右括号后面加一个分号。 对于解释器,Select是一个全新的Statement。 我说那不是你想要的。

使用多记录语法插入:

"Insert Into [tempCaseMgmt] \n
Select * from [dbo_tempDetail] where [userid]= " & [Forms]![frmx]![txtClientID] & ";"

在这种情况下,列命名应该与Martin相同

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM