Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Query

Author  Topic 

Ramnadh
Starting Member

23 Posts

Posted - 2004-07-23 : 02:58:38
Hi,

I have to make a query like that the row number should be there in everyrow in the resultset. we have to use only the "select" query without using any updates.

One method is by doing like this...

Select job_desc, (Select Count(*) + 1 FROM jobs B
WHERE B.job_desc < A.job_desc) AS RecNo
FROM jobs A
ORDER By job_desc

But i am not using the table directly. It was included with the other tables in the query in the StoredProcedure. When i join with the other tables the rownum doesn't get in sequence...
and cannot depend on any column value because all the values in all the Columns can be dupliate..

Can any one help me.

Regards,
Ramnadh.


Ramnadh

Kristen
Test

22859 Posts

Posted - 2004-07-23 : 04:18:49
You could create a temporary table with an IDENTITY column (additional to the columns that you need) and then INSERT INTO #MyTempTable SELECT ... ORDER BY ... so that they are inserted in sequence.

Select job_desc, IDENTITY(1,1) AS RecNo
INTO #TEMP
FROM jobs A
ORDER By job_desc

SELECT * FROM #TEMP ORDER BY RecNo

DROP TABLE #TEMP

If the resultset is more than a handful of rows you need to CREATE TABLE #TEMP first and make sure you make the identity column a Primary Key

Kristen
Go to Top of Page
   

- Advertisement -