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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Register Now and get your question answered!
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Using cursor for this purpose
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

35 Posts

Posted - 05/16/2013 :  11:09:14  Show Profile  Reply with Quote

I have a table that stores descriptions of an index in multiple lines. Each line is numbered sequentially. I would like to use a cursor that will be able to write the descriptions in a single row. Here is an example:

IndexNo LineNo Description
1 1 Joe went to school
1 2 today and come home
1 3 at 3pm.

The goal of the output should be:

IndexNo Description
1 Joe went to school today and come home at 3pm.


James K
Flowing Fount of Yak Knowledge

3873 Posts

Posted - 05/16/2013 :  11:21:43  Show Profile  Reply with Quote
Don't use cursor. That is going to be slow. Do it like shown below
	LTRIM( (SELECT ' '+[Description] FROM Tbl t2
		WHERE t2.[Index] = t1.[Index]
		ORDER BY [LineNo]
	)) AS [Description]
	(SELECT DISTINCT [Index] FROM  Tbl) t1;
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.02 seconds. Powered By: Snitz Forums 2000