Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Query to delete nth row in a table (SQL Server 2008 Express) Expand / Collapse
Author
Message
Posted Thursday, March 3, 2011 12:59 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, March 10, 2011 4:11 AM
Points: 6, Visits: 24
I found a query to select nth row....
 SELECT column/s FROM
(
SELECT column/s, row_number() over(ORDER BY getdate()) as r
FROM tablename
) as t
WHERE r = 4

But couldn't find any query for deletion...

Any idea ??
Post #1072860
Posted Thursday, March 3, 2011 1:08 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, July 22, 2014 9:03 PM
Points: 2,262, Visits: 5,405
smarty3010 (3/3/2011)
I found a query to select nth row....
 SELECT column/s FROM
(
SELECT column/s, row_number() over(ORDER BY getdate()) as r
FROM tablename
) as t
WHERE r = 4

But couldn't find any query for deletion...

Any idea ??


DELETE t.Column
FROM
(
SELECT column/s, row_number() over(ORDER BY getdate()) as r
FROM tablename
) as t
WHERE r = 4

Post #1072875
Posted Thursday, March 3, 2011 5:53 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Saturday, July 19, 2014 8:57 PM
Points: 310, Visits: 650
try this
BEGIN TRAN test

DELETE tablename
FROM (SELECT Column
FROM
(
SELECT column/s, row_number() over(ORDER BY getdate()) as r
FROM tablename
) as t
WHERE r = 4)tmp
INNER JOIN tablename
ON tablename.column = tmp.column

COMMIT TRAN test
Post #1073025
Posted Friday, March 4, 2011 3:32 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, August 11, 2014 6:34 PM
Points: 157, Visits: 290
Using CTE, you can select, update and delete specific record(s). Pls refer the below code

create table #temp (id int)

--delete #temp
insert into #temp values(1)
insert into #temp values(2)
insert into #temp values(3)
insert into #temp values(4)
insert into #temp values(5)

select * from #temp

;with cte (id,r_num)
as(
select *,ROW_NUMBER() over(Order by getdate()) from #temp
)
delete cte where r_num = 3;

select * from #temp

;with cte (id,r_num)
as(
select *,ROW_NUMBER() over(Order by getdate()) from #temp
)
update cte set id = 1000 where r_num = 2;

select * from #temp

drop table #temp

Thanks
Gopi
Post #1073169
Posted Friday, March 4, 2011 3:55 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 5:24 AM
Points: 1,236, Visits: 9,781
It's important to note that none of these solutions will guarantee to delete the same rows as are being returned by the select statement (nor indeed is the select statement guaranteed to return the same rows each time).

Because your row number order is entirely arbitrary, a different plan could well result in different results.

My question is, why would you want to arbitrarily delete rows with no apparent logic?
Post #1073175
Posted Friday, March 4, 2011 4:16 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 10:26 AM
Points: 11,194, Visits: 11,136
HowardW (3/4/2011)
It's important to note that none of these solutions will guarantee to delete the same rows as are being returned by the select statement...

Hi Howard,

I agree with your comments about the reason for deleting the 'nth' row in no particular order, but I can't for the life of me understand what the extract above is getting at. A query like the following is guaranteed to delete the row identified as #4 (peculiar though the logic is):

DELETE  E
FROM (
SELECT rn = ROW_NUMBER() OVER (ORDER BY (SELECT 0))
FROM dbo.Example
) AS E
WHERE E.rn = 4
;

Naturally, we would normally use a deterministic order by with the row number window function to identify the row or rows to delete.

Paul




Paul White
SQL Server MVP
SQLblog.com
@SQL_Kiwi
Post #1073184
Posted Friday, March 4, 2011 6:15 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, March 10, 2011 4:11 AM
Points: 6, Visits: 24
Thanks a million guys !!
Tried the code by SQLkiwi and ColdCoffee (used t instead of t.column)
Worked perfectly

I am building a visual tool for managing a database.....
So a deleting a row (which a user can view and wants to delete) would be much simpler !!
Post #1073261
Posted Friday, March 4, 2011 6:53 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 5:24 AM
Points: 1,236, Visits: 9,781
SQLkiwi (3/4/2011)
HowardW (3/4/2011)
It's important to note that none of these solutions will guarantee to delete the same rows as are being returned by the select statement...

Hi Howard,

I agree with your comments about the reason for deleting the 'nth' row in no particular order, but I can't for the life of me understand what the extract above is getting at. A query like the following is guaranteed to delete the row identified as #4 (peculiar though the logic is):

DELETE  E
FROM (
SELECT rn = ROW_NUMBER() OVER (ORDER BY (SELECT 0))
FROM dbo.Example
) AS E
WHERE E.rn = 4
;

Naturally, we would normally use a deterministic order by with the row number window function to identify the row or rows to delete.

Paul


It's guaranteed to delete the nth row within that delete statement, but as far as I'm aware, you could run the select (which will generate one plan) and assume that the delete statement will delete the same rows. However, the delete statement could reasonably generate a different plan and delete different arbitrary rows than your original select statement did. Unless I'm misunderstanding how the optimiser works in this case and it doesn't generate a plan based on the overall query and will always generate the plan based on the sub query alone.

Hopefully the above makes sense (even if it's wrong!)
Post #1073288
Posted Friday, March 4, 2011 7:34 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 10:26 AM
Points: 11,194, Visits: 11,136
HowardW (3/4/2011)
It's guaranteed to delete the nth row within that delete statement, but as far as I'm aware, you could run the select (which will generate one plan) and assume that the delete statement will delete the same rows. However, the delete statement could reasonably generate a different plan and delete different arbitrary rows than your original select statement did. Unless I'm misunderstanding how the optimiser works in this case and it doesn't generate a plan based on the overall query and will always generate the plan based on the sub query alone. Hopefully the above makes sense (even if it's wrong!)

I see, so you were saying that running the SELECT first (and noting the row it selected) and then expecting the DELETE (with the same SELECT statement as a subquery) to delete the same row would be unsound. I agree with that of course.

I thought you were saying that the DELETE might not delete the row identified by the SELECT (when run as part of the DELETE statement). My apologies - I think I did misread what you were getting at.




Paul White
SQL Server MVP
SQLblog.com
@SQL_Kiwi
Post #1073319
Posted Friday, March 4, 2011 7:40 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 5:24 AM
Points: 1,236, Visits: 9,781
SQLkiwi (3/4/2011)

I see, so you were saying that running the SELECT first (and noting the row it selected) and then expecting the DELETE (with the same SELECT statement as a subquery) to delete the same row would be unsound. I agree with that of course.

I thought you were saying that the DELETE might not delete the row identified by the SELECT (when run as part of the DELETE statement). My apologies - I think I did misread what you were getting at.


Exactly. No problem, I probably wasn't being very clear!
Post #1073324
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse