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.
| Author |
Topic |
|
OldMySQLUser
Constraint Violating Yak Guru
301 Posts |
Posted - 2008-04-04 : 16:46:00
|
I have a table like:Converted_Mortgage_Number Activity_Date Activity_Time Sequence_Number, History_Text100 2006-01-20 84936 1 Sent a reminder letter105 2006-01-23 134502 1 Called, but no reply100 2006 01-24 104532 1 Asked to call again later100 2006-01-24 104532 2 when it is more convenient100 2006-01-24 104532 3 and the person I require is availableI wish to concantenate the History_text fields together for records with the same Converted_Mortgage_Number, Activity_Date and Activity_Time so:100 2006-10-24 104532 Asked to call again later when it is moreconvenient and the person I require is available.To do this I amusing nested cursors so:USE arGODECLARE @mortgage INT, @date datetime, @time INT, @getMortgage CURSOR, @notes varchar(MAX), @notesComplete varchar(MAX), @getDetail CURSORSET @getMortgage = CURSOR FOR SELECT Converted_Mortgage_Number, Activity_Date, Activity_Time FROM format_history GROUP BY Converted_Mortgage_Number, Activity_Date, Activity_Time ORDER BY Converted_Mortgage_Number ASCOPEN @getMortgage FETCH NEXT FROM @getMortgage INTO @mortgage, @date, @timeWHILE @@FETCH_STATUS = 0 BEGIN SET @notes = '' SET @getDetail = CURSOR FOR SELECT History_Text FROM format_history WHERE Converted_Mortgage_Number = @mortgage AND Activity_Date = @date AND Activity_Time = @time ORDER BY Sequence_Number OPEN @getDetail SET @notesComplete = '' FETCH NEXT FROM @getDetail INTO @notes WHILE (@@FETCH_STATUS = 0) BEGIN @notesComplete = @notesComplete + @notes + ' ' FETCH NEXT FROM @getDetail INTO @notes END PRINT @notesComplete CLOSE @getDetail DEALLOCATE @getDetail FETCH NEXT FROM @getMortgage INTO @mortgage, @date, @time ENDCLOSE @getMortgageDEALLOCATE @getMortgageGO But I am getting the error: 'Incorrect syntax near the keyword CLOSE, incorrect syntax near @notesComplete'.Can anyone please see where I am going wrong? |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-04-04 : 16:49:15
|
| Don't put the @ before the name of the cursor. Check BOL for cursor examples.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
ranganath
Posting Yak Master
209 Posts |
Posted - 2008-04-05 : 01:12:11
|
| Hi,Try with this Declare @T Table (Converted_Mortgage_Number Int, Activity_Date varchar(1000), Activity_Time Int, Sequence_Number Int, History_Text varchar(1000))Insert into @TSelect 100, '2006-01-20' ,84936 ,1, 'Sent a reminder letter' Union allSelect 105, '2006-01-23' ,134502 ,1, 'Called, but no reply' Union allSelect 100, '2006-01-24' ,104532 ,1, 'Asked to call again later' Union allSelect 100, '2006-01-24' ,104532 ,2, 'when it is more convenient' Union allSelect 100, '2006-01-24' ,104532 ,3, 'and the person I require is available' --Select * From @Tselect Converted_Mortgage_Number, Activity_Date, Activity_Time, STUFF( ( select ', '+ History_Text from @T A where A.Converted_Mortgage_Number = B.Converted_Mortgage_Numberand A.Activity_Date = B.Activity_Date and A.Activity_Time = B.Activity_Time for xml path('')),1,1,'')as 'History_Text'from @T BGroup By Converted_Mortgage_Number, Activity_Date, Activity_Time |
 |
|
|
OldMySQLUser
Constraint Violating Yak Guru
301 Posts |
Posted - 2008-04-05 : 07:44:23
|
quote: Originally posted by tkizer Don't put the @ before the name of the cursor. Check BOL for cursor examples.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/
Strange, as the 'outer' cursor works OK. It's just the inner loop I'm having trouble with.Can anyone who understands cursors see why this should be please? |
 |
|
|
OldMySQLUser
Constraint Violating Yak Guru
301 Posts |
Posted - 2008-04-05 : 08:09:07
|
| Holdon, I've just realised that I only need to replace@notesComplete = @notesComplete + @notes + ' 'withSET @notesComplete = @notesComplete + @notes + ' 'and it works fine.Don't know why I didn't see that last night when I first posted - tired I guess ... |
 |
|
|
|
|
|
|
|