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 |
galbrecht
Starting Member
17 Posts |
Posted - 2008-02-24 : 19:39:43
|
Hi I have a cursor wich works, but it seems to t=be truncating some of the data. There are two columns that should have 6 sets of adta in it but it only puts in four.This is the cursor----------------------------------------------------------------Openand fetch the data from the cursor--------------------------------------------------------------OPEN Dr_DogCommentsSearchCursorFETCH NEXT FROM Dr_DogCommentsSearchCursor INTO @dg_serial ,@dg_name,@Text_ID,@date_added,@owner, @Text_INWHILE @@FETCH_STATUS = 0BEGIN --check for nullsSET @CurrentReg = @dg_serialIF @CurrentReg = @PreviousRegBEGIN UPDATE dbo.tbl_Oz_DR_DOG_COMMENTS_ASETdg_serial = @dg_serial,dg_name = @dg_name,Text_ID = Text_ID+'**'+CAST(@Text_ID AS VARCHAR),date_added = date_added+'**'+CAST(@date_added AS VARCHAR),owner = owner+'**'+CAST(@owner AS VARCHAR),Text_IN = Text_IN+'**'+CAST(@Text_IN AS VARCHAR)WHERE dg_serial = @CurrentReg-- get the next record from the cursorFETCH NEXT FROM Dr_DogCommentsSearchCursor INTO@dg_serial ,@dg_name,@Text_ID,@date_added,@owner, @Text_INSET @PreviousReg = @CurrentRegENDELSE -- do an insert (create new record)BEGIN --set the previous reg and insert the dataSET @PreviousReg = @CurrentRegINSERT INTO dbo.tbl_Oz_DR_DOG_COMMENTS_A(dg_serial ,dg_name,Text_ID,date_added,owner, Text_IN) VALUES (@dg_serial ,@dg_name,@Text_ID,@date_added,@owner, @Text_IN) -- get the next record from the cursorFETCH NEXT FROM Dr_DogCommentsSearchCursor INTO @dg_serial ,@dg_name,@Text_ID,@date_added,@owner, @Text_INEND END------------------------------------------------------------------------Close and deallocate the cursor ----------------------------------------------------------------------CLOSE Dr_DogCommentsSearchCursor DEALLOCATE Dr_DogCommentsSearchCursorGOThis is the output that is incorrect34306,WEHO,8239**7953**6651**6118,22 Jan 2007**06 Oct 2006**16 Mar 2006**02 Aug 2005,juddyer **rebhart **rebhart **tinscot it sould be34306,WEHO ,8239**7953**6651**6118**????**????,22 Jan 2007**06 Oct 2006**16 Mar 2006**02 Aug 2005**????? **?????,juddyer **rebhart **rebhart **tinscot **?????? **???????Can anyone tell what is wrong with this cursor?ThanksGreg |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-25 : 04:21:28
|
Can you explain what you are trying to achieve here? |
|
|
galbrecht
Starting Member
17 Posts |
Posted - 2008-02-25 : 20:11:15
|
Hi,I have a table that has dog registrations (dog data). The table has a field for text notes that can be added to a dog. Each dog can have more than one text note and each time you enter a note it gets a text id and the date that the note was added.I have extracted the dog id, text note id, date and the text note it self. So then I end up with a dog that has 6 rows of data (as it has 6 notes). The cursor is to combine the six rows into one with a ** to seperate each text id, date and note in the fields.Like this dg_serial,dg_name,ext_ID,date_added,owner, Text_IN34306,WEHO,8239**7953**6651**6118,22 Jan 2007**06 Oct 2006**16 Mar 2006**02 Aug 2005,juddyer **rebhart **rebhart **tinscot Does this make sense?thanksGreg |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-26 : 03:19:46
|
You can do it like thisDECLARE @txtIDStr varchar(1000),@dateStr varchar(1000),@txtStr varchar(1000)UPDATE dgSET @txtIDStr=dg.text_ID = COALESCE(@txtIDStr,'')+ CASE WHEN @txtIDStr IS NOT NULL THEN '**' END+ CAST(txt.textnoteid AS varchar(10)),@dateStr=dg.date_added= COALESCE(@dateStr,'')+ CASE WHEN @txtIDStr IS NOT NULL THEN '**' END+ CAST(txt.date AS varchar(11)),@txtStr=dg.text= COALESCE(@txtStr,'')+ CASE WHEN @txtIDStr IS NOT NULL THEN '**' END+ txt.textnote FROM dogdata dgINNER JOIN textnotetable txton txt.dogid=dg.dogid i'm assuming your textnote data is in textnotetable and also you have 1 record per dog in dog data |
|
|
|
|
|
|
|