Link to home
Create AccountLog in
Avatar of Moiz Saifuddin
Moiz Saifuddin

asked on

Delete records

Is this delete statement correct as in syntax if wanted to delete all the records 


delete * from <TableName>


Im getting an error on ExecuteNonQuery() and errmessage = "Incorrect syntax near '*'." 





if (rsBackup.RecordCount > 0)
{
stgSQL = "DELETE * FROM SalesTaxCombos";
DbCommand TempCommand = mdlG3Config.gdbTarget.Connection.CreateCommand();
TempCommand.CommandText = stgSQL;
UpgradeHelpers.DB.DbConnectionHelper.ResetCommandTimeOut(TempCommand);
TempCommand.Transaction = UpgradeHelpers.DB.TransactionManager.GetCurrentTransaction(mdlG3Config.gdbTarget.Connection);
TempCommand.ExecuteNonQuery();

Open in new window



Avatar of arnold
arnold
Flag of United States of America image

Which sql server?

Delete from <tablename>


This will delete entire rows unless restricts through a where clause.
Avatar of Moiz Saifuddin
Moiz Saifuddin

ASKER

Error message from source "Microsoft OLE DB Provider for SQL Server"
Although in this site below they mention delete * from... as a valid query to delete all the records

https://www.geeksforgeeks.org/sql-delete-statement/?ref=lbp
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
'delete from table' will work but has overhead.  If you want to remove ALL rows, look up truncate:
https://docs.microsoft.com/en-us/sql/t-sql/statements/truncate-table-transact-sql?view=sql-server-ver15