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
Just remove the * and the parentheses. DELETE FROM tblData WHERE recordID = ...
replace with this:
Dc.CommandText = "DELETE FROM tblData WHERE [recordID] = " & recordID
* 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]
Related
I am trying to change the text in a column and in return I get this error message using Visual Basic : The data types text and varchar are incompatible in the equal to operator.
Code :
Dim CMD As New SqlCommand("UPDATE Contacts SET FirstName ='Jack'" & " WHERE FirstName = 'Mark' ", CN)
CN.Open()
Dim rows As Integer
rows = CMD.ExecuteNonQuery
The column name 'FirstName' is specified more than once in the SET clause or column list of an INSERT. A column cannot be assigned more than one value in the same clause. I Modify the clause to make sure that a column is updated only once. If this statement updates or inserts columns into a view, column aliasing can conceal the duplication in the code.
I am new to Vertica and trying this in angular aspx page.
`con.Open();
cmd = con.CreateCommand();
cmd.Connection = con;
cmd.Parameters.Add(new VerticaParameter("#tblCustomers", table));
cmd.CommandText = "insert into customers select * from #tblCustomers";
cmd.ExecuteNonQuery();`
I have established the connection and inserted some fresh records too.
But Now I am trying to insert bulk records in my vertica database's table.
Something Same like SqlServer,
I have loaded my table data into "table" variable. Which is a datatable.
Is it possible to do like this ?? As I am getting some error
"
Incorrect syntax at or near $1"
customers and #tblCustomers both have same columns.
Thanks!
Setting aside whether or not you should do something like this, in the code that you've posted you're passing #tblCustomers as a parameter to the query, so it's going to treat it as a string value, not an object name in the query. You need to build the CommandText in your code without that as a parameter. Something like:
cmd.CommandText = "insert into customers select * from " & tableName
(Sorry if that syntax isn't quite right, but hopefully it gets across the point)
Some additional (and important) notes though:
Always use a column list when doing an INSERT. Use INSERT INTO MyTable (some_column, some_other_column) SELECT... not INSERT INTO MyTable SELECT...
NEVER use SELECT *. List out your column names.
how do you add a parameter after this code so that I can delete the records ininteger data type: here is the code.
com = New OleDbCommand("DELETE * FROM Products WHERE Product = '" & DataGridView1.CurrentRow.Cells(0).Value.ToString & "'", con1)
I keep getting a message that the data type is mismatched
You have no WHERE clause in your SQL code. If you don't specify a WHERE clause then you are referring to every record. You have to use the WHERE clause to specify WHICH record to update. That's what the primary key is for.
You must need a Where clause in your SQL Statement to distinct the row for edit else it can change all.
You must use where clause in that it update only that record which you want..if you don't use where clause it update all records.
for more information of update you use this link
I need to clear the contents of multiple tables in an Access db using VBA.
Below is the code I am attempting to use, but it throws a "Syntax error in FROM clause." The SQL syntax (DELETE * FROM [table name];) works in a query.
Dim tbl As TableDef
Dim sqlstr As String
DoCmd.SetWarnings False
For Each tbl In CurrentDb.TableDefs
If tbl.Name Like "*_drop" Then
sqlstr = "DELETE * FROM " & tbl.Name
DoCmd.RunSQL sqlstr
End If
Next tbl
I found a similar problem and answer here, but it did not solve my problem.
I have also tried it without the sqlstr and received the same error.
For Each tbl In CurrentDb.TableDefs
If tbl.Name Like "*_drop" Then
DoCmd.RunSQL "DELETE * FROM " & tbl.Name
End If
Next tbl
You confirmed a query like this works without error.
DELETE * FROM [table name];
However the query your code builds does not bracket table name. Therefore, any table name which includes a space or contains nearly any character other than letters, digits, and underscore will result in a DELETE statement which triggers that error. If the table name matches a reserved word, that could also trigger the error. (Unfortunately it's difficult to predict exactly when reserved words will cause trouble.) It's safer to bracket all troublesome words in queries when they are used as field or table names. And for those which are troublesome because they are reserved words, you can alias them instead of bracketing.
But wherever practical, the safest practice is to rename all such objects: avoid reserved words; and use only letters, digits, and underscore.
Meanwhile, code defensively to cope with problem names. Revise the code to always bracket the table name.
sqlstr = "DELETE FROM [" & tbl.Name & "]"
I think you just need DELETE FROM without the *
I'm debugging an app with the following code:
sql = myTable
Set datTable.Recordset = myDB.openRecordset(sql, dbOpenDynaset, dbSeeChanges)
where
sql = "select * from table Order by Precipition,Date/Time"
An error occurs on the second line saying "Run-time error '3061': Too few parameters. Expected 2". I believe the issue is the with the value of sql. I don't know to much about SQL, so does anyone have any ideas?
I thingk you can try
sql = "select * from table Order by Precipition,[Date/Time]"
Note the "[]"
You should try to avoid using table names/columns that contains spaces, or keywords, as this will make life very dufficult.
Use name that explain the field in context to the table.
The problem is in your order clause: more specifically here:
Date/Time.