Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
Daniele writes "Hello, this is my first question.I'm a beginners in Sql Server, i must add a number before my columns in select.For example i've this table: Peoplefirst_name, last_nameMARIO ROSSIJOHN BLACK.... ....when i execute a "Select * from people" i would like obtain this: 1 MARIO ROSSI 2 JOHN BLACK Is there a Stored Procedures o some other for my problem?Thanks"
andre
Constraint Violating Yak Guru
259 Posts
Posted - 2002-01-29 : 17:52:13
Here's one way to do it:
CREATE TABLE #Temp (Rownum int identity,FirstName varchar(50),LastName varchar(50))INSERT INTO #Temp (FirstName,LastName)SELECT FirstName,LastName FROM People;SELECT * FROM #Temp ORDER BY Rownum;DROP TABLE #Temp
butlermi_11
Starting Member
10 Posts
Posted - 2002-01-29 : 18:57:19
Here is another way to write your query using an inner join. SELECT RANK=Count(*), P.FirstName, P.LastNameFROM People PINNER JOIN NAMES P1ON P.FirstName + P.LastName >= P1.FirstName + P1.LastNameGROUP BY N.FirstName, N.LastNameORDER BY 1
butlermi_11
Starting Member
10 Posts
Posted - 2002-01-29 : 18:57:22
Here is another way to write your query using an inner join. SELECT RANK=Count(*), P.FirstName, P.LastNameFROM People PINNER JOIN NAMES P1ON P.FirstName + P.LastName >= P1.FirstName + P1.LastNameGROUP BY N.FirstName, N.LastNameORDER BY 1