Trying to insert into access database via SQL gett

2019-07-31 19:44发布

问题:

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";