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

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

bi89405
Starting Member

USA
35 Posts

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

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.

TIA,
ZH

James K
Flowing Fount of Yak Knowledge

3742 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
SELECT
	[Index],
	LTRIM( (SELECT ' '+[Description] FROM Tbl t2
		WHERE t2.[Index] = t1.[Index]
		ORDER BY [LineNo]
		FOR XML PATH('')
	)) AS [Description]
FROM
	(SELECT DISTINCT [Index] FROM  Tbl) t1;
Go to Top of Page
  Previous Topic Topic Next Topic  
 New 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.06 seconds. Powered By: Snitz Forums 2000