Home > Articles > Databases > SQL Server

SQL Server Stored Procedure Basics

  • PrintPrint
  • Share ThisShare This
  • DiscussDiscuss
Learn the basics of writing stored procedures, monitor stored procedure activity using the Profiler utility, and learn from several real-world stored procedure programming issues. Ken Henderson also touches on many of the nuances and quirks in Transact-SQL, and how to use them to your advantage and/or how to work around them as appropriate.

This sample chapter is excerpted from The Guru's Guide to SQL Server Stored Procedures, XML, and HTML.

Today, average software development practices are becalmed in a windless sea of code-and-fix programming—a kind of flat-earth approach to software development that was proven ineffective 20 years ago.
—Steve McConnell1

Working from the assumption that the human brain learns by associating new data with what it already knows, we'll spend this chapter building a base framework onto which we can assemble the knowledge conveyed by the remainder of the book. We'll touch on the topics covered in the book's other chapters, but we'll save the details for the chapters themselves. I'm assuming that you know some basic Transact-SQL with which we can associate these high-level concepts. We'll spend the remainder of the book filling in the details and expanding on what we cover here.

This chapter serves to prime the discussion on SQL Server stored procedure programming. It will tell you what a stored procedure is, how stored procedures are often used, and why and how you should use them. It will also jumpstart the treatment of Transact-SQL as a full-fledged programming language. If I could have you take one thing away from reading this book, it would be that Transact-SQL programming is very much like any other type of programming: It requires the same attention to detail, the same craftsmanship, and the same software engineering skill to do well.

What Is a Stored Procedure?

A Transact-SQL stored procedure is a set of T-SQL code that is stored in a SQL Server database and compiled when used. You create this set of code using the CREATE PROCEDURE command. You can use most Transact-SQL commands in a stored procedure; however, some commands (such as CREATE PROCEDURE, CREATE VIEW, SET SHOWPLAN_TEXT, SET SHOWPLAN_ALL, and so forth) must be the first (or only) statement in a command batch, and therefore aren't allowed in stored procedures. Most Transact-SQL commands behave the same in a stored procedure as they do in a command batch, but some have special capabilities or exhibit different behavior when executed within the context of a stored procedure. Listing 1–1 shows a simple stored procedure (only the code from the CREATE PROCEDURE line down to the ensuing GO actually constitutes the stored procedure):

Listing 1–1 A simple stored procedure.

Use Northwind
GO
IF OBJECT_ID('dbo.ListCustomersByCity') IS NOT NULL
 DROP PROC dbo.ListCustomersByCity
GO
CREATE PROCEDURE dbo.ListCustomersByCity @Country nvarchar(30)='%'
AS
SELECT City, COUNT(*) AS NumberOfCustomers
FROM Customers
WHERE Country LIKE @Country
GROUP BY City
GO
EXEC dbo.ListCustomersByCity
  • Share ThisShare This
  • Your Account

Discussions

SQL Server2005
Posted Mar 3, 2008 02:29 AM by vegaiah2006
0 Replies
gswef
Posted Feb 4, 2008 04:25 AM by prajakta_rane09
1 Replies
how to pass parameters in stored procedures
Posted Jan 9, 2008 06:19 AM by vegaiah2006
1 Replies

Make a New Comment

You must log in in order to post a comment.

Related Resources

There are currently no related podcasts. Please check back later.

User Group Organizations: Finding Support in the Greater IT Community
By Emily Nave on July 29, 2010 1 Comment

Birds of a feather flock together, right? If you’re already a member of an established user group or looking for other like-minded technology evangelists, connecting with peers is an important part of being an active voice in the IT community.

Buck WoodyJust how permanent does that data need to be
By Buck Woody on July 29, 2010 No Comments

As data professionals, we plan for things. We plan for growth, we have a disaster-recovery plan, we have a plan for consolidation. Those are all good things. But I've seen very few shops that have a good data retention plan.

Buck WoodyThe Microsoft IT Showcase
By Buck Woody on July 28, 2010 No Comments

I had dinner in Seattle (three words: bacon-wrapped-steak) with the folks that put on "How Microsoft Does IT" (get it? Does "it", "IT"? OK, I'm easily amused) and I wanted to point out this fantastic resource to you - again

See All Related Blogs

Informit Network