Chapter 11. Saving Time with Functions, Stored Procedures, and Triggers

In Chapters 4 through 6, I describe many of the powerful features of SQL Server and Transact-SQL. That discussion focuses on writing Transact-SQL statements that explicitly tell SQL Server how you'd like the database to react. In the real world, SQL statements can become quite complex, taking dozens of lines (or longer!) to fully express a complex query.

SQL Server provides several features to help you manage this complexity and simplify your Transact-SQL statements. In this chapter, I describe how you can use functions, stored procedures, and triggers to streamline your SQL statements, reuse code, and improve database and application security.

Reusing Logic with Functions

Functions allow you to reuse common functionality, saving you the time and trouble of cutting and pasting (or rewriting!) SQL code that you use often. Before diving in to SQL Server's functions, I give you a brief example of the way you might use functions in the real world.


If you're a programmer, you're probably already familiar with the concept of a function. SQL Server functions are no different than those used by any other programming language.

Imagine that you're the supervisor at a vehicle depot and have several employees working for you who assist you in the management of a fleet of hundreds of vehicles. Before issuing a vehicle to a customer, you send an employee to verify that the car has enough gas. That employee might take the following ...

Get Microsoft® SQL Server® 2008 For Dummies® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.