devxlogo

Combining a Conditional Update with a Single Statement in a Cursor

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
See also  Professionalism Starts in Your Inbox: Keys to Presenting Your Best Self in Email
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist