Is it possible to delete from multiple tables in the same SQL statement?

Nope, you’d need to run multiple statements. Because you need to delete from two tables, consider creating a temp table of the matching ids: SELECT U.Id INTO #RecordsToDelete FROM Users U JOIN LinkingTable J ON U.Id = J.U_Id … And then delete from each of the tables: DELETE FROM Users WHERE Id IN (SELECT Id … Read more

DataTable, How to conditionally delete rows

You could query the dataset and then loop the selected rows to set them as delete. var rows = dt.Select(“col1 > 5”); foreach (var row in rows) { row.Delete(); } dt.AcceptChanges(); … and you could also create some extension methods to make it easier … myTable.Delete(“col1 > 5”); public static DataTable Delete(this DataTable table, string … Read more

How to get the number of deleted rows in PostgreSQL?

You can use RETURNING clause: DELETE FROM table WHERE condition IS TRUE RETURNING *; After that you just have to check number of rows returned. You can streamline it with CTE: WITH deleted AS (DELETE FROM table WHERE condition IS TRUE RETURNING *) SELECT count(*) FROM deleted; This should return just the number of deleted … Read more

SQL DELETE with JOIN another table for WHERE condition

Due to the locking implementation issues, MySQL does not allow referencing the affected table with DELETE or UPDATE. You need to make a JOIN here instead: DELETE gc.* FROM guide_category AS gc LEFT JOIN guide AS g ON g.id_guide = gc.id_guide WHERE g.title IS NULL or just use a NOT IN: DELETE FROM guide_category AS … Read more

Linq to Sql: How to quickly clear a table

You could do a normal SQL truncate or delete command, using the DataContext.ExecuteCommand method: context.ExecuteCommand(“DELETE FROM Entity”); Or context.ExecuteCommand(“TRUNCATE TABLE Entity”); The way you are deleting is taking long because Linq to SQL generates a DELETE statement for each entity, there are other type-safe approaches to do batch deletes/updates, check the following articles: Batch Updates … Read more

Deleting specific rows from DataTable

If you delete an item from a collection, that collection has been changed and you can’t continue to enumerate through it. Instead, use a For loop, such as: for(int i = dtPerson.Rows.Count-1; i >= 0; i–) { DataRow dr = dtPerson.Rows[i]; if (dr[“name”] == “Joe”) dr.Delete(); } dtPerson.AcceptChanges(); Note that you are iterating in reverse … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)