dcsimg
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

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


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

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


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