0

I have a SQL statement to delete a record from a database table. After stepping through it, I can see that it all works correctly, except for the final part (that is to say, it correctly gets the desired row to delete and all variables match the values that they should be). I'm new to using SQL, so can anybody tell me how to fix the following error at all?

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll

Additional information: Incorrect syntax near '*'.

SQL query:

Public Shared Sub deleteRecord(ByVal recordID As Integer, con As OleDbConnection)
    Dim Dc As New OleDbCommand
    Dc.Connection = con

    con.Open()
    Dc.CommandText = "DELETE * FROM tblData WHERE([recordID] =  '" & recordID & "')"

    Dc.ExecuteNonQuery()
    con.Close()

End Sub
4
  • 1
    Get rid of the *. Syntax is DELETE FROM [TABLE] WHERE [FILTERS] Commented Jul 13, 2016 at 15:09
  • 1
    Copy past your query and try it directly in the database... The error tells you the problem, remove the star. Commented Jul 13, 2016 at 15:10
  • 4
    You should use parameterised queries. Whilst the int prevents SQL injection you are still potentially bloating the plan cache with single use queries. Commented Jul 13, 2016 at 15:14
  • 1
    @einpoklum Yes I approved the edit as the code was a vb.net function I made on the delete button click, it had nothing to do with the error I was asking about Commented Jul 13, 2016 at 15:21

3 Answers 3

3

Just remove the * and the parentheses. DELETE FROM tblData WHERE recordID = ...

Sign up to request clarification or add additional context in comments.

Comments

1

replace with this:

Dc.CommandText = "DELETE FROM tblData WHERE [recordID] = " & recordID

Comments

1

* is used in lieu of typing out all of the column names you want to SELECT. The important thing to note here is that it represents columns.

DELETE occurs against rows. Therefore, saying DELETE * does not make any sense. You can't delete single columns/cells in this way.

Correct syntax is DELETE FROM [TABLE] WHERE [FILTERS]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.