Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: SQL
Expertise: Advanced
Aug 2, 2005

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_Value
1    '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 Cursor
Fetch @Char_Value

Update table_A
Set Char_Col =
   Case
   When DataLength(LTrim(Char_Col)) > 0 Then 
        Char_Col + ',' + @Char_Value
   Else @Char_Value
   End
Where Condition
 
End Cursor
Srivastava Alok
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date