Trying to insert into access database via SQL getting ID from another table

时光毁灭记忆、已成空白 提交于 2019-12-11 05:47:20

问题


I have 2 tables in an Access database:

  • tblMachine
  • fields:
  • MachineID
  • MachineDescription

  • tblProblem

  • fields
  • ProblemID
  • MachineID
  • ProblemDescription

I am trying to add a new record into tblProblem, using the MachineDescription to find the MachineID from tblMachine

However, my SQL statement throws an error on the sub-select statement

Here is my statement:

string sql = "INSERT INTO tblProblem" +
                " ([MachineID], [ProblemDescription], [ProblemOrder])" +
                " VALUES (" + 
                "(SELECT ([MachineID] FROM tblMachine WHERE MachineDescription = @MachineDescription))," +
                " @ProblemDescription, @ProblemOrder);";

The problem being highlighted is this part:

"(SELECT ([MachineID] FROM tblMachine WHERE MachineDescription = @MachineDescription)),"

Have I done something wrong? It is telling me there is a syntax error...


回答1:


Extra set of parentheses, and perhaps unqualified field names:

String sql = "INSERT INTO tblProblem " + 
    "([MachineID], [ProblemDescription], [ProblemOrder])" +
    "SELECT tblMachine.[MachineID], @ProblemDescription, @ProblemOrder " +
    "FROM tblMachine " +
    "WHERE tblMachine.MachineDescription = @MachineDescription";


来源:https://stackoverflow.com/questions/17196689/trying-to-insert-into-access-database-via-sql-getting-id-from-another-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!