Вы находитесь на странице: 1из 11

Microsoft Virtual Labs Express

What's new in SQL Server 2005


What's new in SQL Server 2005

Table of Contents
What's new in SQL Server 2005 ............................................................................................. 1
Exercise 1 Data Management in SQL Server 2005...............................................................................................2
Exercise 2 Transact-SQL Enhancements: PIVOT and UNPIVOT .....................................................................5
Exercise 3 Transact-SQL Enhancements: TRY/CATCH .....................................................................................7
What's new in SQL Server 2005

What's new in SQL Server 2005


After completing this lab, you will be better able to:
Objectives ƒ Use the SQL Server Management Studio
ƒ Use Try/Catch blocks in SQL
ƒ PIVOT and UNPIVOT data
In today's connected world, data and the systems that manage that data must
Scenario always be secure yet available to your users. With SQL Server 2005, users and
information technology (IT) professionals across your organization will benefit
from reduced application downtime, increased scalability and performance, and
tight yet flexible security controls. SQL Server 2005 also includes many new and
improved capabilities to help make your IT staff more productive. This scenario
provides a brief introduction to some of these new features and capabilities of
Microsoft SQL Server 2005.

Estimated Time to 15 Minutes


Complete This Lab
The password for the Administrator account on this computer is: P@ssw0rd
Computer used in this Lab

Page 1 of 9
What's new in SQL Server 2005

Exercise 1
Data Management in SQL Server 2005

Scenario
The SQL Server Management Studio simplifies management by provided one integrated management console to
monitor and manage the SQL Server relational database. This exercise briefly introduces this powerful tool.

Tasks Detailed Steps


1. The SQL Server Welcome to VLab Express. In this lab, we will take a brief look at just some of the
Management Studio. features of SQL Server 2005. For more information, please visit the SQL Server 2005
site, or take the full length Virtual Labs.
While there are many improvements in SQL Server 2005, this lab will briefly touch on
the improvements in data management, and new commands. First, let’s take a look at
the SQL Server Management Studio.
Microsoft SQL Server 2005 provides the Microsoft Windows Server System integrated
server software with a database platform for the next generation of connected,
scalable, and reliable enterprise applications. The breadth and depth of innovation in
this version is in response to the needs of customers.
While many features in SQL Server are evolutionary, there is one area of SQL Server
that is revolutionary—the tool set. SQL Server Management Studio is the first
database administration suite that allows users to connect to and work with SQL
Server, Analysis Services, and a SQL Server Mobile Edition database. Moreover, you
can also manage DTS, Reporting Services, and Notification Services applications.
In this exercise, we will take a look at just a small piece of this new toolset.
a. Click Start | All Programs | SQL Server 2005 | SQL Server Management
Studio.
b. Click Connect.
SQL Server 2005 replaces SQL Query Analyzer with the Query Editor, a component of
SQL Server Management Studio and the primary tool for designing and testing
Transact-SQL statements, queries, batches, and scripts interactively. With the Query
Editor, you can write new scripts in Transact-SQL and Multidimensional Expressions
(MDX). You can also edit scripts that are created from files or that are automatically
generated from SQL Server Management Studio dialog boxes or from SQL Object
Explorer.
c. Expand the Databases node.
d. Expand the AdventureWorks database.
e. Expand the Tables node.
f. Scroll through the tables.
g. Expand the Person.Contact node.
h. Expand the Columns node.
i. Expand the Triggers node.
j. Right-click on the uContact trigger and click Modify.
The SQL Server Management console allows you to see and modify tables, data,
management, triggers, constraints, indices, and stored procedures associated with
your database. In this case, every time Person.Contact is updated in a non-replication
scenario, the ModifiedDate field is updated. When we clicked Modify, the SQL Server

Page 2 of 9
What's new in SQL Server 2005
Tasks Detailed Steps
Management Studio provided us with the basis for modifying the trigger.
k. Right-click the Person.Contact node and click Open Table.
l. Close the Properties pane.
The SQL Server Management Studio provides full editable tables as well as multiple
query windows.
m. Scroll through the data.
n. Click on Person.Contact.
o. Click New Query.
p. In the new query pane, type
SELECT * from Person.Contact
WHERE FirstName=’Gustavo’
q. Click the check button next to the Execute button.
r. Click the Execute button.
s. Collapse the Tables node.
2. Database security The SQL Server Management console allows you to view and edit users and roles
roles through the associated with a database.
Management Studio. a. Click to expand the AdventureWorks | Security node.
b. Click to expand the Users node.
c. Click to expand the Roles node.
d. Under the Users node, right-click guest and click Properties.
Here, we are able to edit database permission that a user has.
e. Close the Database User – guest window.
f. Collapse the AdventureWorks node.
3. Groups and a. Click to expand the Security node.
permissions for SQL In the SQL Server security section, administrators can edit the Logins and Server
Server. Roles for the entire SQL Server system.
b. Click to expand the Management node.
The SQL Server Management Studio brings together the many aspects of SQL Server
management. Here, adminstrators can monitors SQL Server Logs, monitor activity
and undertake other tasks related to Server management.
4. Multiple a. In the Object Explorer, Click Connect | Database Engine.
Connections. b. Click the Server Name dropdown
c. Click <Browse for more>.
Microsoft SQL Server Management Studio allows you to connect to multiple
registered servers at once. On this machine, there is only one machine and one
registered server.
d. Click Cancel.
e. Click Cancel.

Page 3 of 9
What's new in SQL Server 2005
Tasks Detailed Steps
5. SQL Server If you are familiar with Visual Studio 2005, you will be familiar with the concept of
Management Studio projects. Microsoft SQL Server Management Studio supports projects, allowing you to
projects. save all of your queries, views, and connections in one easy to access project.
a. Click File | New | Project.
b. Click OK.
c. If prompted to save, click No.
d. Note the new pane, the Solution Explorer.
e. In the Solution Explorer, right-click Connections, and click New Connection.
f. Click OK.
g. In the Solution Explorer, right-click Queries, and click New Query.
h. Click Connect.
i. In the VSTS.master –SQLQuery1.sql pane, type the same query as before:
USE AdventureWorks
SELECT * from Person.Contact
WHERE FirstName=’Gustavo’
j. Click File | Close Solution.
k. Click Yes.
l. Click File | Open | Project/Solution.
m. Double-click the SQL Server Scripts1 directory.
n. Select SQL Server Scripts1.ssmssln and click Open.
6. Error checking. The SQL Server Management Studio also allows you to easily find errors in your
scripts.
a. In the Object Explorer, expand the VSTS | Databases | AdventureWorks |
Programmability | Stored Procedures node.
b. Right-click on dbo.uspGetBillOfMaterials and click Modify.
c. Minimize the Solution Explorer, by clicking the Thumb-Tack icon, for more
viewing room if necessary.
d. Remove the AND from the line
AND @CheckDate <= ISNULL(b.[EndDate], @CheckDate)
e. Click the Parse button. (The button with the check icon)
f. Double-click on the red error in the Results pane.
When double-clicking on a scripting error, SQL Server Management Studio will direct
you to the line containing the error.
g. Correct the error by returning the AND and click Parse again.
h. Minimize the SQL Server Management Studio.

Page 4 of 9
What's new in SQL Server 2005

Exercise 2
Transact-SQL Enhancements: PIVOT and UNPIVOT

Scenario
In this exercise, we will see how the new PIVOT and UNPIVOT commands can be used to rotate data around a
column.

Tasks Detailed Steps


1. PIVOT and There are new PIVOT and UNPIVOT operators. These operators perform a
UNPIVOT. manipulation on an input table-valued expression and produce an output table as a
result set. The PIVOT operator rotates rows into columns, and may perform
aggregations or other mathematical calculations along the way. It widens the input
table expression based on a given pivot column, generating an output table with a
column for each unique value in the column.
a. Maximize SQL Server Management Studio.
b. In the Object Explorer, open the node VSTS | Databases | AdventureWorks |
Views | Sales.vSalesPersonSalesByFiscalYear.
c. Right-click Sales.vSalesPersonSalesByFiscalYear and click Script View as |
CREATE To | New Query Editor Window.
d. Examine the SELECT statement of the view:
SELECT
pvt.[SalesPersonID]
,pvt.[FullName]
,pvt.[Title]
,pvt.[SalesTerritory]
,pvt.[2002]
,pvt.[2003]
,pvt.[2004]
FROM (SELECT
soh.[SalesPersonID]
,c.[FirstName] + ' ' + COALESCE(c.[MiddleName],
'') + ' ' + c.[LastName] AS [FullName]
,e.[Title]
,st.[Name] AS [SalesTerritory]
,soh.[SubTotal]
,YEAR(DATEADD(m, 6, soh.[OrderDate])) AS
[FiscalYear]
FROM [Sales].[SalesPerson] sp
INNER JOIN [Sales].[SalesOrderHeader] soh
ON sp.[SalesPersonID] = soh.[SalesPersonID]
INNER JOIN [Sales].[SalesTerritory] st
ON sp.[TerritoryID] = st.[TerritoryID]
INNER JOIN [HumanResources].[Employee] e
ON soh.[SalesPersonID] = e.[EmployeeID]
INNER JOIN [Person].[Contact] c
ON e.[ContactID] = c.ContactID
) AS soh
PIVOT
(
SUM([SubTotal])

Page 5 of 9
What's new in SQL Server 2005
Tasks Detailed Steps
FOR [FiscalYear]
IN ([2002], [2003], [2004])
) AS pvt;
e. Note especially the PIVOT command at the end of the view.
This is creating a view from several tables, and then pivoting the data around the
years 2002, 2003 and 2004. This type of PIVOT operation is extremely useful when
displaying data that can be logically grouped by multiple different columns.
f. Right-click Sales.vSalesPersonSalesByFiscalYear and click Open View.
g. Note how the data is pivoted around the column Fiscal Year.
The UNPIVOT operator performs an opposite operation to PIVOT, rotating columns
into rows. It narrows the input table expression based on a pivot column. These new
recursive queries provide a way to reshape data.

Page 6 of 9
What's new in SQL Server 2005

Exercise 3
Transact-SQL Enhancements: TRY/CATCH

Scenario
In this exercise, we discuss the new TRY/CATCH feature of Transact-SQL and examine a stored procedure that
uses it.

Tasks Detailed Steps


1. TRY/CATCH in SQL Server 2005 modernizes the transactional capabilities of queries by adding an
Transact/SQL error handling capability. SQL Server 2005 introduces a simple but powerful
exception-handling mechanism in the form of a TRY/CATCH Transact-SQL construct.
Transaction abort errors that cause a batch to terminate in earlier versions of
SQL Server can be caught and handled in SQL Server 2005.
a. Open the node VSTS | Databases | AdventureWorks | Programmability |
Stored Procedures node.
b. Right-click dbo.uspLogError and click Script Stored Procedure as | CREATE
To | New Query Editor Window.
c. Examine the stored procedure.
BEGIN
SET NOCOUNT ON;

-- Output parameter value of 0 indicates that error


-- information was not logged
SET @ErrorLogID = 0;
BEGIN TRY
-- Return if there is no error information to log
IF ERROR_NUMBER() IS NULL
RETURN;

-- Return if inside an uncommittable transaction.


-- Data insertion/modification is not allowed when
-- a transaction is in an uncommittable state.
IF XACT_STATE() = -1
BEGIN
PRINT 'Cannot log error since the current
transaction is in an uncommittable state. '
+ 'Rollback the transaction before
executing uspLogError in order to successfully log error
information.';
RETURN;
END

INSERT [dbo].[ErrorLog]
(
[UserName],
[ErrorNumber],
[ErrorSeverity],
[ErrorState],
[ErrorProcedure],
[ErrorLine],

Page 7 of 9
What's new in SQL Server 2005
Tasks Detailed Steps
[ErrorMessage]
)
VALUES
(
CONVERT(sysname, CURRENT_USER),
ERROR_NUMBER(),
ERROR_SEVERITY(),
ERROR_STATE(),
ERROR_PROCEDURE(),
ERROR_LINE(),
ERROR_MESSAGE()
);

-- Pass back the ErrorLogID of the row inserted


SET @ErrorLogID = @@IDENTITY;
END TRY
BEGIN CATCH
PRINT 'An error occurred in stored procedure
uspLogError: ';
EXECUTE [dbo].[uspPrintError];
RETURN -1;
END CATCH
END;
d. Examine the line:
BEGIN TRY
The line BEGIN TRY indicates the beginning of SQL code that may fail. END TRY
indicates the end of the Try block.
e. Examine the lines:
END TRY
BEGIN CATCH
PRINT 'An error occurred in stored procedure
uspLogError: ';
EXECUTE [dbo].[uspPrintError];
RETURN -1;
END CATCH
In this stored procedure, if the try block fails, and the insert command in the try block
fails, the catch code is executed. At that point, the uspPrintError stored procedure is
executed. In this way, errors in the execution of Transact-SQL code can be handled.

Of course, these three examples are just scratching the surface of the new
functionality provided in SQL Server 2005. SQL Server 2005 provides the
technologies and capabilities that organizations and database administrators count
on. The spectrum of new and enhanced functionality that affect database
administration is immense. For more information on the new functionality in SQL
2005, visit the Microsoft SQL Server 2005 web site, or look for other Virtual Labs
using SQL Server 2005.

Page 8 of 9
What's new in SQL Server 2005

Thank you for attending Microsoft Virtual Labs Express. Do


you want more time? Would you like more comprehensive
content? Take a 90-minute Microsoft Virtual Lab today!

ƒ TechNet Virtual Labs for IT Pros:


http://www.microsoft.com/technet/traincert/virtuallab

ƒ MSDN Virtual Labs for Developers:


http://msdn.microsoft.com/virtuallabs

Page 9 of 9

Вам также может понравиться