| Author |
Topic |
|
Sep410
Posting Yak Master
117 Posts |
Posted - 2008-05-22 : 17:41:42
|
| Is there any way I can add record number to my query result?Exp:Name Add telA 1 st 123456B H ave. 987456I need:No Name Add tel 1 A 1 st 123456 2 B H ave. 987456Thanks |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-05-22 : 17:44:27
|
| Are you using SQL Server 2005?Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Database maintenance routines:http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx |
 |
|
|
Sep410
Posting Yak Master
117 Posts |
Posted - 2008-05-22 : 17:46:53
|
| No |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2008-05-22 : 19:50:43
|
Here is one way:select rownumber = (select count(*) from sysobjects sq where sq.id <= so.id) ,namefrom sysobjects soorder by id you could also put the results into a #temp table that has an identity column defined then select out of the #temp table.Be One with the OptimizerTG |
 |
|
|
Sep410
Posting Yak Master
117 Posts |
Posted - 2008-05-23 : 11:02:01
|
| I don’t think this can help me TG. I don’t have id column or any other column that contains numbers. |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2008-05-23 : 11:15:01
|
<sigh>declare @t table (Name varchar(10), Addr varchar(10), tel varchar(10))insert @tselect 'A', '1 st', '123456' union allselect 'B', 'H ave.', '987456'select rownumber = (select count(*) from @t sq where sq.name <= so.name) ,name ,Addr ,telfrom @t soorder by nameoutput:rownumber name Addr tel----------- ---------- ---------- ----------1 A 1 st 1234562 B H ave. 987456 Be One with the OptimizerTG |
 |
|
|
Sep410
Posting Yak Master
117 Posts |
Posted - 2008-05-23 : 11:26:11
|
| Dear TG, I have 1124 records in my table it is not possible to do the first part that you mentioned. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-05-23 : 11:43:20
|
| The first part is just to illustrate the problem. Run the entire thing to see how it works. Then use just the last query and modify it for your objects.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Database maintenance routines:http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx |
 |
|
|
jdippner
Starting Member
1 Post |
Posted - 2008-05-23 : 11:49:06
|
quote: Originally posted by Sep410 Dear TG, I have 1124 records in my table it is not possible to do the first part that you mentioned.
Sep, This should give you what you need. It is a trivial example using the sysobjects table, but essentially you create a temp table to hold the columns of your original column + a column for the row count. Then you do an insert/select to populate the temp table. Lastly, you query from the temp table. declare @t table ( row_id int identity (1,1), name varchar(50), id int)insert into @t(name, id)select name, id from sysobjectsorder by idselect * from @t |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-05-26 : 05:02:22
|
quote: Originally posted by Sep410 Is there any way I can add record number to my query result?Exp:Name Add telA 1 st 123456B H ave. 987456I need:No Name Add tel 1 A 1 st 123456 2 B H ave. 987456Thanks
Where do you want to show data?If you use front end application, do numbering thereMadhivananFailing to plan is Planning to fail |
 |
|
|
|