Results 1 to 6 of 6

Thread: syntax error in sql statement...

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2003
    Posts
    436

    syntax error in sql statement...

    I am writing a "insert" sql statement.

    and I get exception as "Syntax error (missing operator) in query expression '6/20/2006 9:30:45 AM'."

    The following is the sql statement:

    string ssql = "INSERT INTO AT_ARC_ADVANCES(AT_ARC_DATA_DATE,AT_ARC_MATURITY) VALUES ";

    ssql = ssql + "("+ DateTime.Now + ','+ "2 Years" + ")";

    try
    {
    accessCommand.CommandText = ssql;
    accessConnection.Open();
    accessCommand.ExecuteNonQuery();
    }
    catch (Exception ex)
    {

    Console.WriteLine(ex.Message);
    }

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: syntax error in sql statement...

    Try
    Code:
    string ssql = "INSERT INTO AT_ARC_ADVANCES (AT_ARC_DATA_DATE,AT_ARC_MATURITY) VALUES ";
    ssql = ssql & "('" & DateTime.Now & "','" & "'2 Years'" & "')";

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2003
    Posts
    436

    Re: syntax error in sql statement...

    Nope. Didn't work. the error is "Syntax error (missing operator) in query expression '''2 Years'''."

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: syntax error in sql statement...

    I think Hack put in too many single quote characters

    try this:
    Code:
    ssql = ssql & "('" & DateTime.Now & "','2 Years')";

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: syntax error in sql statement...

    Quote Originally Posted by si_the_geek
    I think Hack put in too many single quote characters

    try this:
    Code:
    ssql = ssql & "('" & DateTime.Now & "','2 Years')";
    Oops. I think you are right.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: syntax error in sql statement...

    And here's just one more example of why you should never build SQL statements using string concatenation. If you ever want to include a variable value in an SQL statement then you should always use a parameter:
    Code:
    string ssql = "INSERT INTO AT_ARC_ADVANCES(AT_ARC_DATA_DATE, AT_ARC_MATURITY) VALUES (@AT_ARC_DATA_DATE, '2 Years')";
    accessCommand.CommandText = ssql;
    accessCommand.Parameters.AddWithValue("@AT_ARC_DATA_DATE", DateTime.Now);
    Having said that, I'm guessing you may actually want DateTime.Today, which has the time component zeroed, rather than DateTime.Now. Also, there is the GETDATE function in T-SQL that gets the current date so you could just use that in your SQL code instead of inserting a value from your VB code, assuming that it is supported by Access, which I think is the database you're using.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width