Kevin Kline is the Technical Strategy Manager for SQL Server Solutions at Quest Software, a leading provider of award winning tools for database management and application monitoring tools. Kevin is also a founding board member and former President of the international Professional Association for SQL Server (PASS) and frequently contributes to database technology magazines, web sites, and discussion forums. Kevin's most popular book is SQL in a Nutshell published by O'Reilly Media. Kevin is also the author of monthly magazine columns for SQL Server Magazine and Database Trends & Applications. Kevin is a top rated speaker, appearing at international conferences like Microsoft TechEd, DevTeach, PASS, Microsoft IT Forum, and SQL Connections.
Lee Gould is a principal consultant with Sybase Professional Services in New York where she works mainly in the financial services industry on Wall Street. She has been working with SQL Server since 1992 and has been involved in the computer industry for over ten years. She has published numerous articles in Microsoft SQL Server Professional (Pinnacle Publishing) and presented on a variety of topics at the International Sybase User Group and Powersoft conferences. Lee was born in Liverpool, England. She grew up in Johannesburg, South Africa, where she attended the University of Witwatersrand and received her bachelors of commerce in business information systems. Lee immigrated to America four years ago and is currently pursuing an MBA at Seton Hall University in New Jersey. Lee lives in the suburb of North Brunswick, New Jersey, with her two cats, Lady and Paganini. She can often be seen running, cycling, swimming, and horseriding while she trains for marathons and ultra triathlon events. When not immersed in sport, Lee is an avid fan of classical music, good wine, the piano, science fiction and Terminator II. Her motto on life is "carpe diem," which she endeavors to follow regardless of where it leads her. Lee can be contacted at email@example.com.
Andrew Zanevsky, an independent consultant and founder and president of AZ Databases, Inc., has been working with SQL Server since 1992. He writes a monthly column "Super Administrator," for Microsoft SQL Server Professional (Pinnacle Publishing) and has published more than a hundred technical articles throughout his career. He immigrated to the United States from Minsk, Belarus in 1991. He started programming in 1982 and holds a degree from the Belarus State University of Informatics and Radioelectronics (equivalent to an M.S. in computer science). Andrew lives in a suburb of Chicago with his wife Katrin, son Anthony, and stepdaughter Esther. Katrin is also a SQL Server consultant. They work on some projects together and can talk about Transact-SQL at dinner. Andrew's daughter Nikkie lives in New York, wants to be a writer, and has won numerous awards in art contests. Andrew was the president of the Great Lakes SQL Server Users Group in Chicago from 1994 through 1997. He can be reached at firstname.lastname@example.org.
Chapter 1 - Introduction to Transact-SQL
In this chapter:
SQL and the Introduction of Transact-SQL
The Relational Database Model
Row Processing Versus Set Processing
The History of SQL Server
What Is Transact-SQL?
Transact-SQL, an extension to the SQL database programming language, is a powerful language offering many features--a wide variety of datatypes, temporary objects, system and extended stored procedures, scrollable cursors, conditional processing, transaction control, exception and error handling, and much more. We'll introduce those features later in this chapter in the section "What is Transact-SQL?" Before getting to Transact-SQL specifics, however, we'll provide some background information that will help you get a feel for the overall database environment in which Transact-SQL operates. After we explain the basic differences between ANSI SQL and Transact-SQL, we'll jump back to more generalized topics. We'll cover the genesis of the relational database model and its impact on SQL programming languages. We'll talk a bit about normalizing data and introduce you to the idea of row-processing and set-processing information technology. We'll spend a little time talking about the history of SQL Server in general. Finally, we'll introduce many of the features of the Transact-SQL programming language itself.
SQL and the Introduction of Transact-SQL
SQL, on which Transact-SQL is based, started life in the mid-1970s as an IBM product called SEQUEL. SEQUEL stood for Structured English Query Language. After a permutation or two and some legal problems, IBM changed the name to SQL--the Structured Query Language. The language was designed to provide a standard method for accessing data in a relational database. Ironically, although IBM introduced SQL, Oracle was the first to bring a SQL-using product to market.
Today, many different relational database systems utilize SQL as the primary means for accessing and manipulating data. When the American National Standards Institute (ANSI) published a standard for the SQL language in 1989, they set a universal standard to which database vendors could adhere. Later, in 1992, ANSI released an update to the SQL standard, known as SQL-92. The standards helped formalize many of the behaviors and syntax structures of SQL. The ANSI standard covered lots of important details concerning the querying and manipulation of data. The syntax was formalized for many commands; some of these are SELECT, INSERT, UPDATE, DELETE, CREATE, and DROP.
Unfortunately, the standards didn't address every facet of programming for a relational database. To meet the needs of their own user communities, database vendors began to extend the SQL language with capabilities that enhanced the basic functionality of SQL. The Transact-SQL language was introduced by Sybase to answer user requirements for programming extensions to SQL--extensions enabling conditional processing, error handling, declared variables, row processing, and numerous other functions. Even some of the simplest operations, like creating an index or performing a conditional operation, are extensions to the SQL language.
Furthermore, many relational database products had been on the market for some time before a standard of any kind had been published. As a result, many developers began to implement their own extensions to the SQL language. In most cases, these extensions to SQL were incompatible from vendor to vendor. A program written in Oracle's dialect of SQL, for example, wouldn't run properly under Sybase or DB2 and vice versa unless it contained only the simplest ANSI-standard SQL statements.
The Relational Database Model
These days, relational database management systems (RDBMSs) like SQL Server and Sybase are the primary engines of information systems everywhere--particularly distributed client/server computing systems. Though RDBMSs are now common enough to trip over, it wasn't always that way. Not too long ago, you would probably trip over hierarchical database systems or network database systems or COBOL (heck, that still happens). Here's a quick-and-dirty definition for a relational database: a system whose users view data as a collection of tables related to one another through common data values.
Perhaps you are interested in more than a quick-and-dirty definition? Here goes. The whole basis for the relational model follows this train of thought: data is stored in tables, which are composed of rows and columns. Tables of independent data can be linked, or related, to one another if all have columns of data that represent the same data value, called keys. This concept is so common as to seem trivial; however, it was not so long ago that achieving and programming a system capable of sustaining the relational model was considered a long shot with limited usefulness.
Relational data theory was first proposed by E. F. Codd in his 1970 paper to the ACM entitled "A Relational Model of Data for Large Shared Data Banks." Soon after, Codd clarified his position in the 1974 paper to the Texas Conference on Computing Systems entitled "The Relational Approach to Data Base Management: An Overview." It was in this paper that Codd proposed the now legendary 12 Principles of Relational Databases. If a vendor's database product didn't meet Codd's 12-item litmus test, then it was not a member of the club. The good news is that rules do not apply to applications development; rather, these rules determine whether the database engine itself can be considered truly "relational." Nowadays, most RDBMSs--including both Microsoft and Sybase variants of SQL Server--pass the test.