none
Syntax error in query. Incomplete query clause.

    Question

  • Hello.

    I am using an Access .MDB file. I need to get the Name of an entry based on its ID.

    My SQL code is as follows:

    OleDbCommand cmdGetName = new OleDbCommand("SELECT Name FROM @pTable WHERE GroupKey = @pContactID", dbConnect);
    cmdGetName.Parameters.Add("@pTable", OleDbType.VarChar);
    cmdGetName.Parameters.Add("@pContactID", OleDbType.Integer);
    cmdGetName.Parameters[0].Value = "Group";  //string 
    cmdGetName.Parameters[1].Value = ID;       //int value
    String Name = (String)cmdGetName.ExecuteScalar();

    The problem is the ExecueScalar() returns an exception error

    "Syntax error in query.  Incomplete query clause."

    It seems that using an integer and a string parameter has a difference.

    How can I correct the SQL command?

    Thanks.

    Wednesday, March 07, 2012 3:59 AM

Answers

  • Try:

    OleDbCommand cmdGetName = new OleDbCommand("SELECT Name FROM Group WHERE GroupKey = @pContactID", dbConnect);
    
    cmdGetName.Parameters.Add("@pContactID", OleDbType.Integer);
    cmdGetName.Parameters[0].Value = ID;       //int value
    String Name = (String)cmdGetName.ExecuteScalar();


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog

    • Marked as answer by KJian_ Tuesday, March 13, 2012 3:27 AM
    Wednesday, March 07, 2012 4:31 AM

All replies

  • Hello,

    You can't use a parameter for the table name. Replace the parameter @pTable with the real table name and try it again; then it will work.

    If you want to query different table, you have to build the SQL statement dynamically.


    Olaf Helper
    * cogito ergo sum * errare humanum est * quote erat demonstrandum *
    Wenn ich denke, ist das ein Fehler und das beweise ich täglich
    Blog Xing


    • Edited by Olaf HelperMVP Wednesday, March 07, 2012 4:03 AM
    • Proposed as answer by Naomi N Wednesday, March 07, 2012 4:27 AM
    Wednesday, March 07, 2012 4:03 AM
  • Thanks for the response.

    I'm using another SQL command which is similar to this, except that instead of using an integer "ID" for the search criteria, all the parameters are strings.

    The only difference here is that the ID is an integer, not a string.

    Wednesday, March 07, 2012 4:27 AM
  • Try:

    OleDbCommand cmdGetName = new OleDbCommand("SELECT Name FROM Group WHERE GroupKey = @pContactID", dbConnect);
    
    cmdGetName.Parameters.Add("@pContactID", OleDbType.Integer);
    cmdGetName.Parameters[0].Value = ID;       //int value
    String Name = (String)cmdGetName.ExecuteScalar();


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog

    • Marked as answer by KJian_ Tuesday, March 13, 2012 3:27 AM
    Wednesday, March 07, 2012 4:31 AM