Professional Documents
Culture Documents
Table of Contents
What's new in SQL Server 2008 CTP4 for OLTP Applications .............................................. 1
Exercise 1 Change Data Capture in Action ...................................................................................................................2
Exercise 2 DATE/TIME datatype in SQL Server 2008 .................................................................................................6
Exercise 3 Implementing a naming convention policy using Declarative Management Framework (DMF) .............. 11
Exercise 4 Implementing table valued parameters ...................................................................................................... 15
What's new in SQL Server 2008 CTP4 for OLTP Applications
In these series of exercises you will get a good understanding of the new features
Scenario in SQL Server 2008. You will learn and understand how to implement and work
with Change Data Capture, discover the new features of Database Mirroring, as
well as implement Table Valued Parameters. One of the other features you will
discover in the exercises is the use of the new date datatypes.
Note: This lab is tested with SQL Server 2008 codename ‘Katmai’ CTP 4. The
documentation and instructions in this lab is pre-release and is subject to change
in future release.
The password for the Administrator account on all computers in this lab is:
Pass@word1.
Page 1 of 18
What's new in SQL Server 2008 CTP4 for OLTP Applications
Exercise 1
Change Data Capture in Action
Scenario
Change data capture is designed to capture insert, update, and delete activity applied to SQL Server tables, and to
make the details of the changes available in an easily consumed relational format. The change tables used by change
data capture contain columns that mirror the column structure of a tracked source table, along with the metadata
needed to understand the changes that have occurred.
Note: Lab Scripts - In order to assist you completing the hands-on-labs a .sql script file for every exercise in this lab
has been created inside the virtual machine. The scripts for this lab can be found on the desktop under the SQL 2008
HOLs shortcut.
Page 2 of 18
What's new in SQL Server 2008 CTP4 for OLTP Applications
Tasks Detailed Steps
= NULL, @supports_net_changes =1;
GO
use SQL2008CDC
GO
INSERT INTO dbo.Employee
values (1, N'John Wills', N'jw@contoso.com')
GO
4. Update the inserted a. In the same query window, delete the previous statement(s).
data b. Run the following statement(s) by clicking ‘Execute’ on the standard toolbar or
by pressing ‘F5’
use SQL2008CDC
GO
UPDATE dbo.Employee SET EmpName = N'Don Miles' WHERE EmpID = 1;
GO
5. Using the Note: Let’s understand the query - cdc.lsn_time_mapping table is created when we
cdc.fn_cdc_get_all_c enable CDC as shown in the previous exercise. It returns one row for each transaction
hanges_<capture_ins having rows in a change table. This table is used to map between log sequence
tance> number (LSN) commit values and the time the transaction committed.
We can use the sys.fn_cdc_map_lsn_to_time and sys.fn_cdc_map_time_to_lsn system
functions to query the table. One strategy is to begin by querying for the minimum and
maximum LSN values for the capture instance. As shown in the following example, this
method returns all changes associated with the current change data capture timeline
for the capture instance.
a. If the query window is open, delete all the statement(s) or click on ‘New Query’
in the standard toolbar.
b. Run the following statement(s) by clicking ‘Execute’ on the standard toolbar or
by pressing ‘F5’
use SQL2008CDC
GO
DECLARE @from_lsn binary(10), @to_lsn binary(10);
SET @from_lsn = sys.fn_cdc_get_min_lsn('dbo_employee');
SET @to_lsn = sys.fn_cdc_get_max_lsn();
SELECT * FROM cdc.fn_cdc_get_all_changes_dbo_employee(@from_lsn,
@to_lsn, 'all');
GO
You will notice that 2 records are displayed (one for insert and another for update
Page 3 of 18
What's new in SQL Server 2008 CTP4 for OLTP Applications
Tasks Detailed Steps
operation)
Note: When querying for all changes in a specified interval, the row filter option 'all'
is commonly used. This option returns all changes, but for an operation, only the after
image is returned. To retrieve both the row containing the values before the update
and the row containing the values after the update, the row filter option 'all update
old' should be used.
Note: The caller can retain the value of @to_lsn so that it can be used to determine an
appropriate range for a subsequent request.
6. Using the a. In the same query window, delete the previous statement(s).
cdc.fn_cdc_get_net_ b. Run the following statement(s) by clicking ‘Execute’ on the standard toolbar or
changes_<capture_in
by pressing ‘F5’
stance>
use SQL2008CDC
GO
DECLARE @from_lsn binary(10), @to_lsn binary(10);
SET @from_lsn = sys.fn_cdc_get_min_lsn('dbo_employee');
SET @to_lsn = sys.fn_cdc_get_max_lsn();
SELECT * FROM cdc.fn_cdc_get_net_changes_dbo_employee(@from_lsn,
@to_lsn, 'all');
You will notice that only 1 record is returned which is the final (net) change (only
the updated operation)
Note: The above query may take slightly longer than expected when it is being run for
the first time.
7. Query the change a. If the query window is open, delete all the statement(s) or click on ‘New Query’
data in the standard toolbar.
b. Run the following statement(s) by clicking ‘Execute’ on the standard toolbar or
by pressing ‘F5’
use SQL2008CDC
GO
DECLARE @from_lsn binary(10), @to_lsn binary(10);
SET @from_lsn = sys.fn_cdc_get_min_lsn('dbo_employee');
SET @to_lsn = sys.fn_cdc_get_max_lsn();
SELECT * FROM cdc.fn_cdc_get_all_changes_dbo_employee(@from_lsn,
@to_lsn, 'all')
Note: You will notice that both the insert and update operations are being shown.
8. Clean the CDC table Note: Information - sp_cdc_cleanup_change_table removes rows from the change
until the last table in the current database based on the specified low_water_mark value.
operation This stored procedure is provided for users who want to directly manage the change
table cleanup process. Caution should be used, however, because the procedure
affects all consumers of the data in the change table.
a. In the same query window, delete the previous statement(s).
b. Run the following statement(s) by clicking ‘Execute’ on the standard toolbar or
Page 4 of 18
What's new in SQL Server 2008 CTP4 for OLTP Applications
Tasks Detailed Steps
by pressing ‘F5’
use SQL2008CDC
GO
DECLARE @end_time datetime;
DECLARE @to_lsn binary(10);
SET @end_time = GETDATE();
SELECT @to_lsn = sys.fn_cdc_map_time_to_lsn('largest less than or equal',
@end_time);
exec sys.sp_cdc_cleanup_change_table @capture_instance = 'dbo_employee',
@low_water_mark=@to_lsn
9. Close SQL Server a. Close SQL Server Management Studio without saving changes.
Management Studio
Page 5 of 18
What's new in SQL Server 2008 CTP4 for OLTP Applications
Exercise 2
DATE/TIME datatype in SQL Server 2008
Scenario
In SQL Server 2008 additional support for date and time data is provided by the introduction of new datatypes. The
introduction of these new datatypes allow you to better store and work with date and time data, including multiple
time zones and enhanced date calculations.
Besides these new datatypes a new set of date and time related functions are introduced. In this exercise you will
implement the new SQL Server 2005 date/time datatypes and explore new built-in-functions and features.
select * from sys.systypes where name like '%date%' or name like '%time%'
Page 6 of 18
What's new in SQL Server 2008 CTP4 for OLTP Applications
Tasks Detailed Steps
Note that the following datatypes are new in SQL Server 2005: Datetime2,
datetimeoffset, date, time
4. Create a table with a. In the same query window, delete the previous statement(s).
the new datatypes b. Run the following statement(s) by clicking ‘Execute’ on the standard toolbar or
by pressing ‘F5’
USE LABDB
GO
CREATE TABLE TBL_NewDatetimetypes (DateValue date, Timevalue Time,
DateTimeOffset datetimeoffset, Datetime2value datetime2)
5. Insert data in the a. In the same query window, delete the previous statement(s).
table b. Run the following statement(s) by clicking ‘Execute’ on the standard toolbar or
by pressing ‘F5’
Page 7 of 18
What's new in SQL Server 2008 CTP4 for OLTP Applications
Tasks Detailed Steps
system of the computer on which the instance of SQL Server is running
c. In the same query window, delete the previous statement(s).
d. Run the following statement(s) by clicking ‘Execute’ on the standard toolbar or
by pressing ‘F5’
6. Understanding the Note: Based on the previous query results you should understand that the new
stored data datatypes in SQL Server support the following storage:
This will allow you for a more granular storage of date and time data in SQL Server
2008.
7. Using the Note: Information - The datetimeoffset datatype allows you to store the timezone
datetimeoffset identifier together with the date value.
datatype
a. In the same query window, delete the previous statement(s).
b. Run the following statement(s) by clicking ‘Execute’ on the standard toolbar or
by pressing ‘F5’
Page 8 of 18
What's new in SQL Server 2008 CTP4 for OLTP Applications
Tasks Detailed Steps
Create table tbl_timezones (Entryid int identity(1,1),
Currenttime datetimeoffset)
GO
insert into tbl_timezones (currenttime)
values ('1998-09-20 7:45:50.71345 -5:00')
insert into tbl_timezones (currenttime)
values ('1956-01-27 6:45:50.00000 -3:00')
insert into tbl_timezones (currenttime)
values ('1972-12-18 7:45:50.71345 +1:00')
insert into tbl_timezones (currenttime)
values ('2005-01-20 7:12:50.71345 +9:00')
insert into tbl_timezones (currenttime)
values ('2005-01-20 01:00:00.00000 +4:00')
8. Use the DATEPART a. In the same query window, delete the previous statement(s).
function to return the b. Run the following statement(s) by clicking ‘Execute’ on the standard toolbar or
timezone offset for
by pressing ‘F5’
the current database
select datepart(TZoffset,sysdatetimeoffset())
Note: Depending on the server timezone settings this will return the value in minutes
from the GMT standard time. This will be -420 for PST, 60 for GMT+1
Note: Information - The DATEPART function has been extended to return the
timezone value from a given timezone, the TZoffset value is returned in a number,
representing the number in minutes from GMT standard time. So PST (GMT - 7) will
return the value of -420.
9. Use the a. In the same query window, delete the previous statement(s).
SWITCHOFFSET b. Run the following statement(s) by clicking ‘Execute’ on the standard toolbar or
function to return a
by pressing ‘F5’
date in a different
timezone
select currenttime,SWITCHOFFSET ( currenttime,
datepart(TZoffset,sysdatetimeoffset())) as TimeinCurrentTimezone
from tbl_timezones
Page 9 of 18
What's new in SQL Server 2008 CTP4 for OLTP Applications
Tasks Detailed Steps
10. Close SQL Server a. Close SQL Server Management Studio without saving changes.
Management Studio
Page 10 of 18
What's new in SQL Server 2008 CTP4 for OLTP Applications
Exercise 3
Implementing a naming convention policy using Declarative
Management Framework (DMF)
Scenario
In this exercise you will create a naming policy that will force users to create objects using the appropriate naming
conventions as defined within the companies policy standard. You are required to only allow the creation of tables
when they start with 'tbl' as the object name identifier. Since you don't want the policy to interfere with other
applications you will implement a filter that will apply the policy only to the Adventureworks database.
b. In the Create New Condition dialog box, in the Name box, type Table starts
with tbl.
c. In the Facet box, confirm that Table Facet is selected.
d. In the Expression area, in the Field box, select Name, in the Operator box select
LIKE, and in the Value type tbl%.
e. Optionally, you can type a description of the condition, by clicking on the
Description tab.
f. The dialog box should look similar to as shown below.
Page 11 of 18
What's new in SQL Server 2008 CTP4 for OLTP Applications
Tasks Detailed Steps
b. In the Create New Policy dialog box, in the Name box, type Table Naming
Conventions.
c. Select the Enabled box.
d. In the Condition box, select Table Starts with TBL.
e. In Applies to: select Server/Database/Table.
f. Click on Edit Filter to set the appropriate filter to only Adventureworks
g. In the Policy Target Filter dialog box, select Database
h. In Filter select Name from the Fields List and type Adventureworks in the
Value Box.
i. The dialog box should look similarto as shown below:
Page 12 of 18
What's new in SQL Server 2008 CTP4 for OLTP Applications
Tasks Detailed Steps
j. Click OK.
k. In the Create New Policy dialog box, check Enforce in the Execution mode.
l. The dialog box should look similar to as shown below:
m. Click OK.
4. Create a new table to a. Click on ‘New Query’ in the standard toolbar.
test if the policy gets b. Run the following statement(s) by clicking ‘Execute’ on the standard toolbar or
applied
by pressing ‘F5’
USE ADVENTUREWORKS
CREATE TABLE failedpolicy (policyid int)
You will notice that this table does not comply with the naming conventions and
therefore will not pass the policy check.
c. Run the following statement(s) by clicking ‘Execute’ on the standard toolbar or
by pressing ‘F5’
Page 13 of 18
What's new in SQL Server 2008 CTP4 for OLTP Applications
Tasks Detailed Steps
5. Disable the policy a. Run the following statement(s) by clicking ‘Execute’ on the standard toolbar or
by pressing ‘F5’
Note: Although you can use the GUI to disable policies, it is also possible to use a set
of system stored procedures to define and create DMF components. Books Online
provides you with an overview of the system stored procedures and metadata used by
DMF components that are defined and stored in the MSDB database.
6. Close SQL Server a. Close SQL Server Management Studio without saving changes.
Management Studio
Page 14 of 18
What's new in SQL Server 2008 CTP4 for OLTP Applications
Exercise 4
Implementing table valued parameters
Scenario
Table-valued parameters are a new parameter type in SQL Server 2008. Table-valued parameters are declared by
using user-defined table types. You can use table-valued parameters to send multiple rows of data to a Transact-SQL
statement or a routine, such as a stored procedure or function, without creating a temporary table or many
parameters.
Table-valued parameters are like parameter arrays in OLE DB and ODBC, but offer more flexibility and closer
integration with Transact-SQL. Table-valued parameters also have the benefit of being able to participate in set-
based operations
In this exercise your goal is to insert a entire set of data using multiple rows by using a single stored procedure.
Before SQL Server 2008, there was no integrated functionality that supported to call a stored procedure to achieve
that goal, using on server round trip. You will implement the workarounds that were existing pre-table valued
parameters and then implement the solution using table valued parameters.
USE AdventureWorks
GO
CREATE TABLE dbo.Employee(
EmpID int NOT NULL,
EmpName nvarchar(100) NOT NULL,
EmpEmail nvarchar(100) NOT NULL)
3. Create a new stored a. In the same query window, delete the previous statement(s).
procedure that will b. Run the following statement(s) by clicking ‘Execute’ on the standard toolbar or
insert data into the
by pressing ‘F5’
Employee table
USE AdventureWorks
GO
CREATE PROCEDURE NewEmployeeMS(@EmpID int,@EmpName
Page 15 of 18
What's new in SQL Server 2008 CTP4 for OLTP Applications
Tasks Detailed Steps
nvarchar(100),@EmpEmail nvarchar(100))
As
BEGIN
INSERT INTO dbo.Employee
values(
@EmpID, @EmpName, @EmpEmail)
END
Note: The stored procedure accepts three parameters to insert data into the table.
4. Execute the stored a. In the same query window, delete the previous statement(s).
procedure passing in b. Run the following statement(s) by clicking ‘Execute’ on the standard toolbar or
the required
by pressing ‘F5’
parameters
use AdventureWorks
Go
Note: The stored procedure has to be run three times to insert the required data.
5. Verify the data in a. If the query window is open, delete all the statement(s) or click on ‘New Query’
dbo.Employee table in the standard toolbar.
b. Run the following statement(s) by clicking ‘Execute’ on the standard toolbar or
by pressing ‘F5’
USE AdventureWorks
GO
select * from dbo.Employee;
USE AdventureWorks
GO
truncate table dbo.Employee
Page 16 of 18
What's new in SQL Server 2008 CTP4 for OLTP Applications
Tasks Detailed Steps
7. Verify that there is a. In the same query window, delete the previous statement(s).
no data in b. Run the following statement(s) by clicking ‘Execute’ on the standard toolbar or
dbo.Employee table
by pressing ‘F5’
USE AdventureWorks
GO
select * from dbo.Employee
8. Create a new table a. In the same query window, delete the previous statement(s).
type b. Run the following statement(s) by clicking ‘Execute’ on the standard toolbar or
by pressing ‘F5’
USE AdventureWorks
GO
CREATE TYPE EmployeeTableType AS TABLE
(EmpID INT, EmpName nvarchar(100), EmpEmail nvarchar(100));
Go
9. You can browse the
new table type in
Object Explorer
10. Create a stored a. In the same query window, delete the previous statement(s).
procedure that will b. Run the following statement(s) by clicking ‘Execute’ on the standard toolbar or
use the new table
by pressing ‘F5’
type
USE AdventureWorks
GO
CREATE PROCEDURE NewEmployee(@EmployeeDetails
EmployeeTableType READONLY)
As
BEGIN
INSERT INTO dbo.Employee
SELECT * FROM @EmployeeDetails
END
Page 17 of 18
What's new in SQL Server 2008 CTP4 for OLTP Applications
Tasks Detailed Steps
by pressing ‘F5’
use AdventureWorks
Go
DECLARE @NewEmployees EmployeeTableType
Note: After the routine is out of scope, the table-valued parameter is no longer
available. However, the type definition remains until it is dropped.
12. Verify the data in a. If the query window is open, delete all the statement(s) or click on ‘New Query’
dbo.Employee table in the standard toolbar.
b. Run the following statement(s) by clicking ‘Execute’ on the standard toolbar or
by pressing ‘F5’
USE AdventureWorks
GO
select * from dbo.Employee
Page 18 of 18