What’s New in SQL Server System and Database Administration
System Administrator Responsibilities
System Databases
System Tables
System Views
System Stored Procedures
Summary
8 Installing SQL Server 2008
What’s New in Installing SQL Server 2008
Installation Requirements
Installation Walkthrough
Installing SQL Server Using a Configuration File
Installing Service Packs and Cumulative Updates
Slipstream Installations
Summary
9 Upgrading to SQL Server 2008
What’s New in Upgrading SQL Server
Using the SQL Server Upgrade Advisor (UA)
Destination: SQL Server 2008 or SQL Server 2008 R2
Upgrading Using a Configuration File
Slipstreaming Upgrades
Upgrading Other SQL Server Components
Summary
10 Client Installation and Configuration
What’s New in Client Installation and Configuration
Client/Server Networking Considerations
Client Installation
Client Configuration
Client Data Access Technologies
Summary
11 Security and User Administration
What’s New in Security and User Administration
An Overview of SQL Server Security
Authentication Methods
Managing Principals
Managing Securables
Managing Permissions
Managing SQL Server Logins
Managing SQL Server Users
Managing Database Roles
Managing SQL Server Permissions
The Execution Context
Summary
12 Data Encryption
What’s New in Data Encryption
An Overview of Data Security
An Overview of Data Encryption
SQL Server Key Management
Column-Level Encryption
Transparent Data Encryption
Column-Level Encryption Versus Transparent Data Encryption
Summary
13 Security and Compliance
Exposure and Risk
Across the Life Cycle
The Security Big Picture
Identity Access Management Components
Compliance and SQL Server
SQL Server Auditing
Setting Up Auditing via T-SQL
SQL Injection Is Easy to Do
Summary
14 Database Backup and Restore
What’s New in Database Backup and Restore
Developing a Backup and Restore Plan
Types of Backups
Recovery Models
Backup Devices
Backing Up a Database
Backing Up the Transaction Log
Backup Scenarios
Restoring Databases and Transaction Logs
Restore Scenarios
Additional Backup Considerations
Summary
15 Database Mail
What’s New in Database Mail
Setting Up Database Mail
Sending and Receiving with Database Mail
Using SQL Server Agent Mail
Related Views and Procedures
Summary
16 SQL Server Scheduling and Notification
What’s New in Scheduling and Notification
Configuring the SQL Server Agent
Viewing the SQL Server Agent Error Log
SQL Server Agent Security
Managing Operators
Managing Jobs
Managing Alerts
Scripting Jobs and Alerts
Multiserver Job Management
Event Forwarding
Summary
17 Administering SQL Server 2008 with PowerShell
What’s New with PowerShell
Overview of PowerShell
PowerShell Scripting Basics
PowerShell in SQL Server 2008
Step-By-Step Examples
Summary
18 SQL Server High Availability
What’s New in High Availability
What Is High Availability?
The Fundamentals of HA
Building Solutions with One or More HA Options
Other HA Techniques That Yield Great Results
High Availability from the Windows Server Family Side
Summary
19 Replication
What’s New in Data Replication
What Is Replication?
The Publisher, Distributor, and Subscriber Magazine Metaphor
Replication Scenarios
Subscriptions
Replication Agents
Planning for SQL Server Data Replication
SQL Server Replication Types
Basing the Replication Design on User Requirements
Setting Up Replication
Scripting Replication
Monitoring Replication
Summary
20 Database Mirroring
What’s New in Database Mirroring
What Is Database Mirroring?
Roles of the Database Mirroring Configuration
Setting Up and Configuring Database Mirroring
Testing Failover from the Principal to the Mirror
Client Setup and Configuration for Database Mirroring
Migrate to Database Mirroring 2008 as Fast as You Can
Using Replication and Database Mirroring Together
Using Database Snapshots from a Mirror for Reporting
Summary
21 SQL Server Clustering
What’s New in SQL Server Clustering
How Microsoft SQL Server Clustering Works
Installing SQL Server Clustering
Summary
22 Administering Policy-Based Management
Introduction to Policy-Based Management
Policy-Based Management Concepts
Implementing Policy-Based Management
Sample Templates and Real-World Examples
Policy-Based Management Best Practices
Summary
Part IV: Database Administration
23 Creating and Managing Databases
What’s New in Creating and Managing Databases
Data Storage in SQL Server
Database Files
Creating Databases
Setting Database Options
Managing Databases
Summary
24 Creating and Managing Tables
What’s New in SQL Server 2008
Creating Tables
Defining Columns
Defining Table Location
Defining Table Constraints
Modifying Tables
Dropping Tables
Using Partitioned Tables
Creating Temporary Tables
Summary
25 Creating and Managing Indexes
What’s New in Creating and Managing Indexes
Types of Indexes
Creating Indexes
Managing Indexes
Dropping Indexes
Online Indexing Operations
Indexes on Views
Summary
26 Implementing Data Integrity
What’s New in Data Integrity
Types of Data Integrity
Enforcing Data Integrity
Using Constraints
Rules
Defaults
Summary
27 Creating and Managing Views in SQL Server
What’s New in Creating and Managing Views
Definition of Views
Using Views
Creating Views
Managing Views
Data Modifications and Views
Partitioned Views
Indexed Views
Summary
28 Creating and Managing Stored Procedures
What’s New in Creating and Managing Stored Procedures
Advantages of Stored Procedures
Creating Stored Procedures
Executing Stored Procedures
Deferred Name Resolution
Viewing Stored Procedures
Modifying Stored Procedures
Using Input Parameters
Using Output Parameters
Returning Procedure Status
Debugging Stored Procedures Using SQL Server Management Studio
Using System Stored Procedures
Startup Procedures
Summary
29 Creating and Managing User-Defined Functions
What’s New in SQL Server 2008
Why Use User-Defined Functions?
Types of User-Defined Functions
Creating and Managing User-Defined Functions
Rewriting Stored Procedures as Functions
Creating and Using CLR Functions
Summary
30 Creating and Managing Triggers
What’s New in Creating and Managing Triggers
Using DML Triggers
Using DDL Triggers
Using CLR Triggers
Using Nested Triggers
Using Recursive Triggers
Summary
31 Transaction Management and the Transaction Log
What’s New in Transaction Management
What Is a Transaction?
How SQL Server Manages Transactions
Defining Transactions
Transactions and Batches
Transactions and Stored Procedures
Transactions and Triggers
Transactions and Locking
Coding Effective Transactions
Transaction Logging and the Recovery Process
Long-Running Transactions
Bound Connections
Distributed Transactions
Summary
32 Database Snapshots
What’s New with Database Snapshots
What Are Database Snapshots?
Limitations and Restrictions of Database Snapshots
Copy-on-Write Technology
When to Use Database Snapshots
Setup and Breakdown of a Database Snapshot
Reverting to a Database Snapshot for Recovery
Setting Up Snapshots Against a Database Mirror
Database Snapshots Maintenance and Security Considerations
Summary
33 Database Maintenance
What’s New in Database Maintenance
The Maintenance Plan Wizard
Managing Maintenance Plans Without the Wizard
Executing a Maintenance Plan
Maintenance Without a Maintenance Plan
Database Maintenance Policies
Summary
Part V: SQL Server Performance and Optimization
34 Data Structures, Indexes, and Performance
What’s New for Data Structures, Indexes, and Performance
Understanding Data Structures
Database Files and Filegroups
Database Pages
Space Allocation Structures
Data Compression
Understanding Table Structures
Understanding Index Structures
Data Modification and Performance
Index Utilization
Index Selection
Evaluating Index Usefulness
Index Statistics
SQL Server Index Maintenance
Index Design Guidelines
Indexed Views
Indexes on Computed Columns
Filtered Indexes and Statistics
Choosing Indexes: Query Versus Update Performance
Identifying Missing Indexes
Identifying Unused Indexes
Summary
35 Understanding Query Optimization
What’s New in Query Optimization
What Is the Query Optimizer?
Query Compilation and Optimization
Query Analysis
Row Estimation and Index Selection
Join Selection
Execution Plan Selection
Query Plan Caching
Other Query Processing Strategies
Parallel Query Processing
Common Query Optimization Problems
Managing the Optimizer
Summary
36 Query Analysis
What’s New in Query Analysis
Query Analysis in SSMS
SSMS Client Statistics
Using the SET SHOWPLAN Options
Using sys.dm_exec_query_plan
Query Statistics
Query Analysis with SQL Server Profiler
Summary
37 Locking and Performance
What’s New in Locking and Performance
The Need for Locking
Transaction Isolation Levels in SQL Server
The Lock Manager
Monitoring Lock Activity in SQL Server
SQL Server Lock Types
SQL Server Lock Granularity
Lock Compatibility
Locking Contention and Deadlocks
Table Hints for Locking
Optimistic Locking
Summary
38 Database Design and Performance
What’s New in Database Design and Performance
Basic Tenets of Designing for Performance
Logical Database Design Issues
Denormalizing a Database
Database Filegroups and Performance
RAID Technology
SQL Server and SAN Technology
Summary
39 Monitoring SQL Server Performance
What’s New in Monitoring SQL Server Performance
Performance Monitoring Tools
A Performance Monitoring Approach
Summary
40 Managing Workloads with the Resource Governor
Overview of Resource Governor
Resource Governor Components
Configuring Resource Governor
Monitoring Resource Usage
Modifying Your Resource Governor Configuration
Summary
41 A Performance and Tuning Methodology
The Full Architectural Landscape
Primary Performance and Tuning Handles
A Performance and Tuning Methodology
Performance and Tuning Design Guidelines
Tools of the Performance and Tuning Trade
Summary
Part VI: SQL Server Application Development
42 What’s New for Transact-SQL in SQL Server 2008
MERGE Statement
Insert over DML
GROUP BY Clause Enhancements
Variable Assignment in DECLARE Statement
Compound Assignment Operators
Row Constructors
New date and time Data Types and Functions
Table-Valued Parameters
Hierarchyid Data Type
Using FILESTREAM Storage
Sparse Columns
Spatial Data Types
Change Data Capture
Change Tracking
Summary
43 Transact-SQL Programming Guidelines, Tips, and Tricks
General T-SQL Coding Recommendations
General T-SQL Performance Recommendations
T-SQL Tips and Tricks
In Case You Missed It: New Transact-SQL Features in SQL Server 2005
The xml Data Type
The max Specifier
TOP Enhancements
The OUTPUT Clause
Common Table Expressions
Ranking Functions
PIVOT and UNPIVOT
The APPLY Operator
TRY...CATCH Logic for Error Handling
The TABLESAMPLE Clause
Summary
44 Advanced Stored Procedure Programming and Optimization
T-SQL Stored Procedure Coding Guidelines
Using Cursors in Stored Procedures
Nested Stored Procedures
Using Temporary Tables in Stored Procedures
Using Remote Stored Procedures
Stored Procedure Performance
Using Dynamic SQL in Stored Procedures
Installing and Using .NET CLR Stored Procedures
Using Extended Stored Procedures
Summary
45 SQL Server and the .NET Framework
What’s New in SQL Server 2008 and the .NET Framework
Getting Comfortable with ADO.NET 3.5 and SQL Server 2008
Developing with LINQ to SQL
Using ADO.NET Data Services
Leveraging the Microsoft Sync Framework
Summary
46 SQLCLR: Developing SQL Server Objects in .NET
What’s New for SQLCLR in SQL Server 2008
Developing Custom Managed Database Objects
Summary
47 Using XML in SQL Server 2008
What’s New in Using XML in SQL Server 2008
Understanding XML
Relational Data As XML: The FOR XML Modes
XML As Relational Data: Using OPENXML
Using the xml Data Type
Indexing and Full-Text Indexing of xml Columns
Summary
48 SQL Server Web Services
What’s New in SQL Server Web Services
Web Services Migration Path
Web Services History and Overview
Building Web Services
Examples: A C# Client Application
Using Catalog Views and System Stored Procedures
Controlling Access Permissions
Summary
49 SQL Server Service Broker
What’s New in Service Broker
Understanding Distributed Messaging
Designing a Sample System
Understanding Service Broker Constructs
Service Broker Routing and Security
Troubleshooting SSB Applications with ssbdiagnose.exe
Related System Catalogs
Summary
50 SQL Server Full-Text Search
What’s New in SQL Server 2008 Full-Text Search
Upgrade Options in SQL Server 2008
How SQL Server FTS Works
Implementing SQL Server 2008 Full-Text Catalogs
Setting Up a Full-Text Index
Full-Text Searches
Full-Text Search Maintenance
Full-Text Search Performance
Full-Text Search Troubleshooting
Summary
Part VII: SQL Server Business Intelligence Features
51 SQL Server 2008 Analysis Services
What’s New in SSAS
Understanding SSAS and OLAP
Understanding the SSAS Environment Wizards
An Analytics Design Methodology
An OLAP Requirements Example: CompSales International
Summary
52 SQL Server Integration Services
What’s New with SSIS
SSIS Basics
SSIS Architecture and Concepts
SSIS Tools and Utilities
A Data Transformation Requirement
Running the SSIS Wizard
The SSIS Designer
The Package Execution Utility
Connection Projects in Visual Studio
Change Data Capture Addition with R2
Using bcp
Logged and Nonlogged Operations
Summary
53 SQL Server 2008 Reporting Services
What’s New in SSRS 2008
Reporting Services Architecture
Installing and Configuring SSRS
Developing Reports
Management and Security
Performance and Monitoring
Summary
Part VIII: Bonus Chapters
54 Managing Linked and Remote Servers
What’s New in Managing Linked and Remote Servers
Managing Remote Servers
Linked Servers
Adding, Dropping, and Configuring Linked Servers
Mapping Local Logins to Logins on Linked Servers
Obtaining General Information About Linked Servers
Executing a Stored Procedure via a Linked Server
Setting Up Linked Servers Using SQL Server Management Studio
Summary
55 Configuring, Tuning, and Optimizing SQL Server Options
What’s New in Configuring, Tuning, and Optimizing SQL Server Options
SQL Server Instance Architecture
Configuration Options
Fixing an Incorrect Option Setting
Setting Configuration Options with SSMS
Obsolete Configuration Options
Configuration Options and Performance
Database Engine Tuning Advisor
Data Collection Sets
Summary
56 SQL Server Disaster Recovery Planning
What’s New in SQL Server Disaster Recovery Planning
How to Approach Disaster Recovery
Microsoft SQL Server Options for Disaster Recovery
The Overall Disaster Recovery Process
Have You Detached a Database Recently?
Third-Party Disaster Recovery Alternatives
Summary
Index
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y-Z
Nội dung
ptg 1664 CHAPTER 43 Transact-SQL Programming Guidelines, Tips, and Tricks convert the date value into a date you need. You need to start thinking in terms of date intervals—for example, how many date intervals it is from the current date to the date you want to calculate, or how many date intervals it is from today to some other date, such as ”2000-01-01”, and so on. You use the DATEADD and DATEDIFF functions to calculate your desired date by determining the appropriate date intervals from the current date and then adding or subtracting intervals to arrive at the desired calculated date. Understanding how to use the various date intervals helps you more easily understand how to calculate the desired dates. Calculating the First Day of Month Let’s look at a method for determining the first day of the month for a given date. To do this, you start out with the initial date. (In this example, you can use getdate() to work with the current system date and time.) The next step is to figure out the number of months between the given date and the date ”1/1/1900”. NOTE The value “1/1/1900” is the default for a date if an empty string (’’) is used to repre- sent a date. You can use the DATEDIFF function to determine the number of months from ”1/1/1900”: select DATEDIFF(mm,’’,getdate()) go 1321 Now, using the number of months, you can add that result to ”1/1/1900” to obtain the first day of the month for the given date: select DATEADD(mm, DATEDIFF(mm,’’,getdate()), ‘’) By adding the number of months between the given date and ”1/1/1900” to ”1/1/1900”, you are able to arrive at the first day of the current month. In addition, the time portion of the calculated date is set to ”00:00:00.000”. This technique for calculating a date interval between the current date and the year, ”1900-01-01”, and then adding the calculated number of intervals to ”1900-01-01” can be used to calculate many different dates. The next four examples use the same technique to generate different dates based on the current date. Calculating the First Day of the Year You can use the year interval ( yy) to display the first day of the year: select DATEADD(yy, DATEDIFF(yy,’’,getdate()), ‘’) ptg 1665 T-SQL Tips and Tricks 43 Calculating the First Day of the Quarter To calculate the first day of the current quarter, you use the quarter ( qq) interval: select DATEADD(qq, DATEDIFF(qq,’’,getdate()), ‘’) Calculating Midnight for the Current Day If you need to truncate the time portion for a datetime value so it reflects the current date at midnight, you can use the date interval ( dd) to get the midnight time stamp for the desired date: select DATEADD(dd, DATEDIFF(dd,’’,getdate()), ‘’) Calculating Monday of the Current Week You can use the week interval ( wk) to calculate what date is Monday of the current week: select DATEADD(wk, DATEDIFF(wk,’’,getdate()), ‘’) Calculating Other Dates As you have seen, by using simple DATEADD and DATEDIFF calculations, you can come up with many different dates that might be valuable. All the examples so far have only calcu- lated the number of date intervals between the current date and ”1/1/1900” and then added the appropriate number of intervals to ”1900-01-01” to arrive at the calculated date. If you have to calculate other date values, you can use this calculation as the basis and then add or subtract additional intervals to come up with other useful dates. For example, to calculate the last day of the previous month for a given date, you can use the following calculation to determine the first day of the current month and subtract a day from it: select DATEADD(dd, -1, DATEADD(mm, DATEDIFF(mm,’’,getdate()), ‘’)) You can perform a similar calculation to determine the last day of the previous year, based on the formula to calculate the first date of the current year for the given date: select DATEADD(dd, -1, DATEADD(yy, DATEDIFF(yy,’’,getdate()), ‘’)) What if you need to determine the last day of the current month for a given date? One way to do this is to calculate the first date of the next month and subtract one day from that. To calculate the first day of the next month, you can use the formula to calculate the first day of the current month and add one to the number of intervals returned by DATEDIFF when comparing the given date to ”1/1/1900” to get the first day of the next month: select DATEADD(mm, DATEDIFF(mm,’’,getdate()) + 1, ‘’) Now that you have the first date of the next month, you simply subtract one day from it to get the last day of the current month: select DATEADD(dd, -1, DATEADD(mm, DATEDIFF(mm,’’,getdate()) + 1, ‘’)) ptg 1666 CHAPTER 43 Transact-SQL Programming Guidelines, Tips, and Tricks Similarly, you can modify the formula to calculate the first day of the year to return the last day of the previous year: select DATEADD(dd, -1, DATEADD(yy, DATEDIFF(yy,’’,getdate()) + 1, ‘’)) Now, let’s try a little more advanced calculation: the first Monday of the current month. To find this, you start with the calculation for the Monday of the current week and modify it slightly. Rather than use getdate() as the date value, you use the calculation to get the first day of the month and add five days to it. Adding five days to the first day of the month ensures that you are in the first full week of the month (SQL Server treats Sunday as the first day of the week, so if the first day of the month was on a Monday, adding 5 days keeps you in the same week. If the first day is Tuesday or later, adding 5 days puts you into the next week). You can use the following calculation to get the first day of the month and add five days to it: select DATEADD(dd, 5, DATEADD(mm, DATEDIFF(mm,’’,getdate()), ‘’)) Now, you use this expression in place of the getdate() function in the calculation to get the date for Monday of the current week: select DATEADD(wk, DATEDIFF(wk,’’, DATEADD(dd, 5, DATEADD(mm, DATEDIFF(mm,’’,getdate()), ‘’))), ‘’) The examples presented in this chapter should give you some insight into using the DATEADD and DATEDIFF functions for calculating dates using date intervals. You can use them as a basis for calculating other dates that your applications might need. TIP If you find yourself using any of these date calculations frequently, it might be a good idea to create one or more user-defined functions to encapsulate these calculations. It would save your having to reenter the sometimes complex formulas, which can be easi- ly mistyped, leading to incorrect calculations. For information on creating user-defined functions, see Chapter 29, “Creating and Managing User-Defined Functions.” Converting Dates for Comparison Because the datetime data type contains both time and date components, searching for data rows matching a specific date only, excluding the time component, can sometimes be a bit tricky—especially when you consider that SQL Server stores time values only down to 3/1,000 second. For example, if you want to find all rows where the date is for a certain day, you have to perform a range search for all times within that day. Because a date without a time specified defaults to a time of midnight ( 00:00:00.000) for that date, the following query doesn’t return all matching rows if any of the data values contain a time other than midnight: ptg 1667 T-SQL Tips and Tricks 43 select title_id, pubdate from dbo.titles where pubdate = ‘2006-01-14’ To be sure to include all rows for a particular date, regardless of the time component stored, you could run a query similar to the following: select title_id, pubdate from dbo.titles where pubdate between ‘2006-01-14 00:00:00.0’ and ‘2006-01-14 23:59:59.997’ go title_id pubdate FI3599 2006-01-14 00:00:00.000 Now you might be wondering, why use a time of ”2006-01-14 23:59:59.997” as the last time of the day? You do so because SQL Server stores datetime values only down to 3/1,000 second. If you enter a time of ”2006-01-14 23:59:59.999”, SQL Server rounds it up to ”2006-01-15 00:00:00.000”, and it actually matches any rows with that datetime value, as in this example: select title_id, pubdate from dbo.titles where pubdate between ‘2006-01-14 00:00:00.0’ and ‘2006-01-14 23:59:59.999’ title_id pubdate FI3599 2006-01-14 00:00:00.000 FI5162 2006-01-15 00:00:00.000 This is one reason you have to be careful when performing date searches. Now you might be wondering why not just use the DATEDIFF function as in the following example: select title_id, pubdate from dbo.titles where datediff(day, pubdate, ‘2006-01-14’) = 0 go title_id pubdate FI3599 2006-01-14 00:00:00.000 Although this query returns the correct result, the use of the function on the pubdate column may prevent SQL Server from using any indexes that exist on the pubdate column to optimize the query, and it is likely to end up performing a table scan. (For more infor- mation on query optimization and optimizable search arguments, see Chapter 35.) To help ensure that your queries are optimized effectively, you need to try to avoid using any functions or expressions on the column in the search argument, and you need to search against constant expressions. ptg 1668 CHAPTER 43 Transact-SQL Programming Guidelines, Tips, and Tricks Another way to write the preceding query would be to use the date calculations discussed previously in this section. For example, you could use the calculation to determine midnight of the desired date and use that as the inclusive lower bound, and you could use the calculation of midnight of the next day as the noninclusive upper bound and write a query similar to the following: declare @date datetime set @date = ‘2006-01-14’ select title_id, pubdate from dbo.titles where pubdate >= DATEADD(dd, DATEDIFF(dd,’’,@date), ‘’) and pubdate < DATEADD(dd, DATEDIFF(dd,’’,@date) + 1, ‘’) go title_id pubdate FI3599 2006-01-14 00:00:00.000 SQL Server 2008 introduces the date and time data types, as well as the datetime2 data type. The long-awaited date and time data types store just a date value or time value, respectively, making date-only or time-only comparisons much simpler. For example, the previous solution for finding all books published on a specific day can be simplified a bit using the date data type because there is no need to consider a time component: declare @date date set @date = ‘2006-01-14’ select title_id, pubdate from dbo.titles where pubdate >= @date and pubdate < DATEADD(dd, 1, @date) go If the pubdate column were defined using the date data type instead of datetime (reason- able because the time of publication of a book is irrelevant), the comparison becomes even simpler: alter table titles drop constraint DF__titles__pubdate__103673A0 drop statistics titles.pubdate alter table titles alter column pubdate date null alter table titles add constraint DF__titles__pubdate__103673A0 default getdate() for pubdate go declare @date date set @date = ‘2006-01-14’ select title_id, pubdate from dbo.titles where pubdate = @date go ptg 1669 T-SQL Tips and Tricks 43 title_id pubdate FI3599 2006-01-14 00:00:00.000 The datetime2 data type stores the time value down to microseconds and avoids the 3/1,000 second rounding issue that was present with the datetime data type. For example, if you redefine the pubdate column using the datetime2 data type, you avoid the round- ing issue and get a single row as expected by the following query: alter table titles drop constraint DF__titles__pubdate__103673A0 alter table titles alter column pubdate datetime2 null alter table titles add constraint DF__titles__pubdate__103673A0 default sysdatetime() for pubdate go select title_id, pubdate from dbo.titles where pubdate between ‘2006-01-14 00:00:00.0’ and ‘2006-01-14 23:59:59.999999’ go title_id pubdate FI3599 2006-01-14 00:00:00.000 Sorting Results with the GROUPING Function When working with the CUBE or ROLLUP operator, SQL Server generates NULL values for the columns that are being rolled up to generate the aggregate values. When you are viewing the results, however, it can be difficult to determine whether the NULL value shown for a nonaggregate column is the result of a rollup or because the column itself contains a NULL value. Fortunately, SQL Server provides the GROUPING function, which you can use to distinguish between real NULL values and NULL values that represent a rollup of all values for a column in the result set. The GROUPING function returns 1 when the value is grouped and 0 when the column contains a NULL value. In Listing 43.13, the GROUPING function is used to replace NULL values for the rolled-up columns with ALL. LISTING 43.13 Using the GROUPING Function SELECT CASE when GROUPING(type) = 1 then ‘ALL’ else isnull(type, ‘Other’) END AS type, cast(CASE when (grouping(advance) = 1) then ‘ALL’ else isnull(convert(varchar(10), advance), ‘Unknown’) ptg 1670 CHAPTER 43 Transact-SQL Programming Guidelines, Tips, and Tricks END as varchar(10)) as advance, count(*) AS number FROM DBO.titles where type like ‘%cook%’ or type like ‘p%’ GROUP BY type, advance WITH rollup go type advance number mod_cook 0.00 1 mod_cook 15000.00 1 mod_cook ALL 2 popular_comp Unknown 1 popular_comp 7000.00 1 popular_comp 8000.00 1 popular_comp ALL 3 psychology 2000.00 1 psychology 2275.00 1 psychology 4000.00 1 psychology 6000.00 1 psychology 7000.00 1 psychology ALL 5 trad_cook 4000.00 1 trad_cook 7000.00 1 trad_cook 8000.00 1 trad_cook ALL 3 ALL ALL 13 You can also use the GROUPING function to order the result sets to move all the rollups toward the bottom, as shown in Listing 43.14. LISTING 43.14 Using the GROUPING Function to Order the Result Sets SELECT CASE when GROUPING(type) = 1 then ‘ALL’ else isnull(type, ‘Other’) END AS type, cast(CASE when (grouping(advance) = 1) then ‘ALL’ else isnull(convert(varchar(10), advance), ‘Unknown’) END as varchar(10)) as advance, count(*) AS number FROM DBO.titles where type like ‘%cook%’ or type like ‘p%’ GROUP BY type, advance WITH rollup ptg 1671 T-SQL Tips and Tricks 43 ORDER by GROUPING(type), GROUPING(advance) go type advance number popular_comp Unknown 1 popular_comp 7000.00 1 popular_comp 8000.00 1 psychology 2000.00 1 psychology 2275.00 1 psychology 4000.00 1 psychology 6000.00 1 psychology 7000.00 1 trad_cook 4000.00 1 trad_cook 7000.00 1 trad_cook 8000.00 1 mod_cook 0.00 1 mod_cook 15000.00 1 mod_cook ALL 2 trad_cook ALL 3 psychology ALL 5 popular_comp ALL 3 ALL ALL 13 Using CONTEXT_INFO Although SQL Server enables you to define local variables within a T-SQL batch or stored procedure, local variables do not retain values between batches or stored procedures. Unfortunately, SQL Server 2008 does not enable you to create user-defined global vari- ables. However, you can simulate global variables by using the CONTEXT_INFO setting, which allows you to store information in the context_info column in the sys.sysprocesses catalog view. A row in sys.sysprocesses exists for every connection to SQL Server, so the data remains there until you disconnect from SQL Server. The context_info column is a binary (128) column. You can store any data value in it with the SET CONTEXT_INFO command, but you have to deal with hexadecimal data when retrieving it. If you are handy at manipulating hexadecimal data, you can store multiple values in the context_info column. The following example stores the average price from the titles table in the context_info column: declare @avg_price money select @avg_price = avg(price) from dbo.titles set context_info @avg_price You can retrieve the value stored in context_info by using a SELECT statement. You need to convert the binary data back to money when you retrieve it. Because avg(price) is the ptg 1672 CHAPTER 43 Transact-SQL Programming Guidelines, Tips, and Tricks only value stored in context_info, you can retrieve it by performing a substring on the first 8 bytes of the context_info column. (The money data type is 8 bytes in size.) Because SQL Server assigns a unique server process ID (SPID) to each connection, you use the @@SPID function to retrieve the information for the current connection: select convert(money, substring(context_info, 1, 8)) as AVG_PRICE from master sysprocesses where spid = @@spid go AVG_PRICE 0.3751 If you don’t use a substring to specify only the first 8 bytes of the context_info column, SQL Server assumes that the money data is stored in the last 8 bytes and returns a result of 0: select convert(money, context_info) as AVG_PRICE from master sysprocesses where spid = @@spid go AVG_PRICE 0.00 Because money can be implicitly converted to binary, you don’t need to convert it when setting context_info. For some other data types, such as char or datetime, you need to explicitly convert the data to binary because implicit conversions from those data types to binary is not supported. In the following example, you append a datetime value to the average price value already stored in context_info. You explicitly convert the datetime value to binary and append it to the 8 bytes you have already stored in context_info: declare @max_date datetime, @context_info binary(128) select @max_date = max(pubdate) from dbo.titles select @context_info = substring(context_info, 1, 8) + convert(binary(8), @max_date) from master sysprocesses where spid = @@spid set context_info @context_info You now have two values stored in context_info. Using the appropriate substring, you can retrieve either the average price or the maximum pubdate from context_info: declare @avg_price money, @max_pubdate datetime ptg 1673 T-SQL Tips and Tricks 43 select @avg_price = substring(context_info, 1, 8), @max_pubdate = substring(context_info, 9, 8) from master sysprocesses where spid = @@spid select @avg_price as ‘Avg Price’, @max_pubdate as ‘Max PubDate’ go Avg Price Max PubDate 0.3751 2009-05-31 00:00:00.000 Note that the binary data converts implicitly to money and datetime. Working with Outer Joins An outer join is used to return all the rows from the specified outer table (specified with LEFT OUTER, RIGHT OUTER, or FULL OUTER), even if the other table has no match. Rows returned from the outer table that have no corresponding match in the inner table display the value NULL for any columns retrieved from the inner table. For example, you might want to display the names of all authors along with the average royalty paid, if available: select au_lname, au_fname, avg(royaltyper) as avg_royalty from dbo.authors a left outer join dbo.titleauthor ta on a.au_id = ta.au_id group by au_lname, au_fname order by 3 go au_lname au_fname avg_royalty Greene Morningstar NULL Greenfield Tom NULL McBadden Heather NULL Smith Meander NULL Stringer Dirk NULL Gringlesby Burt 30 O’Leary Michael 35 Ringer Anne 37 Yokomoto Akiko 40 MacFeather Stearns 42 Hunter Sheryl 50 Dull Ann 50 Bennet Abraham 60 Green Marjorie 70 DeFrance Michel 75 Karsen Livia 75 Ringer Albert 75 . CONTEXT_INFO Although SQL Server enables you to define local variables within a T -SQL batch or stored procedure, local variables do not retain values between batches or stored procedures. Unfortunately, SQL Server. last time of the day? You do so because SQL Server stores datetime values only down to 3/1,000 second. If you enter a time of ”2006-01-14 23:59:59.999”, SQL Server rounds it up to ”2006-01-15 00:00:00.000”,. view. A row in sys.sysprocesses exists for every connection to SQL Server, so the data remains there until you disconnect from SQL Server. The context_info column is a binary (128) column. You