- SQL Server Reference Guide
- Introduction
- SQL Server Reference Guide Overview
- Table of Contents
- Microsoft SQL Server Defined
- SQL Server Editions
- SQL Server Access
- Informit Articles and Sample Chapters
- Online Resources
- Microsoft SQL Server Features
- SQL Server Books Online
- Clustering Services
- Data Transformation Services (DTS) Overview
- Replication Services
- Database Mirroring
- Natural Language Processing (NLP)
- Analysis Services
- Microsot SQL Server Reporting Services
- XML Overview
- Notification Services for the DBA
- Full-Text Search
- SQL Server 2005 - Service Broker
- Using SQL Server as a Web Service
- SQL Server Encryption Options Overview
- SQL Server 2008 Overview
- SQL Server 2008 R2 Overview
- SQL Azure
- The Utility Control Point and Data Application Component, Part 1
- The Utility Control Point and Data Application Component, Part 2
- Microsoft SQL Server Administration
- The DBA Survival Guide: The 10 Minute SQL Server Overview
- Preparing (or Tuning) a Windows System for SQL Server, Part 1
- Preparing (or Tuning) a Windows System for SQL Server, Part 2
- Installing SQL Server
- Upgrading SQL Server
- SQL Server 2000 Management Tools
- SQL Server 2005 Management Tools
- SQL Server 2008 Management Tools
- SQL Azure Tools
- Automating Tasks with SQL Server Agent
- Run Operating System Commands in SQL Agent using PowerShell
- Automating Tasks Without SQL Server Agent
- Storage – SQL Server I/O
- Service Packs, Hotfixes and Cumulative Upgrades
- Tracking SQL Server Information with Error and Event Logs
- Change Management
- SQL Server Metadata, Part One
- SQL Server Meta-Data, Part Two
- Monitoring - SQL Server 2005 Dynamic Views and Functions
- Monitoring - Performance Monitor
- Unattended Performance Monitoring for SQL Server
- Monitoring - User-Defined Performance Counters
- Monitoring: SQL Server Activity Monitor
- SQL Server Instances
- DBCC Commands
- SQL Server and Mail
- Database Maintenance Checklist
- The Maintenance Wizard: SQL Server 2000 and Earlier
- The Maintenance Wizard: SQL Server 2005 (SP2) and Later
- The Web Assistant Wizard
- Creating Web Pages from SQL Server
- SQL Server Security
- Securing the SQL Server Platform, Part 1
- Securing the SQL Server Platform, Part 2
- SQL Server Security: Users and other Principals
- SQL Server Security – Roles
- SQL Server Security: Objects (Securables)
- Security: Using the Command Line
- SQL Server Security - Encrypting Connections
- SQL Server Security: Encrypting Data
- SQL Server Security Audit
- High Availability - SQL Server Clustering
- SQL Server Configuration, Part 1
- SQL Server Configuration, Part 2
- Database Configuration Options
- 32- vs 64-bit Computing for SQL Server
- SQL Server and Memory
- Performance Tuning: Introduction to Indexes
- Statistical Indexes
- Backup and Recovery
- Backup and Recovery Examples, Part One
- Backup and Recovery Examples, Part Two: Transferring Databases to Another System (Even Without Backups)
- SQL Profiler - Reverse Engineering An Application
- SQL Trace
- SQL Server Alerts
- Files and Filegroups
- Partitioning
- Full-Text Indexes
- Read-Only Data
- SQL Server Locks
- Monitoring Locking and Deadlocking
- Controlling Locks in SQL Server
- SQL Server Policy-Based Management, Part One
- SQL Server Policy-Based Management, Part Two
- SQL Server Policy-Based Management, Part Three
- Microsoft SQL Server Programming
- An Outline for Development
- Database
- Database Services
- Database Objects: Databases
- Database Objects: Tables
- Database Objects: Table Relationships
- Database Objects: Keys
- Database Objects: Constraints
- Database Objects: Data Types
- Database Objects: Views
- Database Objects: Stored Procedures
- Database Objects: Indexes
- Database Objects: User Defined Functions
- Database Objects: Triggers
- Database Design: Requirements, Entities, and Attributes
- Business Process Model Notation (BPMN) and the Data Professional
- Business Questions for Database Design, Part One
- Business Questions for Database Design, Part Two
- Database Design: Finalizing Requirements and Defining Relationships
- Database Design: Creating an Entity Relationship Diagram
- Database Design: The Logical ERD
- Database Design: Adjusting The Model
- Database Design: Normalizing the Model
- Creating The Physical Model
- Database Design: Changing Attributes to Columns
- Database Design: Creating The Physical Database
- Database Design Example: Curriculum Vitae
- NULLs
- The SQL Server Sample Databases
- The SQL Server Sample Databases: pubs
- The SQL Server Sample Databases: NorthWind
- The SQL Server Sample Databases: AdventureWorks
- The SQL Server Sample Databases: Adventureworks Derivatives
- UniversalDB: The Demo and Testing Database, Part 1
- UniversalDB: The Demo and Testing Database, Part 2
- UniversalDB: The Demo and Testing Database, Part 3
- UniversalDB: The Demo and Testing Database, Part 4
- Getting Started with Transact-SQL
- Transact-SQL: Data Definition Language (DDL) Basics
- Transact-SQL: Limiting Results
- Transact-SQL: More Operators
- Transact-SQL: Ordering and Aggregating Data
- Transact-SQL: Subqueries
- Transact-SQL: Joins
- Transact-SQL: Complex Joins - Building a View with Multiple JOINs
- Transact-SQL: Inserts, Updates, and Deletes
- An Introduction to the CLR in SQL Server 2005
- Design Elements Part 1: Programming Flow Overview, Code Format and Commenting your Code
- Design Elements Part 2: Controlling SQL's Scope
- Design Elements Part 3: Error Handling
- Design Elements Part 4: Variables
- Design Elements Part 5: Where Does The Code Live?
- Design Elements Part 6: Math Operators and Functions
- Design Elements Part 7: Statistical Functions
- Design Elements Part 8: Summarization Statistical Algorithms
- Design Elements Part 9:Representing Data with Statistical Algorithms
- Design Elements Part 10: Interpreting the Data—Regression
- Design Elements Part 11: String Manipulation
- Design Elements Part 12: Loops
- Design Elements Part 13: Recursion
- Design Elements Part 14: Arrays
- Design Elements Part 15: Event-Driven Programming Vs. Scheduled Processes
- Design Elements Part 16: Event-Driven Programming
- Design Elements Part 17: Program Flow
- Forming Queries Part 1: Design
- Forming Queries Part 2: Query Basics
- Forming Queries Part 3: Query Optimization
- Forming Queries Part 4: SET Options
- Forming Queries Part 5: Table Optimization Hints
- Using SQL Server Templates
- Transact-SQL Unit Testing
- Index Tuning Wizard
- Unicode and SQL Server
- SQL Server Development Tools
- The SQL Server Transact-SQL Debugger
- The Transact-SQL Debugger, Part 2
- Basic Troubleshooting for Transact-SQL Code
- An Introduction to Spatial Data in SQL Server 2008
- Performance Tuning
- Performance Tuning SQL Server: Tools and Processes
- Performance Tuning SQL Server: Tools Overview
- Creating a Performance Tuning Audit - Defining Components
- Creating a Performance Tuning Audit - Evaluation Part One
- Creating a Performance Tuning Audit - Evaluation Part Two
- Creating a Performance Tuning Audit - Interpretation
- Creating a Performance Tuning Audit - Developing an Action Plan
- Understanding SQL Server Query Plans
- Performance Tuning: Implementing Indexes
- Performance Monitoring Tools: Windows 2008 (and Higher) Server Utilities, Part 1
- Performance Monitoring Tools: Windows 2008 (and Higher) Server Utilities, Part 2
- Performance Monitoring Tools: Windows System Monitor
- Performance Monitoring Tools: Logging with System Monitor
- Performance Monitoring Tools: User Defined Counters
- General Transact-SQL (T-SQL) Performance Tuning, Part 1
- General Transact-SQL (T-SQL) Performance Tuning, Part 2
- General Transact-SQL (T-SQL) Performance Tuning, Part 3
- Performance Monitoring Tools: An Introduction to SQL Profiler
- Performance Tuning: Introduction to Indexes
- Performance Monitoring Tools: SQL Server 2000 Index Tuning Wizard
- Performance Monitoring Tools: SQL Server 2005 Database Tuning Advisor
- Performance Monitoring Tools: SQL Server Management Studio Reports
- Performance Monitoring Tools: SQL Server 2008 Activity Monitor
- The SQL Server 2008 Management Data Warehouse and Data Collector
- Performance Monitoring Tools: Evaluating Wait States with PowerShell and Excel
- Practical Applications
- Choosing the Back End
- The DBA's Toolbox, Part 1
- The DBA's Toolbox, Part 2
- Scripting Solutions for SQL Server
- Building a SQL Server Lab
- Using Graphics Files with SQL Server
- Enterprise Resource Planning
- Customer Relationship Management (CRM)
- Building a Reporting Data Server
- Building a Database Documenter, Part 1
- Building a Database Documenter, Part 2
- Data Management Objects
- Data Management Objects: The Server Object
- Data Management Objects: Server Object Methods
- Data Management Objects: Collections and the Database Object
- Data Management Objects: Database Information
- Data Management Objects: Database Control
- Data Management Objects: Database Maintenance
- Data Management Objects: Logging the Process
- Data Management Objects: Running SQL Statements
- Data Management Objects: Multiple Row Returns
- Data Management Objects: Other Database Objects
- Data Management Objects: Security
- Data Management Objects: Scripting
- Powershell and SQL Server - Overview
- PowerShell and SQL Server - Objects and Providers
- Powershell and SQL Server - A Script Framework
- Powershell and SQL Server - Logging the Process
- Powershell and SQL Server - Reading a Control File
- Powershell and SQL Server - SQL Server Access
- Powershell and SQL Server - Web Pages from a SQL Query
- Powershell and SQL Server - Scrubbing the Event Logs
- SQL Server 2008 PowerShell Provider
- SQL Server I/O: Importing and Exporting Data
- SQL Server I/O: XML in Database Terms
- SQL Server I/O: Creating XML Output
- SQL Server I/O: Reading XML Documents
- SQL Server I/O: Using XML Control Mechanisms
- SQL Server I/O: Creating Hierarchies
- SQL Server I/O: Using HTTP with SQL Server XML
- SQL Server I/O: Using HTTP with SQL Server XML Templates
- SQL Server I/O: Remote Queries
- SQL Server I/O: Working with Text Files
- Using Microsoft SQL Server on Handheld Devices
- Front-Ends 101: Microsoft Access
- Comparing Two SQL Server Databases
- English Query - Part 1
- English Query - Part 2
- English Query - Part 3
- English Query - Part 4
- English Query - Part 5
- RSS Feeds from SQL Server
- Using SQL Server Agent to Monitor Backups
- Reporting Services - Creating a Maintenance Report
- SQL Server Chargeback Strategies, Part 1
- SQL Server Chargeback Strategies, Part 2
- SQL Server Replication Example
- Creating a Master Agent and Alert Server
- The SQL Server Central Management System: Definition
- The SQL Server Central Management System: Base Tables
- The SQL Server Central Management System: Execution of Server Information (Part 1)
- The SQL Server Central Management System: Execution of Server Information (Part 2)
- The SQL Server Central Management System: Collecting Performance Metrics
- The SQL Server Central Management System: Centralizing Agent Jobs, Events and Scripts
- The SQL Server Central Management System: Reporting the Data and Project Summary
- Time Tracking for SQL Server Operations
- Migrating Departmental Data Stores to SQL Server
- Migrating Departmental Data Stores to SQL Server: Model the System
- Migrating Departmental Data Stores to SQL Server: Model the System, Continued
- Migrating Departmental Data Stores to SQL Server: Decide on the Destination
- Migrating Departmental Data Stores to SQL Server: Design the ETL
- Migrating Departmental Data Stores to SQL Server: Design the ETL, Continued
- Migrating Departmental Data Stores to SQL Server: Attach the Front End, Test, and Monitor
- Tracking SQL Server Timed Events, Part 1
- Tracking SQL Server Timed Events, Part 2
- Patterns and Practices for the Data Professional
- Managing Vendor Databases
- Consolidation Options
- Connecting to a SQL Azure Database from Microsoft Access
- SharePoint 2007 and SQL Server, Part One
- SharePoint 2007 and SQL Server, Part Two
- SharePoint 2007 and SQL Server, Part Three
- Querying Multiple Data Sources from a Single Location (Distributed Queries)
- Importing and Exporting Data for SQL Azure
- Working on Distributed Teams
- Professional Development
- Becoming a DBA
- Certification
- DBA Levels
- Becoming a Data Professional
- SQL Server Professional Development Plan, Part 1
- SQL Server Professional Development Plan, Part 2
- SQL Server Professional Development Plan, Part 3
- Evaluating Technical Options
- System Sizing
- Creating a Disaster Recovery Plan
- Anatomy of a Disaster (Response Plan)
- Database Troubleshooting
- Conducting an Effective Code Review
- Developing an Exit Strategy
- Data Retention Strategy
- Keeping Your DBA/Developer Job in Troubled Times
- The SQL Server Runbook
- Creating and Maintaining a SQL Server Configuration History, Part 1
- Creating and Maintaining a SQL Server Configuration History, Part 2
- Creating an Application Profile, Part 1
- Creating an Application Profile, Part 2
- How to Attend a Technical Conference
- Tips for Maximizing Your IT Budget This Year
- The Importance of Blue-Sky Planning
- Application Architecture Assessments
- Transact-SQL Code Reviews, Part One
- Transact-SQL Code Reviews, Part Two
- Cloud Computing (Distributed Computing) Paradigms
- NoSQL for the SQL Server Professional, Part One
- NoSQL for the SQL Server Professional, Part Two
- Object-Role Modeling (ORM) for the Database Professional
- Business Intelligence
- BI Explained
- Developing a Data Dictionary
- BI Security
- Gathering BI Requirements
- Source System Extracts and Transforms
- ETL Mechanisms
- Business Intelligence Landscapes
- Business Intelligence Layouts and the Build or Buy Decision
- A Single Version of the Truth
- The Operational Data Store (ODS)
- Data Marts – Combining and Transforming Data
- Designing Data Elements
- The Enterprise Data Warehouse — Aggregations and the Star Schema
- On-Line Analytical Processing (OLAP)
- Data Mining
- Key Performance Indicators
- BI Presentation - Client Tools
- BI Presentation - Portals
- Implementing ETL - Introduction to SQL Server 2005 Integration Services
- Building a Business Intelligence Solution, Part 1
- Building a Business Intelligence Solution, Part 2
- Building a Business Intelligence Solution, Part 3
- Tips and Troubleshooting
- SQL Server and Microsoft Excel Integration
- Tips for the SQL Server Tools: SQL Server 2000
- Tips for the SQL Server Tools – SQL Server 2005
- Transaction Log Troubles
- SQL Server Connection Problems
- Orphaned Database Users
- Additional Resources
- Tools and Downloads
- Utilities (Free)
- Tool Review (Free): DBDesignerFork
- Aqua Data Studio
- Microsoft SQL Server Best Practices Analyzer
- Utilities (Cost)
- Quest Software's TOAD for SQL Server
- Quest Software's Spotlight on SQL Server
- SQL Server on Microsoft's Virtual PC
- Red Gate SQL Bundle
- Microsoft's Visio for Database Folks
- Quest Capacity Manager
- SQL Server Help
- Visual Studio Team Edition for Database Professionals
- Microsoft Assessment and Planning Solution Accelerator
- Aggregating Server Data from the MAPS Tool
Modern programming languages use multiple types of loops, each with its advantages and appropriate uses. Transact-SQL is a bit more limited, with only one or two true looping commands. The reason is pretty straightforward - looping activities in a database have to do with data, not normally program logic. In today's tutorial, I’ll examine a couple of simple loops, and then move on to the row operations in which SQL Server has a little more advantage.
For the true looping code, there's really only one command in T-SQL, called WHILE. Here's an example:
/* First Loop Example */ DECLARE @a INT SET @a = 1 WHILE @a < 11 BEGIN SELECT @a SET @a = @a + 1 END PRINT 'All Done'
Notice that I've declared an integer variable in the first line, and set it to 1 in the next. This is nothing you haven't probably seen before in any beginning programming book.
In the WHILE command in the third line, I tell the computer to perform the block below while the variable of @a is less than 11. I have a BEGIN and END block set up, and between them lie the commands to run for the duration of the WHILE logic. In this simple example, I've merely selected the value to the screen.
The important part is to increment the value of the variable by 1, so that it will eventually reach 11, and then get out of the loop. I selected 11 for the WHILE condition, because I want the system to count to 10. Without incrementing, I'd never reach 11, and I'd never exit the loop. SQL Server is really not especially good at breaking out of loops; do one of them wrong, and it may be necessary to kill the ID of the process that is running it.
This is a simple loop, but the logic holds for more complex work. You can nest these kinds of loops, but more than three or four loops become inefficient. As a matter of fact, if you end up with more than three or four, it's time to examine the code to see if the goal can be accomplished in another way.
There's another way to do a loop, but it's not technically the same thing. In the next example, I’ll explain another T-SQL construct called a label. A label is simply a marker for a block of code. Here's the example:
/* Second Loop Example */ DECLARE @b INT SET @b = 1 test_loop: BEGIN SELECT @b SET @b = @b + 1 END IF @b < 11 GOTO test_loop GO
This type of GOTO-based looping is similar to old BASIC code, and I haven't seen a lot of it in use recently.
Those are the only true loops you have in T-SQL. Other database platforms implement more looping keywords, but in reality they aren't often necessary. Most looping tasks are done using other development languages, and the kind of repetitive task we're after in T-SQL is often not really a loop at all.
What that means is that I often want to pull in some data, and work with it a row at a time. You might think of this as a loop, but it's actually something called a row-based set. T-SQL is normally data set-based, not row based, and so I think of these row-based operations as a kind of loop.
OK, back to Transact-SQL — to handle row-based operations, T-SQL provides a construct call a Cursor. Cursors are just row-based operation commands, and they aren't difficult to learn at all. Here's the basic outline for creating a cursor:
- Declare the cursor and fill it
- Open it
- Fetch from it
- Close the cursor and deallocate it
Cursors' purpose will become more apparent as I show you a few. I'll begin this topic in this tutorial, but it has so many facets that you'll see it again in future installments. Here, I’ll focus more on creating and accessing the cursor. In future tutorials, I'll put them to more complex use.
Let me step through the process of creating a simple cursor. First, I declare the cursor, much like the variables you've seen before, and then I’ll fill it with some data:
DECLARE test_cursor CURSOR FOR SELECT * FROM authorsPretty simple, but it gets a bit deeper from here. There are many options available, in just this first part of the cursor, so you can just pause here to look at those options. Don't worry if they seem a bit confusing at the moment; just read through them and you'll apply them later.
First, the cursor's name is how I’ll refer to it throughout the process. The name can have from 1 to 128 characters.
The first option on the DECLARE name CURSOR is LOCAL. This option specifies that the cursor is available only in the batch, stored procedure, or trigger in which the cursor was created. The LOCAL cursor is implicitly deallocated when the batch, stored procedure, or trigger terminates. (This saves you a step, later on.)
The alternate to LOCAL is GLOBAL. If you set the cursor to GLOBAL, the cursor would available to the entire connection, not just its batch of code. The GLOBAL cursor is implicitly deallocated at disconnect.
The next set of options deal with how data is streamed from the server. FORWARD_ONLY specifies that the cursor will only fetch data sequentially from the first to the last row. If you use this option, then FETCH NEXT is the only fetch option supported (you’ll see more about this in a moment).
The option with the most relevance to data integrity is STATIC. This option means that the cursor will use a temporary copy of the data instead of the base tables. When set up this way, the cursor does not allow modifications; modifications made to base tables are not reflected in the data returned by fetches made to this cursor. This is sort of a "dirty read," since it may not reflect up-to-the-moment data changes. Use with caution!
The KEYSET option isn't used a great deal, but can be quite important. KEYSET ensures that the cursor uses the set of keys that uniquely identify the cursor's rows (keyset), so that the membership and the order of rows in the cursor are fixed when the cursor is opened. The KEYSET cursor keeps updates to non-key values from being made through this cursor. One side effect is that inserts made by other connections are not always visible.
The DYNAMIC option sets the cursor to reflect all data changes made to the base tables as you scroll through the cursor. The side effect here is that you can't use the FETCH ABSOLUTE option if you use a DYNAMIC cursor.
Here's an option you’ll see a lot more of: FAST_FORWARD. This type of cursor will be a FORWARD_ONLY and a READ_ONLY cursor. FAST_FORWARD cursors produce the least amount of overhead on SQL Server, but allow potential dirty reads.
Similar to FAST_FORWARD, a READ ONLY option means that the cursor cannot be updated.
If you're after a little heavier hand, the SCROLL_LOCKS option will lock the rows as they are read into the cursor to ensure that updates or deletes made through the cursor will succeed.
Another popular option, OPTIMISTIC, doesn't lock rows as they are read into the cursor. Instead, the updates or deletes made through the cursor will not succeed if the row is updated outside the cursor.
You probably won't run into the TYPE_WARNING option often. This option means that a warning message should be sent to the client if the cursor is converted from the requested type to another.
Those are most of the options on the DECLARE name CURSOR. In the SELECT statement, the primary things to know about a cursor are that the T-SQL cannot contain the COMPUTE, COMPUTE BY, FOR BROWSE, and INTO keywords.
The final option is UPDATE [OF column_name [,...n]]. This option means that all the cursor's columns can be updated, or only the columns listed in the OF column_name [,...n] list will allow modifications.
So those are the primary options I’ll use. Next, I open the cursor to work with it:
OPEN test_cursor
I "open" a cursor to work with the data. That action makes the contents of the cursor the current set we're working with.
Finally, I get the data and work with it:
FETCH NEXT FROM test_cursor
This is the row-based bit I’ve been looking for. I work with the cursor one line at a time. Here's the code all in one place, with the results:
DECLARE test_cursor CURSOR FOR SELECT * FROM authors OPEN test_cursor FETCH NEXT FROM test_cursor FETCH NEXT FROM test_cursor FETCH NEXT FROM test_cursor DEALLOCATE test_cursor ----------------------------------- au_id au_lname au_fname phone address city state zip contract ----------- ---------------------------------------- -------------------- ------------ ---------------------------------------- -------------------- ----- ----- -------- 172-32-1176 White Johnson 408 496-7223 10932 Bigge Rd. Menlo Park CA 94025 1 (1 row(s) affected) au_id au_lname au_fname phone address city state zip contract ----------- ---------------------------------------- -------------------- ------------ ---------------------------------------- -------------------- ----- ----- -------- 213-46-8915 Green Marjorie 415 986-7020 309 63rd St. #411 Oakland CA 94618 1 (1 row(s) affected) au_id au_lname au_fname phone address city state zip contract ----------- ---------------------------------------- -------------------- ------------ ---------------------------------------- -------------------- ----- ----- -------- 238-95-7766 Carson Cheryl 415 548-7723 589 Darwin Ln. Berkeley CA 94705 1 (1 row(s) affected)
I FETCHed three lines, so I can see the iteration. Notice I added one more line; the cleanup bit is DEALLOCATE.
There’s actually another way to work over the same set of data, introduced later in SQL Server 2005’s Transact-SQL. The feature is called a Common Table Expression, or CTE. A CTE is a way of setting some data aside in memory (from a query), and then being able to work over that set of data, similar to a table variable.
Where this construct becomes more as a looping function is when it is used recursively - a process where a function can work on the function itself. Since this is a larger topic, I’ll leave it for another article, but there are two references (here: http://msdn.microsoft.com/en-us/library/ms190766.aspx and here: http://msdn.microsoft.com/en-us/library/ms186243.aspx) that you can use in the meantime. CTE’s are one of the most powerful ways to work over a set of data, but are not as suited to classical looping and incrementing functionality.