Combining a Conditional Update with a Single Statement in a Cursor

Oftentimes, cursors are used to concatenate multiple columns into one string. For instance, you may want something like this to show as ‘A,B,C’ on your report (Table table_A):

Id   Char_Value1    'A'2    'B'3    'C'

In a cursor, rather than checking for values and using an exists condition and having an if/else block, you can accomplish the same result using a case statement.

The following example is for Sybase/SQL Server:

Begin CursorFetch @Char_ValueUpdate table_ASet Char_Col =   Case   When DataLength(LTrim(Char_Col)) > 0 Then         Char_Col + ',' + @Char_Value   Else @Char_Value   EndWhere Condition End Cursor
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: