December 8, 2003 at 8:00 pm
How to add a sequence number in select clause?
for example
select name,Sex from stuent
name sex
andy male
judy female
...
now I want to get the following result
1 andy male
2 judy female
3 ...
In a word I want to add a sequence field in my select result.
Thanks in advance
Andy
December 8, 2003 at 8:32 pm
I admit this answer doesn't directly answer your question - how to add a sequence number directly into a SELECT statement, but it does provide that result.
This may not be the cleanest method - it uses a temp table, which I usually try to avoid, and it adds a couple steps to your query.
The IDENTITY() function can be used when performing a SELECT...INTO [tablename], and it acts like the column is an identity column.
-- using a column of type int called Seq to hold the sequence number
SELECT IDENTITY(int, 1, 1) AS Seq, Name, Sex
INTO #tmpStudent
FROM Student
SELECT * FROM #tmpStudent
ORDER BY Seq
DROP TABLE #tmpStudent
GO
Hope this helps,
-Eddie
Eddie Wuerch
MCM: SQL
December 9, 2003 at 2:04 am
I like these 'easy' questions
Check this out http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/q186/1/33.asp&NoWebContent=1
Frank
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
December 9, 2003 at 2:09 am
And hopefully one day I'll learn to post the correct link
http://support.microsoft.com:80/support/kb/articles/q186/1/33.asp&NoWebContent=1
Frank
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
December 9, 2003 at 2:46 am
And hopefully some day I'll see the forest from the trees
http://www.sqlservercentral.com/columnists/glarsen/sequentialnumbering.asp
Frank
Learn each day something new!
Edited by - Frank kalis on 12/09/2003 04:38:13 AM
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply