| Author |
Topic  |
|
|
narf
Starting Member
7 Posts |
Posted - 06/10/2012 : 04:53:05
|
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
47173 Posts |
Posted - 06/10/2012 : 14:44:34
|
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/
|
 |
|
|
narf
Starting Member
7 Posts |
Posted - 06/11/2012 : 06:47:33
|
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? |
 |
|
|
webfred
Flowing Fount of Yak Knowledge
Germany
8515 Posts |
Posted - 06/11/2012 : 06:52:58
|
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. |
 |
|
|
narf
Starting Member
7 Posts |
Posted - 06/11/2012 : 07:54:40
|
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" |
 |
|
|
DonAtWork
Flowing Fount of Yak Knowledge
2111 Posts |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47173 Posts |
Posted - 06/11/2012 : 15:23:54
|
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/
|
 |
|
|
narf
Starting Member
7 Posts |
Posted - 06/11/2012 : 15:51:38
|
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 ;) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47173 Posts |
Posted - 06/11/2012 : 15:55:55
|
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/
|
 |
|
|
narf
Starting Member
7 Posts |
Posted - 06/12/2012 : 03:23:04
|
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? |
 |
|
|
Sandips
Starting Member
India
5 Posts |
Posted - 06/12/2012 : 07:00:34
|
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 |
 |
|
|
narf
Starting Member
7 Posts |
Posted - 06/12/2012 : 08:38:13
|
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) |
 |
|
|
webfred
Flowing Fount of Yak Knowledge
Germany
8515 Posts |
|
|
narf
Starting Member
7 Posts |
Posted - 06/12/2012 : 10:21:08
|
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 :) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47173 Posts |
Posted - 06/12/2012 : 14:59:16
|
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/
|
 |
|
|
kumaraswamy
Starting Member
India
1 Posts |
Posted - 06/13/2012 : 08:13:59
|
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 |
 |
|
|
webfred
Flowing Fount of Yak Knowledge
Germany
8515 Posts |
Posted - 06/13/2012 : 08:21:24
|
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. |
 |
|
| |
Topic  |
|