Alex Rivera | Logout

DELETE*FROM table

Asked 2011-03-31T07:17:56.360
16

Is there a way (similar to the below code) to delete all the rows in a specified table using c#?

SqlConnection con = new SqlConnection(conString);
con.Open();

string sql = @"DELETE*FROM compsTickers;";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.ExecuteNonQuery();
con.Close();

Right now i'm getting an error:

Incorrect syntax near '*'

Edit
Report

3 Answers

16

You don't specify columns DELETE FROM compsTickers is enough.

(Also TRUNCATE TABLE compsTickers will do, it's a different way of deletion, that could make sense depending on your transaction log settings)

answered 2011-03-31T07:18:32.553
10

Just remove the * as it isn't needed to for DELETE statements

string sql = @"DELETE FROM compsTickers;";

answered 2011-03-31T07:18:35.837
3

DELETE (Transact-SQL) (link)

FROM is optional. You're always DELETing from a table, so all you needed was (you don't even need a statement delimiter, for a single statement):

DELETE compsTickers

Which empties the table

  • but does not reset the identity column, if any
answered 2011-03-31T07:39:30.780

Your Answer