T-SQL: Cursor

Please see my other Database Development articles.

Performing iterative actions.

Update: SQL Server 2008 introduced a new feature to T-SQL, Common Table Expressions (CTEs) which is meant to replace the use of cursors. For more information read my WITH article.

Sometimes business requirements demands that complex actions must be performed on recordsets in such a manner that each row must be acted upon before proceeding to the next (SELECT, INSERT, etc.).

The following example demonstrates how to retrieve data from a table, store it into a temporary table, then return that data.

cursor_code Here are the results of the cursor.cursor_results

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s