Skip to main content

Conditional Processing, Control-of-Flow, and Cursors

  • Chapter
  • 809 Accesses

Abstract

In this chapter, I’ll present recipes that demonstrate SQL Server Transact-SQL for

  • Conditional processing: You’ll learn how to use the CASE and IF...ELSE statements to evaluate conditions and return values accordingly. I’ll review how to use the CASE function to evaluate a single input expression and return a value, and also how to evaluate one ormore Boolean expressions. Finally, I’ll demonstrate returning a value when the expressions are TRUE.

  • Control-of-flow functionality: This recipe demonstrates how to control the execution of Transact-SQL statements or batches based on commands such as RETURN, WHILE, WAITFOR, and GOTO. RETURN is used to exit the current Transact-SQL batch immediately, and doesn’t allow any code in the batch that executes after it. The WHILE command is used to repeat a specific operation or batch of operations while a condition remains TRUE. The WAITFOR command is used to delay the execution of Transact-SQL code for a specified length of time or until a specific time. GOTO is used to jump to a label in your Transact-SQL batch, passing over the code that follows it.

  • Creating and using cursors: Here, I’ll demonstrate Transact-SQL cursors, which allow you to work with one row at a time. Based onmy experiences in the field, I’mnot a big fan of cursors. Cursors can cause significant performance problems due to excessive singleton row calls,memory consumption, and code bloat issues when not implemented correctly. However, there stillmay be rare occasions when the use of a cursor is a better choice than a set-based solution.

An understanding of how and when (and when not) to use these techniques will allow you to create flexible and intelligent Transact-SQL code.

This is a preview of subscription content, log in via an institution.

Buying options

Chapter
USD   29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD   39.99
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD   54.99
Price excludes VAT (USA)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Learn about institutional subscriptions

Preview

Unable to display preview. Download preview PDF.

Unable to display preview. Download preview PDF.

Rights and permissions

Reprints and permissions

Copyright information

© 2008 Joseph Sack

About this chapter

Cite this chapter

(2008). Conditional Processing, Control-of-Flow, and Cursors. In: SQL Server 2008 Transact-SQL Recipes. Apress. https://doi.org/10.1007/978-1-4302-0625-5_9

Download citation

Publish with us

Policies and ethics