TIP: Row by Row operations without cursors

In general, cursors suck ass! Ok, that’s a bit extreme, but I have a long and ugly history with cursors. Let me diverge here and tell you a true story.

A while ago, a friend of mine recommended me to a company in serious need of senior developers for full-time or contract work. After talking to the dev manager over the phone, he felt my rate was too high, but wanted me to come in anyways. He set his top three developers in the room with me and left as they began to drill me with technical questions.

Now, I don’t mind being asked difficult technical questions in an interview. In fact, I think it’s a necessary part of an interview. But it was clear from the outset that these three hadn’t set their egos aside and they were quite antagonistic. One of them asked me the following question.

Suppose you have a table named tblUser with column named FirstName. On the whiteboard, construct a query that will select all the first names into a single varchar with commas. I asked if I may assume that the list of names will fit in a VARCHAR 8000, to which they replied yes. So I promptly wrote the following on the board.

DECLARE @FirstNames VARCHAR(8000)
SET @FirstNames = '' 
SELECT @FirstNames = FirstNames + ',' + @FirstNames
FROM tblUser

With disdain on their faces, they shook their heads and said no. No! Well of course they did, they were looking for a cursor answer. They wanted to know if I could write a cursor. Needless to say, I didn’t get the job, but after the fact, I couldn’t help sending the manager an email informing him that not only was my answer correct, but it was 1000 times faster than the answer they wrote on the board. Yes, I’m still bitter. ;)

Which brings me back to the point of this post. If you can avoid a cursor solution, by all means do. The following article describes a technique for performing row by row operations without using a cursor. It makes several assumptions about your table, but for the most part, this is very useful.

What others have said

Requesting Gravatar... Rob Conery Mar 02, 2007 9:19 AM
# re: TIP: Row by Row operations without cursors
Hey I travelled back 3 years and learned something! Nice post Phil - but how would this work with an OUTER JOIN?
Requesting Gravatar... Shan Plourde Mar 02, 2007 1:27 PM
# re: TIP: Row by Row operations without cursors
Hi Phil, I use this type of technique all the time too. BUT your solution will always leave a comma at the end of the string ;) This could have been the deal breaker for that senior job ;)

DECLARE @FirstNames VARCHAR(8000)
SET @FirstNames = ''
SELECT top 10 @FirstNames = @firstNames + case len(@firstnames) when 0 then '' else ',' end + FirstName
FROM tblnames
print @firstnames
Requesting Gravatar... Haacked Mar 03, 2007 6:50 AM
# re: TIP: Row by Row operations without cursors
Heh heh. Hi Shan, in the actual solution in the interview, I remembered to truncate the comma. I just wanted to present the gist of the solution here.
Requesting Gravatar... Adam May 02, 2007 9:00 AM
# re: TIP: Row by Row operations without cursors
Great post, thanks!!! I was looking for a solution that wouldn't require me to use a cursor.
Requesting Gravatar... Shan May 23, 2007 12:36 AM
# re: TIP: Row by Row operations without cursors
Rob, wouldn't IsNull(column, '') work?
Requesting Gravatar... DW Feb 24, 2009 11:31 PM
# re: TIP: Row by Row operations without cursors
Thanks for the post. I liked the solution. Although I ran into a problem. I tried to implement it with VARCHAR(8000) for the resulting string, and the result came out truncated. It seems that using the technique you suggested you can't get the string with more than 4000 characters. Whereas with the cursor I got maximum 8000 characters in the resulting string.
Please advise

Very much appreciated
DW
Requesting Gravatar... Andrei Rinea Apr 13, 2010 7:45 AM
# re: TIP: Row by Row operations without cursors
It was the egos and your rate. It bit me too in the past, but at least they were honest and told me that no other developer was paid that well and my requested rate was way over that.
Requesting Gravatar... loupus Jul 20, 2010 10:45 PM
# re: TIP: Row by Row operations without cursors
a brilliant solution, but the article is no more, wish to read it
Requesting Gravatar... Martin V Dec 20, 2011 7:19 PM
# re: TIP: Row by Row operations without cursors
Hi,
I'm not so deep into SQL, so I might have missed something here. But shouldn't @FirstName

in the line:
SELECT @FirstName = FirstName + ',' + @FirstName

actually be @FirstNames? (i.e. the thing that was declared at the beginning as: DECLARE @FirstNames VARCHAR(8000))
Thanks for clarifying.
Requesting Gravatar... haacked Dec 21, 2011 1:40 AM
# re: TIP: Row by Row operations without cursors
@Martin you're RIGHT! Wow, this post has been up 7 years without anyone noticing the typo. :)
Requesting Gravatar... DnshPly9 Apr 27, 2012 7:15 AM
# re: TIP: Row by Row operations without cursors
Ha ha ha!
Seriously I can't stop laughing at the three best guys.
I wonder such silly things do happen (even at Senior developer level).
Really enjoyed reading!!!
Nice post :)

What do you have to say?

(will show your gravatar)
Please add 1 and 4 and type the answer here: