SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL to delete the first 300 rows?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

narf
Starting Member

7 Posts

Posted - 06/10/2012 :  04:53:05  Show Profile  Reply with Quote
hi folks,

i'm trying to delete the first 300 rows of a table. though the rows have an index number, it has a lot of gaps. so, row 1 has indeed index no. 1, but row 10 could already have index no. 50.

so, is there a possibility to delete the first 300 rows that come up when it's sorted by the index no?

table
----
index #, content
1, blah
10, blahblah
23, blaaah
45, blahhhh
[...]


thank you very much in advance :)

cheers,
narf

Edited by - narf on 06/10/2012 07:45:56

visakh16
Very Important crosS Applying yaK Herder

India
47023 Posts

Posted - 06/10/2012 :  14:44:34  Show Profile  Reply with Quote
yep do like below

--SELECT * 
DELETE t
FROM 
(SELECT ROW_NUMBER() OVER (ORDER BY [index #] ASC) AS Seq
--,*
FROM table
)t
WHERE Seq <=300


suggest you to first use SELECT instead of DELETE (uncomment commneted part after commenting DELETE) and then make sure it returns exact records you want
Once you're happy then comment them back to make statement DELETE

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

narf
Starting Member

7 Posts

Posted - 06/11/2012 :  06:47:33  Show Profile  Reply with Quote
thank you very much for your answer, visakh16 :)

i used your sql-statement but i get a syntax error near "("

i tried it without the "order by" statement (...over [index #] as seq), but then i got a syntax error near [index #]. so i think, the upper error is because of the bracket after "over"...

any ideas?
Go to Top of Page

webfred
Flowing Fount of Yak Knowledge

Germany
8513 Posts

Posted - 06/11/2012 :  06:52:58  Show Profile  Visit webfred's Homepage  Reply with Quote
1. Post the statement that errors.
2. Post the exact column names. Is there a column named index #?


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

narf
Starting Member

7 Posts

Posted - 06/11/2012 :  07:54:40  Show Profile  Reply with Quote
i can't tell which statement errors because the error message isn't any more specific than i was :)

SELECT *
FROM
(SELECT ROW_NUMBER() OVER (ORDER BY rate.rate ASC) AS Seq
FROM rate ) t
WHERE Seq <=300

the index column name and the table name both are named "rate"
Go to Top of Page

DonAtWork
Flowing Fount of Yak Knowledge

2111 Posts

Posted - 06/11/2012 :  11:35:46  Show Profile  Reply with Quote
create table #rate (rate int)
declare @f int = 0
while @f < 400
begin
insert into #rate 
select @f 

set @f +=1 
end

select * from #rate


SELECT *
FROM
(SELECT ROW_NUMBER() OVER (ORDER BY #rate.rate ASC) AS Seq
FROM #rate ) t
WHERE Seq <=300


Works like a champ. Perhaps this isnt your FULL sql statement...









How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47023 Posts

Posted - 06/11/2012 :  15:23:54  Show Profile  Reply with Quote
quote:
Originally posted by narf

i can't tell which statement errors because the error message isn't any more specific than i was :)

SELECT *
FROM
(SELECT ROW_NUMBER() OVER (ORDER BY rate.rate ASC) AS Seq
FROM rate ) t
WHERE Seq <=300

the index column name and the table name both are named "rate"


Are you sure you're using SQL 2005 and above?

what does below return?


SELECT @@VERSION
GO
EXEC sp_dbcmptlevel 'yourdbname'
GO


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

narf
Starting Member

7 Posts

Posted - 06/11/2012 :  15:51:38  Show Profile  Reply with Quote
thank you very much for your answers...

@DonAtWork: your statements don't work for me... "syntax error near #rate", but i think, it's the same problem as visakh16 is trying to find out :)

@ visakh16: that statement doesn't work either ("unrecognized token @") :) i'm using SQLite Expert with an sqlite database. i'm not sure how compatible that is to sql 2005... but i think the compatibility is limited ;)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47023 Posts

Posted - 06/11/2012 :  15:55:55  Show Profile  Reply with Quote
quote:
Originally posted by narf

thank you very much for your answers...

@DonAtWork: your statements don't work for me... "syntax error near #rate", but i think, it's the same problem as visakh16 is trying to find out :)

@ visakh16: that statement doesn't work either ("unrecognized token @") :) i'm using SQLite Expert with an sqlite database. i'm not sure how compatible that is to sql 2005... but i think the compatibility is limited ;)


i dont know about sqllite

are you sure your db is sql server?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

narf
Starting Member

7 Posts

Posted - 06/12/2012 :  03:23:04  Show Profile  Reply with Quote
the db is an "sqlite" db... but i didn't find any better category in this board. but i didn't know that this would even matter.... i thought, an sql statement is an sql statement.. now i know better ;)

can anyone help me transform one of the upper statements into a working statement for sqlite databases?
Go to Top of Page

Sandips
Starting Member

India
5 Posts

Posted - 06/12/2012 :  07:00:34  Show Profile  Reply with Quote
CREATE TABLE #Sample
(
ID INT IDENTITY(1,1),
SAMPLE INT
);
GO

INSERT #Sample
SELECT 55
GO 310

DELETE TOP (300) FROM #Sample
GO

SELECT * FROM #Sample
GO
DROP TABLE #Sample
GO
Please let me know if this helps

Edited by - Sandips on 06/12/2012 07:01:48
Go to Top of Page

narf
Starting Member

7 Posts

Posted - 06/12/2012 :  08:38:13  Show Profile  Reply with Quote
sorry, that doesn't work either :)

- i get a syntax error, when trying to use # in the table name
- it doesn't recognize "GO 310"
- function TOP is unknown (but would be great if it was implemented)
Go to Top of Page

webfred
Flowing Fount of Yak Knowledge

Germany
8513 Posts

Posted - 06/12/2012 :  08:46:01  Show Profile  Visit webfred's Homepage  Reply with Quote
http://www.sqlite.org/lang_delete.html



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

narf
Starting Member

7 Posts

Posted - 06/12/2012 :  10:21:08  Show Profile  Reply with Quote
quote:
If SQLite is compiled with the SQLITE_ENABLE_UPDATE_DELETE_LIMIT compile-time option, then the syntax of the DELETE statement is extended by the addition of optional ORDER BY and LIMIT clauses:


that LOOKED so good. but unfortunately, neither sqlite expert nor an sqlite shell recognized the "limit" param :/

but thanks anyway for your answer :)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47023 Posts

Posted - 06/12/2012 :  14:59:16  Show Profile  Reply with Quote
quote:
Originally posted by narf

quote:
If SQLite is compiled with the SQLITE_ENABLE_UPDATE_DELETE_LIMIT compile-time option, then the syntax of the DELETE statement is extended by the addition of optional ORDER BY and LIMIT clauses:


that LOOKED so good. but unfortunately, neither sqlite expert nor an sqlite shell recognized the "limit" param :/

but thanks anyway for your answer :)


search for sqllite forums and post this in it. that would be best bet for you!

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

kumaraswamy
Starting Member

India
1 Posts

Posted - 06/13/2012 :  08:13:59  Show Profile  Reply with Quote
Hi narf,
i am send the code used in server 2008 with a small example.
it might help u

create rate
(
id int,
name nvarchar(10)
)

insert into rate values(1,'a'),(4,'b'),(6,'r'),(2,'w'),(12,'s')

--using common table expression

with temp
as
(select ROW_NUMBER() over(order by id asc) as num,* from rate )
delete from temp where id<=5

kumaraswamy reddy
Go to Top of Page

webfred
Flowing Fount of Yak Knowledge

Germany
8513 Posts

Posted - 06/13/2012 :  08:21:24  Show Profile  Visit webfred's Homepage  Reply with Quote
quote:
Originally posted by kumaraswamy

Hi narf,
i am send the code used in server 2008 with a small example.
it might help u

create rate
(
id int,
name nvarchar(10)
)

insert into rate values(1,'a'),(4,'b'),(6,'r'),(2,'w'),(12,'s')

--using common table expression

with temp
as
(select ROW_NUMBER() over(order by id asc) as num,* from rate )
delete from temp where id<=5

kumaraswamy reddy


goin round in circles...
we have had this idea already!


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.21 seconds. Powered By: Snitz Forums 2000