PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Hébergement serveur > comp.db.ms-sqlserver > Getting record position in sql server 2000
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Getting record position in sql server 2000

Réponse
 
LinkBack Outils de la discussion
Vieux 25/03/2008, 14h24   #1
shilpa.vastrad@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Getting record position in sql server 2000

hi

i am using SQL server 2000. I want to know the record position while
retrieving records. its like row_number() in sql server 2005

Is it possible?
  Réponse avec citation
Vieux 25/03/2008, 15h00   #2
Plamen Ratchev
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Getting record position in sql server 2000

Here is one way to emulate ROW_NUMBER in SQL Server 2000, but be aware this
can be very slow with large data set:

CREATE TABLE Foo (
keycol CHAR(2) PRIMARY KEY,
datacol CHAR(1))

INSERT INTO Foo VALUES ('AA', 'a')
INSERT INTO Foo VALUES ('AC', 'c')
INSERT INTO Foo VALUES ('BC', 'd')
INSERT INTO Foo VALUES ('BA', 'e')

SELECT keycol,
datacol,
(SELECT COUNT(*)
FROM Foo AS B
WHERE B.keycol <= A.keycol) AS rn
FROM Foo AS A
ORDER BY keycol

HTH,

Plamen Ratchev
http://www.SQLStudio.com

  Réponse avec citation
Vieux 25/03/2008, 16h02   #3
Tom van Stiphout
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Getting record position in sql server 2000

On Tue, 25 Mar 2008 05:24:49 -0700 (PDT), shilpa.vastrad@gmail.com
wrote:

There rarely is a good reason for needing a row number. What is yours?
-Tom.


>hi
>
>i am using SQL server 2000. I want to know the record position while
>retrieving records. its like row_number() in sql server 2005
>
>Is it possible?

  Réponse avec citation
Vieux 26/03/2008, 00h45   #4
Erland Sommarskog
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Getting record position in sql server 2000

(shilpa.vastrad@gmail.com) writes:
> i am using SQL server 2000. I want to know the record position while
> retrieving records. its like row_number() in sql server 2005
>
> Is it possible?


As Plamen mentioned, the direct method to mimick the row_number()
function in SQL 2000 with a COUNT subquery performs poorly on large
datasets.

The alternative is to bounce data over a temp table with an IDENTITY
column:

CREATE TABLE #t (ident int IDENTITY, ....)
INSERT #t (...)
SELECT ...
ORDER BY <what you want to number on>

It's important that you use CREATE TABLE to create the table, as with
SELECT INTO there is no guarantee that the IDENTITY column will obey
the ORDER BY. It's not really clear whether this is the case for INSERT
either, but at least the odds seems to be better!

If you need the corresponding to PARTITION BY, you need an extra column
that you set like:

UPDATE #t
SET newnum = a.ident - b.minident + 1
FROM #t a
JOIN (SELECT col, minident = MIN(ident) FROM #t GROUP BY col) AS b
ON a.col = b.col



--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
  Réponse avec citation
Vieux 26/03/2008, 00h47   #5
Erland Sommarskog
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Getting record position in sql server 2000

Tom van Stiphout (no.spam.tom7744@cox.net) writes:
> There rarely is a good reason for needing a row number. What is yours?


The row_number() function is one of the absolutely most useful additions
to SQL 2005. The only thing that can compete is the new error handling.
There are so many problems which are solved easily and efficiently with
the row_number function.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
  Réponse avec citation
Vieux 26/03/2008, 08h25   #6
shilpa.vastrad@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Getting record position in sql server 2000

On Mar 26, 3:47 am, Erland Sommarskog <esq...@sommarskog.se> wrote:
> Tom van Stiphout (no.spam.tom7...@cox.net) writes:
>
> > There rarely is a good reason for needing a row number. What is yours?

>
> The row_number() function is one of the absolutely most useful additions
> to SQL 2005. The only thing that can compete is the new error handling.
> There are so many problems which are solved easily and efficiently with
> the row_number function.
>
> --
> Erland Sommarskog, SQL Server MVP, esq...@sommarskog.se
>
> Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books...
> Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/books.mspx


thanks to all.
the solutions are pretty good. But not feasible to me. is any other
solution available. If so then plz
  Réponse avec citation
Vieux 26/03/2008, 17h58   #7
Erland Sommarskog
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Getting record position in sql server 2000

(shilpa.vastrad@gmail.com) writes:
> thanks to all.
> the solutions are pretty good. But not feasible to me. is any other
> solution available. If so then plz


If they are not feasible to you, it would be a good thing if you could
provide more details the problem you are trying to solved, and why the
suggested solutions will not work for you.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
  Réponse avec citation
Vieux 26/03/2008, 18h15   #8
Ed Murphy
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Getting record position in sql server 2000

shilpa.vastrad@gmail.com wrote:

> On Mar 26, 3:47 am, Erland Sommarskog <esq...@sommarskog.se> wrote:
>> Tom van Stiphout (no.spam.tom7...@cox.net) writes:
>>
>>> There rarely is a good reason for needing a row number. What is yours?

>> The row_number() function is one of the absolutely most useful additions
>> to SQL 2005. The only thing that can compete is the new error handling.
>> There are so many problems which are solved easily and efficiently with
>> the row_number function.
>>
>> --
>> Erland Sommarskog, SQL Server MVP, esq...@sommarskog.se
>>
>> Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books...
>> Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

>
> thanks to all.
> the solutions are pretty good. But not feasible to me. is any other
> solution available. If so then plz


Why aren't they feasible to you? Can you give an example with a small
amount of sample data, and explain in conceptual terms what you would
want to do with that sample data?
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h31.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13196 seconds with 16 queries