简体   繁体   English

INSERT INTO ... SELECT语句(Access SQL)上的语法错误

[英]Syntax error on INSERT INTO … SELECT statement (Access SQL)

I am trying to insert a field ' SID ' Value into tbl1 , But to get the SID It must reference the forename and surname to another table ( tbl2 ) to get the SID , I then have used the following SQL Statement and subsequent code. 我试图将字段' SID '值插入到tbl1中 ,但要获取SID它必须将forename和surname引用到另一个表( 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()

Yet, the SQLQuery is giving the error : 然而,SQLQuery给出了错误:

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll Additional information: Syntax error in query expression 'SELECT StudentID FROM tblStudents WHERE Forename = @Forename AND Surname = @Surname'. System.Data.dll中出现未处理的“System.Data.OleDb.OleDbException”类型异常附加信息:查询表达式中的语法错误'SELECT StudentID FROM tblStudents WHERE Forename = @Forename AND Surname = @Surname'。

But I can not see what is wrong with the part of the statement that is specified as wrong, can someone tell me where its wrong please? 但是我看不出指定为错误的声明部分有什么问题,有人可以告诉我它的错误吗?

I think the error is very evident. 我认为错误非常明显。 The syntax for insert . . . select insert . . . select的语法insert . . . select insert . . . select insert . . . select does not use the values keyword: insert . . . select不使用values关键字:

INSERT INTO tblPayments(StudentID, Payment, PaymentDate) 
    SELECT StudentID,  @SPaid, @todaysdate
    FROM tblStudents
    WHERE Forename = @Forename AND Surname = @Surname;

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

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