tags:

views:

2751

answers:

6

Is it possible to delete the 'first' record from a table in MS SQL Server, without using any WHERE condition and without using a cursor?

+11  A: 
WITH  q AS
        (
        SELECT TOP 1 *
        FROM    mytable
        /* You may want to add ORDER BY here */
        )
DELETE
FROM    q

Note that

DELETE TOP (1)
FROM   maytable

will also work, but, as stated in the documentation:

The rows referenced in the TOP expression used with INSERT, UPDATE, or DELETE are not arranged in any order.

Therefore, you better use WITH decision with ORDER BY clause, which will let you specify more exactly which row you consider to be the first.

Quassnoi
Well done that man
ck
+4  A: 

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.

Alnitak
+1  A: 

Does this really make sense?
There is no "first" record in a relational database, you can only delete one random record.

haarrrgh
It makes sense when you mix in "order by"; Question doesn't mention not to use "order by", just not "where" and cursors
Sung Meister
Only if you order by a column that is unique. If it's not unique, you still don't know exactly which record you delete.
haarrrgh
+2  A: 

Define "First"? If the table has a PK then it will be ordered by that, and you can delete by that:

DECLARE @TABLE TABLE
(
    ID INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
    Data NVARCHAR(50) NOT NULL
)

INSERT INTO @TABLE(Data)
SELECT 'Hello' UNION
SELECT 'World' 

SET ROWCOUNT 1
DELETE FROM @TABLE
SET ROWCOUNT 0

SELECT * FROM @TABLE

If the table has no PK, then ordering won't be guaranteed...

Meff
What if I have clustered index that is not the same as my PK?
onedaywhen
Ah of course, my bad - Then it will be by the first row in the clustered index, as that physically orders the rows, not the PK. Apologies for the confusion.
Meff
Even then, it's not guaranteed to be the "first" ordered by the clustered index. SQL Server makes no guarantees on this.
Damien_The_Unbeliever
Damien_The_Unbeliever
I like that PPT, thank you very much. LOL at "NoLock is a cool hint" - I worked in a place that believed that and would not accept otherwise. I left.
Meff
+2  A: 

depends on your DBMS (people don't seem to know what that is nowadays)

-- MYSql:
DELETE FROM table LIMIT 1;
-- Postgres:
DELETE FROM table LIMIT 1;
-- MSSql:
DELETE TOP(1) FROM table;
-- Oracle:
DELETE FROM table WHERE ROWNUM = 1;
soulmerge
Oracle's one violates "without ... where" restriction (whatever is the purpose of said restriction).
Stanislav Kniazev
A: 

What do you mean by «'first' record from a table» ? There's no such concept as "first record" in a relational db, i think.

Using MS SQL Server 2005, if you intend to delete the "top record" (the first one that is presented when you do a simple "*select * from tablename*"), you may use "delete top(1) from tablename"... but be aware that this does not assure which row is deleted from the recordset, as it just removes the first row that would be presented if you run the command "select top(1) from tablename".

XpiritO
But which row would appear first could vary between running that select and running the subsequent delete. There are *no* guarantees on ordering without an order by clause.
Damien_The_Unbeliever
Yes, you're right. I think I underlined that in my post. The "top record" may vary. Thanks for your reply, as my English isn't the best and I may have not been clear in my reply.
XpiritO