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.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Using cursor for this purpose

Author  Topic 

bi89405
Starting Member

35 Posts

Posted - 2013-05-16 : 11:09:14
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
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-05-16 : 11:21:43
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
   

- Advertisement -