Results 1 to 16 of 16

Thread: SQL statement syntax error

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    366

    SQL statement syntax error

    Hi Guys, just trying to save some information to excel, have set up the excel worksheet and Im trying to write to it using:
    Code:
    Dim conn1 As New System.Data.OleDb.OleDbConnection(m_sConn1)
            conn1.Open()
            Dim cmd As New System.Data.OleDb.OleDbCommand()
            cmd.Connection = conn1
            cmd.CommandText = "INSERT INTO [Failed$] (OrderTime, Phone, MessageDetails) values ('" & Order_date & "','" & incomingtextno & "','" & textmsg & "')"
            cmd.ExecuteNonQuery()
            conn1.Close()
    However I get
    Syntax error (missing operator) in query expression ''Hi Guys ')'.
    Where Hi Guys is the contents of textmsg.
    I think i understand the problem is because it thinks that the bracket is part of the expression or something? what have i done wrong in the syntax?
    thanks

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: SQL statement syntax error

    Show us the actual query string (with the values in place - not the variable names). You can put a breakpoint on the line cmd.ExecuteNonQuery and when you debug run your app, copy the value of cmd.CommandText in the watch window and paste it here (or you can do debug.print(cmd.CommandText) then copy that)
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    366

    Re: SQL statement syntax error

    ye here it is:
    INSERT INTO [Failed$] (OrderTime, Phone, MessageDetails) values ('07/09/2011 18:28:05','+4477256*****','Hey guys ')
    I *'d out my number...

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    366

    Re: SQL statement syntax error

    could it be that the string has spaces or anything?

  5. #5
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: SQL statement syntax error

    Try using parameters as shown below

    Code:
    Public ExcelProvider As String = "Microsoft.Jet.OLEDB.4.0"
    Public ExcelVersion As String = "Excel 8.0"
    Public Sub InsertNewRow(ByVal FileName As String, ByVal SheetName As String)
        Dim cn As New OleDbConnection With _
        { _
           .ConnectionString = _
              String.Format("provider={1};Data Source={0};Extended Properties=""{2}; HDR=Yes;""", _
                            FileName, ExcelProvider, ExcelVersion) _
        }
    
        Dim cmd = New OleDbCommand() With {.Connection = cn}
        cmd.CommandText = String.Format("INSERT INTO [{0}$] (OrderTime, Phone, MessageDetails) VALUES (@OrderTime, @Phone, @MessageDetails)", SheetName)
        cmd.Parameters.AddWithValue("@OrderTime", "07/09/2011 18:28:05")
        cmd.Parameters.AddWithValue("@Phone", "+4477256")
        cmd.Parameters.AddWithValue("@MessageDetails", "Hey guys")
    
        cn.Open()
    
        Try
            cmd.ExecuteNonQuery()
        Catch OleDbExceptionErr As OleDbException
            MessageBox.Show(OleDbExceptionErr.Message)
        End Try
    End Sub

  6. #6
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: SQL statement syntax error

    The issue is the extra single quotes in the string. When the SQL parser runs into them it things that is the end of the value. But there is more so there is a SQL error. There are two ways to fix. The first (not the perfered) is to use a replace function like this:

    Code:
    cmd.CommandText = "INSERT INTO [Failed$] (OrderTime, Phone, MessageDetails) values ('" & Order_date & "','" & incomingtextno & "','" & textmsg.Replace("'","''") & "')"
    The second way and the perfered is to use parameters in the command object.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    366

    Re: SQL statement syntax error

    Sorry I dont see why the textmsg part is any different to the Orderdate part or the incomingtextno part. why is the textmsg part causing a problem?

  8. #8
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: SQL statement syntax error

    The query string looks OK to me... I don't know if that trailing blank space has any effects but you can always test it out by removing it, right?
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    366

    Re: SQL statement syntax error

    yeah could it be that?
    oh, not so straight forward, I was going to do a .repalce("vbcrlf", "") but that will take them all out. is there a way to remove the last character in one command?

  10. #10
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: SQL statement syntax error

    Quote Originally Posted by GaryMazzone View Post
    The issue is the extra single quotes in the string. When the SQL parser runs into them it things that is the end of the value. But there is more so there is a SQL error. There are two ways to fix. The first (not the perfered) is to use a replace function like this:

    Code:
    cmd.CommandText = "INSERT INTO [Failed$] (OrderTime, Phone, MessageDetails) values ('" & Order_date & "','" & incomingtextno & "','" & textmsg.Replace("'","''") & "')"
    The second way and the perfered is to use parameters in the command object.
    That was what I thought too, but his actual query (post # 3) doesn't show any extra single quotes though. I'm puzzled by this
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  11. #11
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: SQL statement syntax error

    In regards to a trailing space I tested for this in my reply using parameters

    Code:
    cmd.Parameters.AddWithValue("@MessageDetails", "Hey guys  ")
    Then
    Code:
    cmd.Parameters.AddWithValue("@MessageDetails", "Hey guys")
    Both inserted just fine.

    Now if one of the columns DataType was set in Excel that could be an issue. For instance +4477256******** works fine without formatting but change the column to numeric it will fail.

  12. #12
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: SQL statement syntax error

    Quote Originally Posted by youngnoviceinneedofh View Post
    yeah could it be that?
    oh, not so straight forward, I was going to do a .repalce("vbcrlf", "") but that will take them all out. is there a way to remove the last character in one command?
    Use String.Trim, String.TrimStart or String.TrimEnd to trim white spaces off your string.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  13. #13
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: SQL statement syntax error

    The following shows results from using parameters. The first row has spaces and a line feed, second has line feed embedded while the third has the cell set to wrap. All inserts worked so the provider is handling things differently is my guess between a parmeterized vs non-parameterized SQL INSERT statement.
    Last edited by kareninstructor; Mar 22nd, 2012 at 11:39 AM.

  14. #14
    Hyperactive Member Ram2Curious's Avatar
    Join Date
    Apr 2010
    Posts
    484

    Re: SQL statement syntax error

    Parameters will be the best solution. The ' " & textmsg & " ' will work properly if you try to insert just Hi Guys, but will not work if you try to insert Hi Guys ' which is followed by a single quote. Like suggested earlier, Replace function should work in this case.

    Try to insert some text with a single quote and without a single quote. Test it.

    I tested it on my old projects where i did not use parameters or replace function, it exactly throws the same error !.

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    366

    Re: SQL statement syntax error

    I'm not trying to pass any ' to the excel, if this is whats going on i have missed it. I am only interested in passing the text

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

    Re: SQL statement syntax error

    You should always use parameters.

    If you did, this problem would almost certainly disappear with no effort - as would many other problems.

    As several people have told you in this thread (and in previous ones), you should be using parameters. If you don't, you just create more problems for yourself.

    For an explanation of why you should be using parameters (and links to code examples), see the article Why should I use Parameters instead of putting values into my SQL string? from our Database Development FAQs/Tutorials (at the top of the Database Development forum).

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