Is it possible to delete the 'first' record from a table in SQL Server
, without using any WHERE
condition and without using a cursor?
相关问题
- SQL join to get the cartesian product of 2 columns
- sql execution latency when assign to a variable
- Difference between Types.INTEGER and Types.NULL in
- What is the best way to cache a table from a (SQL)
- php PDO::FETCH_ASSOC doesnt detect select after ba
Does this really make sense?
There is no "first" record in a relational database, you can only delete one random record.
SQL-92:
Similar to the selected answer, a table source can be used, in this case a derived query:
Feel free to add orderbys and conditions.
For the next example, I'll assume that the restriction on 'where' is due to not wanting to select a row based on its values. So assuming that we want to delete a row based on position (in this case the first position):
Note that the (select 1) makes it the sort order that the tables or indexes are in. You can replace that with a newid to get fairly random rows.
You can also add a partition by to delete the top row of each color, for example.
No, AFAIK, it's not possible to do it portably.
There's no defined "first" record anyway - on different SQL engines it's perfectly possible that "
SELECT * FROM table
" might return the results in a different order each time.Note that
will also work, but, as stated in the documentation:
Therefore, it's better to use
WITH
and anORDER BY
clause, which will let you specify more exactly which row you consider to be the first.depends on your DBMS (people don't seem to know what that is nowadays)