我正在尝试将字段“ SID ”值插入 tbl1 ,但要获取 SID 它必须引用名字和姓氏到另一个表( tbl2 )以获取 SID ,然后我使用了以下SQL语句和后续代码。
Dim sqlquery As String = "INSERT INTO tblPayments (StudentID,Payment,PaymentDate) VALUES (SELECT StudentID FROM tblStudents WHERE Forename = @Forename AND Surname = @Surname , Payment = @SPaid, PaymentDate = @todaysdate)"
Dim sqlcommand As New OleDbCommand
With sqlcommand
.CommandText = sqlquery
.Parameters.AddWithValue("@SPaid", Paidtxt.Text)
.Parameters.AddWithValue("@todaysdate", Today.Date)
.Parameters.AddWithValue("@Forename", Forenametxt.Text)
.Parameters.AddWithValue("@Surname", Surnametxt.Text)
.Connection = conn
.ExecuteNonQuery()
End With
MsgBox("query executed, closing connection")
conn.Close()
然而,SQLQuery给出了错误:
“System.Data.OleDb.OleDbException”类型的未处理异常 发生在System.Data.dll中 附加信息:查询表达式中的语法错误'SELECT StudentID FROM tblStudents WHERE Forename = @Forename AND Surname = @Surname”。
但是我看不出指定为错误的声明部分有什么问题,有人可以告诉我它的错误吗?
答案 0 :(得分:3)
我认为错误非常明显。 insert . . . select
的语法不使用values
关键字:
INSERT INTO tblPayments(StudentID, Payment, PaymentDate)
SELECT StudentID, @SPaid, @todaysdate
FROM tblStudents
WHERE Forename = @Forename AND Surname = @Surname;