128
Everything you need to know to get started SQL SERVER DATA TOOLS WORKSHOP

SSDT Workshop @ SQL Bits X (2012-03-29)

Embed Size (px)

DESCRIPTION

Mastering SSDT with the DataDudeThis is your chance to hear the real story behind SSDT, directly from the man who built it. SQL Server Data Tools is effectively the 3rd version of the DataDude project, started in 2005 by Gert Drapers. SQL Server Data Tools (SSDT) lets you develop, test, and maintain SQL Server and SQL Azure databases offline on your desktop. SSDT's modern T-SQL development environment supports declarative model-driven development whether working connected or offline, and integrates with Visual Studio's project and application lifecycle management tools to enable team development and source code control support for SQL Server and SQL Azure databases. This master class will get you started using SSDT; provides you with the architectural ins and outs of schema management using SSDT; team oriented database development and leveraging the command line and programmatic interfaces that accompany SSDT for importing, comparing and deploying database schemas. Learn it from the DataDude himself.

Citation preview

Page 1: SSDT Workshop @ SQL Bits X (2012-03-29)

Everything you need to know to get started

SQL SERVER DATA TOOLSWORKSHOP

Page 3: SSDT Workshop @ SQL Bits X (2012-03-29)

[email protected]

Introduction

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

3

Page 4: SSDT Workshop @ SQL Bits X (2012-03-29)

Copyright and Disclaimer This presentation, including examples are provided for informational

purposes only, the author makes no warranties, either express or implied. Information in this presentation, including samples, URL and other Internet

Web site references, are subject to change without notice. Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under copyright, no part of this document may be

reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of the author.

The names of actual companies and products mentioned herein may be the trademarks of their respective owners.

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

4

Page 5: SSDT Workshop @ SQL Bits X (2012-03-29)

@DataDude 1986-1988 IBM Corporation 1988-1991 Ashton-Tate 1991-present Microsoft Corp.

SDE in the Developer Relations Group (the Netherlands) Senior Development Consultant in MCS (the Netherlands) SDE in Storage Engine (DBCC, Bulk Insert and Convert) Program Manager SQL-DMO and Query Analyzer Product Unit Manager SQL Server Management Tools Software Architect DTS 2005 (now SSIS) and SMO Software Architect MS-DTC, COM+, System.Transactions, WS-AT Software Architect SQL Server Customer Advisory Team (Europe) Principal Group Engineering Manager “Visual Studio Team System Database Edition” Principal Software Architect Azure Active Directory Principal Software Architect Cloud Programmability (SQL) Principal Group Program Manager SQL Server Data Platform

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

5

Page 6: SSDT Workshop @ SQL Bits X (2012-03-29)

Agenda SQL Server Data Tools Overview SQL Server Data Tools Installation Data-Tier Application Component (DAC) Declarative Database Development Connected Development Project Based Development Application Life Cycle Integration Programmatic and Command Line Usage

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

6

Page 7: SSDT Workshop @ SQL Bits X (2012-03-29)

SQL Server Data Tools Overview

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

7

Page 8: SSDT Workshop @ SQL Bits X (2012-03-29)

Database Development Challenges Databases are inherently stateful

Focus is on ALTER instead of CREATE Dependencies complicate scripts Errors are often found only when scripts are executed

Synchronizing application and database versions Database development often not integrated in to

application life cycle management and processes Targeting different SQL Server and SQL Azure

versions

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

8

Page 9: SSDT Workshop @ SQL Bits X (2012-03-29)

History of the “DataDude” Project Project funded April 2005 Project started July 1st 2005 Visual Studio 2005 Team System for Database Professionals Visual Studio 2008 Team System for Database Professionals Re-architected to be declarative model based system Visual Studio 2008 Team System for Database Professionals GDR

R2 Visual Studio 2010 Team System for Database Professionals Transitioned to SQL Server 2009 SQL Server Data Tools

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

9

Page 10: SSDT Workshop @ SQL Bits X (2012-03-29)

Introducing SQL Server Data Tools

CONNECTED DEVELOPMENT

PROJECT BASED DEVELOPMENT

SCHEMA DEPLOYMENT

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

10

Page 11: SSDT Workshop @ SQL Bits X (2012-03-29)

Connected Development

Drift Detection

Schema Comparison

SQL Server Object Explorer

T-SQL Editor

Table Designer

Error Information

T-SQL IntelliSense

View Code

View/Edit Data

T-SQL Debugging

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

11

Page 12: SSDT Workshop @ SQL Bits X (2012-03-29)

Project Based Development

Go To Definition

Find All References

Refactoring

Point-in-time Snapshots

T-SQL Static Code Analysis

Source Code Based

F5 Debugging & Testing with

LocalDB

Source Code Control

MSBuild

Headless Command Line

Tools

Visualize Schema Differences

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

12

Page 13: SSDT Workshop @ SQL Bits X (2012-03-29)

Schema Deployment

Incremental Schema

Deployment

Model Based DACPAC

SQL ScriptRetargeting

Support

SQL Server 2005SQL Server 2008SQL Server 2012

SQL Azure

Format

Engine

API & REDIST

Connected

DeclarativeStandard Formats

Multi-Targeting DAC

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

13

Page 14: SSDT Workshop @ SQL Bits X (2012-03-29)

DAC

SQL Server Data Tools SQL Server Management Studio

PublishCompare Sync

ImportExport

RegisterUnregister

SQL Azure Management Portal

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

Page 15: SSDT Workshop @ SQL Bits X (2012-03-29)

CONNECTED DEVELOPMENT

PROJECT BASED DEVELOPMENT

SCHEMA DEPLOYMENT

DeclarativeStandard Formats

Multi-Targeting DAC

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

Page 16: SSDT Workshop @ SQL Bits X (2012-03-29)

Resources SQL Server Data Tools Online Installation

http://msdn.microsoft.com/data/tools Get It Team Blog

http://blogs.msdn.com/b/ssdt/ MSDN Forum

http://social.msdn.microsoft.com/Forums/en-US/ssdt/threads Articles

MSDN Magazine Sept 2011 The "Juneau" Database Project

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

16

Page 17: SSDT Workshop @ SQL Bits X (2012-03-29)

SQL Server Data Tools Installation

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

17

Page 18: SSDT Workshop @ SQL Bits X (2012-03-29)

Setup Options Install from online Web Platform Installer

feed Install from SQL Server 2012 DVD Install from Visual Studio 11 Install from offline administrative install

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

18

Page 19: SSDT Workshop @ SQL Bits X (2012-03-29)

Supported OS Platforms Windows Vista SP2+ Windows 7 SP1+ Windows Server 2008 SP+ Windows Server 2008 R2 SP1+

Note: this is different then the Visual Studio 2010 platforms

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

19

Page 20: SSDT Workshop @ SQL Bits X (2012-03-29)

Visual Studio Requirements Visual Studio 2010 Integrated Shell+

SQL-CLR requires Visual Studio 2010 Standard+ Visual Studio 2010 SP1+

Notes: SQL Server 2012 only installs VS 2010 Integrated Shell

SP1 If you have an existing Visual Studio 2010 installation

you must install SP1 yourself (?KB VS2010 SP1 checks) Watch for mixed mode (?KB detecting mixed mode)

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

20

Page 21: SSDT Workshop @ SQL Bits X (2012-03-29)

SSDT Online Acquisition Install online via Web Platform Installer (WebPI)

SSDT download link for latest bits http://msdn.microsoft.com/en-us/data/hh297027

Requires WebPI v3.0 or better to be installed User is prompted on first attempt when not installed or

updated Automatically downloads all required new or updated

dependencies Invokes SSDTSetup.exe (chained installer)

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

21

Page 22: SSDT Workshop @ SQL Bits X (2012-03-29)

Installing from SQL Server 2012 DVD Install SSDT from SQL Server 2012 DVD image

Install VS 2010 SP1 shell if you do not have VS installed If you do have VS 2010 install, user needs to install

SP1! DVD puts down a “stub” which will acquire latest SSDT

bits online via (WebPI) See

http://sqlproj.com/index.php/2011/11/ssdt-setup-from-sql-server-2012/ for detailed step through

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

22

Page 23: SSDT Workshop @ SQL Bits X (2012-03-29)

Creating an Admin Install SSDTSetup.exe /layout C:\SSDTAdminInstall

Pulls down all the latest dependencies from the web Enables generating a local install point which can be

shared via a file share or USD drive DMZ or offline installation scenario

For detailed steps see: http://

sqlproj.com/index.php/2011/11/creating-an-administrative-install-for-ssdt

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

23

Page 24: SSDT Workshop @ SQL Bits X (2012-03-29)

SSDTSetup SSDTSetup.exe

Setup chainer (486 KB) SSDT\x86\SSDT.msi

Actual tools (5.75 MB) SSDT\x86\SSDTBuildUtilities.msi

MSBuild targets and utilities (970 KB) SSDT\x86\DbSqlPackageProvider.msi

Web Deploy Provider (729 KB)

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

24

Page 25: SSDT Workshop @ SQL Bits X (2012-03-29)

Setup Dependencies (payload directory) x86\DACFramework.msi x86\NDP40-KB2468871-v2-x86.exe x86\SharedManagementObjects.msi x86\SQLDom.msi x86\SqlLocalDB.msi x86\sqlls.msi x86\SQLSysClrTypes.msi x86\SSDTDBSvcExternals.msi x86\TSqlLanguageService.msi x86\VS10sp1-KB983509.exe x86\VSIntShell.exe

dotNetFx40_Full_x86_x64.exe NDP40-KB2544514-x86-x64.exe x64\DACFramework.msi x64\NDP40-KB2468871-v2-

x64.exe x64\

SharedManagementObjects.msi x64\SQLDom.msi x64\SqlLocalDB.msi x64\sqlls.msi x64\SQLSysClrTypes.msi x64\TSqlLanguageService.msi

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

25

Page 26: SSDT Workshop @ SQL Bits X (2012-03-29)

Setup Dependencies .NET 4.0 CLR runtime

dotNetFx40_Full_x86_x64.exe .NET 4.0 CLR general update

http://support.microsoft.com/kb/2468871 NDP40-KB2544514-x86-x64.exe

Update 4.0.2 for Microsoft .NET Framework 4 – Runtime Update http://support.microsoft.com/kb/2544514 NDP40-KB2544514-x86-x64.exe LocalDB support

Visual Studio 2010 integrated shell VSIntShell.exe

Visual Studio 2010 SP installer VS10sp1-KB983509.exe http://support.microsoft.com/kb/983509

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

26

Page 27: SSDT Workshop @ SQL Bits X (2012-03-29)

Setup Dependencies… SQL Server 2012 Data-Tier Application Framework v3.0

DACFramework.msi SQL Server 2012 Express LocalDB

SqlLocalDB.msi SQL Server 2012 Script DOM (parser)

SQLDom.msi SQL Server 2012 Language Services (compiler and binder)

sqlls.msi SQL Server 2012 CLR types (geo and hierarchy)

SQLSysClrTypes.msi SQL Server 2012 Management Objects (SMO)

SharedManagementObjects.msi SQL Server 2012 Language Services (IntelliSense parser)

TSqlLanguageService.msi

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

27

Page 28: SSDT Workshop @ SQL Bits X (2012-03-29)

Copyright © 2012 Gert Drapers - All Rights Reserved.

28

Headless Installation Install the five following components:

SQL Server 2012 Data-Tier Application Framework (DACFramework.msi) SQL Server 2012 Transact-SQL ScriptDom (SQLDOM.MSI) SQL Server 2012 Transact-SQL Compiler Service (SQLLS.MSI) Microsoft System CLR Types for Microsoft SQL Server 2012

(SQLSysClrTypes.msi) SQL Server Data Tools Build Utilities (SSDTBuildUtilities.msi)

NOTE: Need to copy from SSDT Administrative install point For detailed description see:

http://sqlproj.com/index.php/2012/03/headless-msbuild-support-for-ssdt-sqlproj-projects/

03/29/2012

Page 29: SSDT Workshop @ SQL Bits X (2012-03-29)

Visual Studio 2012 SQL Server Database Projects are a standard feature in

Visual Studio 2012 Replaces Visual Studio Database Projects Available in Visual Studio Professional edition and up Integrate shell version will be made available separately as

online download Visual Studio 2012 RTM will ship with the same functionality

as the SSDT version shipping with SQL Server 2012 RTM Round tripping of projects and project artifacts between

VS2010 and VS2012 hosted SQL Server Database Projects

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

29

Page 30: SSDT Workshop @ SQL Bits X (2012-03-29)

Setup Log Files dir %temp%\sql_SSDT_1000*.log

"C:\Users\gertd\AppData\Local\Temp\sql_SSDT_1000_20120218114502.log" "C:\Users\gertd\AppData\Local\Temp\sql_SSDT_1000_20120218114502_0_SQLLS11X64.log" "C:\Users\gertd\AppData\Local\Temp\sql_SSDT_1000_20120218114502_1_SQLDOM11X64.log" "C:\Users\gertd\AppData\Local\Temp\sql_SSDT_1000_20120218114502_2_TSQLLANGSVC11X64.log" "C:\Users\gertd\AppData\Local\Temp\sql_SSDT_1000_20120218114502_3_DACFX11X86.log" "C:\Users\gertd\AppData\Local\Temp\sql_SSDT_1000_20120218114502_4_DACFX11X64.log" "C:\Users\gertd\AppData\Local\Temp\sql_SSDT_1000_20120218114502_5_SQLLOCALDBX64.log" "C:\Users\gertd\AppData\Local\Temp\sql_SSDT_1000_20120218114502_6_SQLCLRTYPES11X86.log" "C:\Users\gertd\AppData\Local\Temp\sql_SSDT_1000_20120218114502_7_SQLCLRTYPES11X64.log" "C:\Users\gertd\AppData\Local\Temp\sql_SSDT_1000_20120218114502_8_SQLSMO11X86.log" "C:\Users\gertd\AppData\Local\Temp\sql_SSDT_1000_20120218114502_9_SQLSMO11X64.log" "C:\Users\gertd\AppData\Local\Temp\sql_SSDT_1000_20120218114502_10_SSDTEXTERNALS10.log“ "C:\Users\gertd\AppData\Local\Temp\

sql_SSDT_1000_20120218114502_11_SSDTBUILDUTILITIES10.log" "C:\Users\gertd\AppData\Local\Temp\sql_SSDT_1000_20120218114502_12_SSDT10.log"

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

30

Page 31: SSDT Workshop @ SQL Bits X (2012-03-29)

The foundation

Declarative Database Development

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

31

Page 32: SSDT Workshop @ SQL Bits X (2012-03-29)

Simple Schema Versioning Example

-- version 1CREATE TABLE dbo.Auction( id INT NOT NULL, name VARCHAR(25) NOT NULL, start DATETIME NULL, len INT NULL)

-- version 2ALTER TABLE dbo.Auction WITH CHECK ADD CONSTRAINT Au_PK

-- version 3ALTER TABLE dbo.Auction WITH CHECK ADD CONSTRAINT Au_SK UNIQUE (name)

Revision HistoryV 1 V 2 V 3

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

32

Page 33: SSDT Workshop @ SQL Bits X (2012-03-29)

Imperative Imperative scripts hard codes knowledge about:

The state of the target system: Presence of objects, dependencies, unbinding, rebinding Required ordering of operations

Cumulative changes need to be serialized v1v2v3v4v5 instead of v1v4v5

Validating the end-result against expected end-state is hard No easy way to compare expected vs. actual

Batching or separating multiple changes

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

33

Page 34: SSDT Workshop @ SQL Bits X (2012-03-29)

-- version 1 Add table dbo.AuctionIF OBJECT_ID (N'dbo.Auction', N'U') IS NULLBEGINCREATE TABLE dbo.Auction(`

id INT NOT NULL, name VARCHAR(25) NOT NULL, start DATETIME NULL, len INT NULL)END-- version 2 Add PK Au_PKIF NOT EXISTS (SELECT * FROM sys.key_constraints WHERE name = 'Au_PK' AND type = 'PK')BEGIN

ALTER TABLE Auction WITH CHECK ADD CONSTRAINT Au_PK PRIMARY KEY (id)END-- version 3 Add UC Au_SKIF NOT EXISTS (SELECT * FROM sys.key_constraints WHERE name = 'Au_SK' AND type = ‘UQ')BEGIN

ALTER TABLE Auction WITH CHECK ADD CONSTRAINT Au_SK UNIQUE (name)END

Imperative Versioning

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

34

Page 35: SSDT Workshop @ SQL Bits X (2012-03-29)

Declarative Define what you want in the form of a model Fill the model using a DSL (domain specific language)

T-SQL Use the model as the starting point “blueprint” for all

operations Deployment/publish, start by comparing the current state of the

target with the required state (blueprint) Use the resulting difference knowledge to programmatically

generate an deployment plan Use plan to create sequence of (SQL) statements required to make

the target state become like the blueprint state

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

35

Page 36: SSDT Workshop @ SQL Bits X (2012-03-29)

Declarative Versioning-- version 1CREATE TABLE dbo.Auction( id INT NOT NULL, name VARCHAR(25) NOT NULL, start DATETIME NULL, len INT NULL)

-- version 2CREATE TABLE dbo.Auction( id INT NOT NULL PRIMARY KEY, name VARCHAR(25) NOT NULL, start DATETIME NULL, len INT NULL)

-- version 3CREATE TABLE dbo.Auction( id INT NOT NULL PRIMARY KEY, name VARCHAR(25) NOT NULL UNIQUE, start DATETIME NULL, len INT NULL)

Revision HistoryV 1 V 2 V 3

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

36

Page 37: SSDT Workshop @ SQL Bits X (2012-03-29)

Declarative Schema Deployment

37

Model Compare

SourceSchemaModel

Reverse Engineer

TargetDB

TargetSchemaModel

Diff List

PlanOptimizer

Deploy Plan

Script Generator

ScriptExecutor

Additional schema artifacts

Incremental target update

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

37

Page 38: SSDT Workshop @ SQL Bits X (2012-03-29)

Imperative vs. Declarative Manual vs. generated / programmatic Point in time vs. always current Sequential vs. differential

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

38

Page 39: SSDT Workshop @ SQL Bits X (2012-03-29)

Script Fundamentals Requirement to be able

to round trip DSL artifacts Script Model

SQL script Parse Script fragment

Script fragment Script Gen SQL script

Script fragment is AST (Abstract Syntax Tree)

Script

Fragment

T-SQLParse

r

Script Gen T-SQL

Script

Fragment

A

TS

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

39

Page 40: SSDT Workshop @ SQL Bits X (2012-03-29)

ScriptDom SQL Server 2012 managed parser

Supports SQL Server 2005+ Class TSqlXXXParser

XXX = [80, 90, 100, 110] SQLDom.msi (redist x86/x64) Microsoft.SqlServer.TransactSql.ScriptDom.dll

C:\Program Files (x86)\Microsoft SQL Server\110\SDK \Assemblies\Microsoft.SqlServer.TransactSql.ScriptDom.dll

GAC

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

40

Page 41: SSDT Workshop @ SQL Bits X (2012-03-29)

Basic ScriptDom loopstatic void Main(string[] args){

bool initialQuotedIdentifiers = false;TSqlParser parser = new TSql110Parser(initialQuotedIdentifiers);

StreamReader sr = new StreamReader(@".\test.sql");IList<ParseError> errors;

TSqlFragment fragment = parser.Parse(sr, out errors);

StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());

Sql110ScriptGenerator scriptGen = new Sql110ScriptGenerator();scriptGen.GenerateScript(fragment, sw);

}

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

41

Page 42: SSDT Workshop @ SQL Bits X (2012-03-29)

Reusable Building Blocks Model Store Model Builder Model Serialization Model Comparison Model Copy

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

42

Page 43: SSDT Workshop @ SQL Bits X (2012-03-29)

Model Store Elements

Relationships Properties

Identifiers Annotations

Models are stored in ESE database or in-memory

*.dbmdl (Database Model) file extension used by local model cache.

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

43

Page 44: SSDT Workshop @ SQL Bits X (2012-03-29)

Model Builder The schema model is

populated using information from the AST representation of the SQL parser

From a model element you can recreate a script fragment (AST)

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

44

Page 45: SSDT Workshop @ SQL Bits X (2012-03-29)

Model Serialization Act of serializing and

de-serializing a schema model to and from disk or in-memory persistence model

Model is serialized as XML See digesting a DACPAC

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

45

Page 46: SSDT Workshop @ SQL Bits X (2012-03-29)

Digesting a .DACPAC OPC file

Unpack handler associated at file system level Rename .dacpac to .zip to open Compressed Versioned

SQL Server 2012 ships with DACPAC version 3.0.0.0 http://en.wikipedia.org/wiki/Open_Packaging_Con

ventions

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

46

Page 47: SSDT Workshop @ SQL Bits X (2012-03-29)

.DACPAC streams

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

47

Page 48: SSDT Workshop @ SQL Bits X (2012-03-29)

Reverse Engineer Reverse Engineering

is a special form of model builder, which scripts the content of a source database in the form of SQL scripts (model DSL input format) to construct a model

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

48

Page 49: SSDT Workshop @ SQL Bits X (2012-03-29)

Model Copy / Compare Comparing two

declarative models to determine the difference

The comparison engine exposes options for excluding object instances, object types, specific options on objects and certain common use cases

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

49

Page 50: SSDT Workshop @ SQL Bits X (2012-03-29)

Deployment & Schema Compare

Model Compare

Script

Fragment

Model Builder

SourceSchemaModel

Reverse EngineerDB

SchemaPackag

e

ModelSerializer

Script

Fragment

Model Builder

Reverse Engineer DB

SchemaPackag

e

ModelSerializer

TargetSchemaModel

Diff List

PlanOptimizer

Deploy Plan

Script Generator

DiffVisualizer

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

50

Page 51: SSDT Workshop @ SQL Bits X (2012-03-29)

Model Based

All schema objects are represented inside a model What is in the model is defined by the provider

To load/hydrate a model instance Parse SqlCodeDom, based on parsers Abstract Syntax Tree (AST) Interpretation Schema Model

Symbol list Object References (hard and soft dependencies)

Validation

.SQL Source Parse

SqlScriptDom

Interpret

Schema Model

Validate

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

51

Page 52: SSDT Workshop @ SQL Bits X (2012-03-29)

Component Stack

SQL Server Data Tools

DACFX

SQLLS SQLDOM SQLCLRTypes

SMOSystem.Data

.SqlClient

LocalDB

TSQL LanguageServices

NetFX Patches

Visual StudioVS2010 SP1,

VS2012

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

52

Page 53: SSDT Workshop @ SQL Bits X (2012-03-29)

DAC

Data-Tier Application Component

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

53

Page 54: SSDT Workshop @ SQL Bits X (2012-03-29)

What is a Data-Tier Application (dacpac)? Data-Tier Application Component (DAC)

SQL Server database registered as Data-Tier Application Component DAC is a self contained entity of a database used by an

application Unit of deployment and management

Authored along side your application code Compiled into a DAC package (DACPAC)

Extracted for existing databases Database extracted (DACPAC) or exported (BACPAC) into packages

Package is deployable to SQL Server and SQL Azure

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

54

Page 55: SSDT Workshop @ SQL Bits X (2012-03-29)

Copyright © 2012 Gert Drapers - All Rights Reserved.

55

DAC Framework v3.0 (DACFX) Core SQL Server redist component providing modeling, reverse

engineering and deployment capabilities

Managed Public API Exposes verbs for DACPAC and BACPAC package operations

Command-line tool (SqlPackage.exe) Executable over the managed public API

DACUnpack.exe Windows file handler for unpacking DACPACs to disk

DACFX Clients SqlPackage.exe, SSMS, SSDT, SAMP, I&E, VS Web and DB Publishing

03/29/2012

Page 56: SSDT Workshop @ SQL Bits X (2012-03-29)

DACFX Packages .dacpac - Packaged Schema representing the declarative model of the

database Not SQL, but a declarative Schema Model representation of the database Compiled from source code or Extracted from existing databases Deploy as new database or Incremental Upgrade an existing database Supports all SQL Server Objects and Types

.bacpac - Packaged Schema and Data representing the state of the database Composed of Schema Model with stateful properties and Data Streams Exported from existing databases Imported as a new database or populates an empty database Supports most SQL Server Objects and Types (SQL Azure parity)

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

56

Page 57: SSDT Workshop @ SQL Bits X (2012-03-29)

DAC Evolution

.dbschema v1.1

Visual Studio 2008

Database Edition GDR

.dbschema v1.2

Visual Studio 2010

Database Projects

.sqlx v1.0SQL Server Data Tools

CTP3

.dacpac v2.5SQL Server Data Tools

CTP4

.dacpac v1.0SQL 2008 R2

RC

.dacpac v1.05SQL 2008 R2

RTM

.dacpac v1.1SQL 2008 R2

SP1

.dacpac v2.0SQL 2012

RC0

.dacpac v3.0redist

consumed by all

SQL products

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

57

Page 58: SSDT Workshop @ SQL Bits X (2012-03-29)

DACFX Supported SQL Server Objects and Versions

SQL Server 2012 DACFX (v3.0) supports the full SQL Server Domain

Supports the following versions: SQL Server 2005 SQL Server 2008/R2 SQL Server 2012 SQL Azure

DACFX (v3.0) backwards compatibility DACFX 1.0, 1.1, and 2.0

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

58

Page 59: SSDT Workshop @ SQL Bits X (2012-03-29)

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

Page 60: SSDT Workshop @ SQL Bits X (2012-03-29)

SQL Server Data Tools Developer-focused toolset for authoring, building and publishing DACPACs

Experiences Enabled Connected Development Project Based Development Application Lifecycle & Tools

DACPAC verbs Author DACPAC declaratively and build from source Compare DACPAC to project, database, and other DACPACs Import DACPAC into project Publish DACPAC/Project to database

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

60

Page 61: SSDT Workshop @ SQL Bits X (2012-03-29)

Copyright © 2012 Gert Drapers - All Rights Reserved.

61

SQL Server Management Studio DBA-focused tools for deploying and extracting DACPACs; importing and exporting

BACPACs

Experiences Enabled Administration and Monitoring Connected Development Configuration and Task Wizards

DACPAC Verbs Supported Deploy Data Tier Application Register/Unregister Data Tier Application Upgrade Data Tier Application

BACPAC Verbs Supported Import Data Tier Application Export Data Tier Application Migrate Database to SQL Azure

03/29/2012

Page 62: SSDT Workshop @ SQL Bits X (2012-03-29)

SQL Azure Management Portal Subscriber and Operator oriented toolset for managing SQL Azure databases through

DAC Verbs

Experiences Enabled Connected Development Portal Based Tools Application Lifecycle support

DACPAC verbs supported Deploy Data Tier Application (create new database from dacpac) Upgrade Data Tier Application (upgrade database to match dacpac) Extract Data Tier Application (create dacpac from existing database)

BACPAC Verbs supports Export Data Tier Application (export database as bacpac to Windows Azure Blob Storage) Import Data Tier Application (import bacpac as new database from Windows Azure Blob

Storage)

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

62

Page 63: SSDT Workshop @ SQL Bits X (2012-03-29)

DAC Lifecycle

Develop (SSDT)• Import Database/

DACPAC• Author DAC• Compare and

Synchronize Source• Compile Database

Project to DACPAC• Publish DACPAC

Deploy (SSMS)• Deploy Package

• New Databases• Upgrade

Databases

Manage (SSMS and SAMP)• Export and Extract

Packages• Register Database as

DAC

Developer

DBA DBA/Admin

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

63

Page 64: SSDT Workshop @ SQL Bits X (2012-03-29)

64

Copyright © 2012 Gert Drapers - All Rights Reserved.

Imperative & Declarative Schema Authoring and Deployment

Connected Development

03/29/2012

Page 65: SSDT Workshop @ SQL Bits X (2012-03-29)

Connected Development

Copyright © 2012 Gert Drapers - All Rights Reserved.

65

SQL Server Object Explorer (SSOX) T-SQL Editor/Debugger/IntelliSense (New Query) Power-Buffer (New Object/View Code) Table Designer (View Designer) Data Editor (View Data) Execute Procedure/Function

Schema Compare

03/29/2012

Page 66: SSDT Workshop @ SQL Bits X (2012-03-29)

SQL Server Object Explorer (SSOX)

Copyright © 2012 Gert Drapers - All Rights Reserved.

66

The SQL Server navigation tool window Modeled after the SSMS Object Explorer Server scoped vs. Server Explorer which is database scoped Launch point for T-SQL Editor, Debugger, Object Execution, Designers, …

Supports connecting directly to SQL Server SQL Server {2005, 2008, 2008R2, 2012} and SQL Azure

Supports connecting directly to a (contained) database Constructs a partial model in the background and under the covers

Supports: Cascading Drops and Renames Drift detection

Automatically updates the tree/source as changes occur on the server SSOX is a DSRef drag source

03/29/2012

Page 67: SSDT Workshop @ SQL Bits X (2012-03-29)

SSOX Drift Detection Controlling Refresh Rate

HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\10.0\SQLDB\DialogPage\Microsoft.VisualStudio.Data.Tools.Package.ToolsOptions.OnlineEditing.OnlineEditingOptionsSettings]"PollingDriftCheckSeconds"="30"

Defaults to 30. If set to <= 0 will be infinite (3 days). Cannot be less than 30.

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

67

Page 68: SSDT Workshop @ SQL Bits X (2012-03-29)

68

Copyright © 2012 Gert Drapers - All Rights Reserved.

T-SQL Editor/Debugger/IntelliSense T-SQL IntelliSense

SQL Server 2005+ incl. SQL Azure

Result to Grid | Text | File Execution modes

Parse Estimated Execution Plan Execute Execute with debugger

Other Options: Actual Execution Plan SQLCMD mode Client Statistics Code snippets

Reusable debug script generation

Keyboard shortcut mapping alignment Out-of-band delivery of

SSMS profile 03/29/2012

Page 69: SSDT Workshop @ SQL Bits X (2012-03-29)

T-SQL Editor Keyboard Mapping

Copyright © 2012 Gert Drapers - All Rights Reserved.

69

CommandsSSMS Keyboard Compatibility Settings

SQL Server Development Settings

Execute CTRL+E / ALT+X / CTRL+SHIFT+E CTRL+SHIFT+EExecute w/Debugger ALT+F5 ALT+F5

Cancel Query Execution ALT+BREAK ALT+BREAK

Include Actual Execution Plan CTRL+M CTRL+D, A

Display Estimated Execution Plan CTRL+L CTRL+D, E

Results As Grid CTRL+D CTRL+D, G

Results As Text CTRL+T CTRL+D, T

Results As File CTRL+SHIFT+F CTRL+D, FShow/Hide Results CTRL+R CTRL+D, R

New File CTRL+N None defined03/29/2012

http://blogs.msdn.com/b/ssdt/archive/2012/03/07/using-ssms-keyboard-mappings-in-ssdt.aspx

Page 70: SSDT Workshop @ SQL Bits X (2012-03-29)

T-SQL Editor/Debugger/IntelliSense Toolbar consistency with SSMS

Parse

Display Estimated Execution

Plan

IncludeActual

Execution Plan

SQLCMD Mode

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

70

Page 71: SSDT Workshop @ SQL Bits X (2012-03-29)

T-SQL Editor/Debugger/IntelliSense Context menu consistency improvement

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

71

Page 72: SSDT Workshop @ SQL Bits X (2012-03-29)

Power-Buffer/Table Designer Declarative deferred online schema management

Brings “project-oriented” features to online / connected development Live errors and warnings as changes are made against code “Project-oriented” validation during editing

Table designer Code-behind designer Works on top of Power-Buffer (partial model) or project model Strives to be the same experience online as experienced in the project system

Coordinated execution of buffers against the same target Diff scripts by default are transactional

(Tools\Options\SQL Server Tools\Online Editing\Include transactional scripts)

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

72

Page 73: SSDT Workshop @ SQL Bits X (2012-03-29)

Data Editor

Copyright © 2012 Gert Drapers - All Rights Reserved.

73

View and Edit Data via grid editor Copy-Paste support Generate as INSERT to file or query

window

03/29/2012

Page 74: SSDT Workshop @ SQL Bits X (2012-03-29)

Execute Procedure/Function

Copyright © 2012 Gert Drapers - All Rights Reserved.

74

Dialog based input for parameter collection Generates SQL script for

execution/debugging

03/29/2012

Page 75: SSDT Workshop @ SQL Bits X (2012-03-29)

Schema Compare

Copyright © 2012 Gert Drapers - All Rights Reserved.

75

Performs a model comparison between: Databases Projects DACPAC files (snapshots, build outputs, extracts)

Supports: Refactoring log SQLCMD variables

03/29/2012

Page 76: SSDT Workshop @ SQL Bits X (2012-03-29)

Schema Compare

Copyright © 2012 Gert Drapers - All Rights Reserved.

76

Behavioral comparison options Object type filtering

Application scoped Non-application scoped

03/29/2012

Page 77: SSDT Workshop @ SQL Bits X (2012-03-29)

Schema CompareNext and Previous move between top-level objects, no longer expand the tree.

Simpler set of columns are aligned around the central action column echoes alignment of schemas above

Group-by Action is the default

Same column order is used for all groupings; cleaner and easier to parse regardless of grouping

Schema no longer used as a parent for top-level objects; only shown when changed

Improved script diff’ algorithm for tables emphasizes columns as unit of change. Gives much improved alignment. Also improved look

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

77

Page 78: SSDT Workshop @ SQL Bits X (2012-03-29)

Schema Compare78

Copyright © 2012 Gert Drapers - All Rights Reserved. 03/29/2012

Page 79: SSDT Workshop @ SQL Bits X (2012-03-29)

Offline and Isolated Schema Authoring and Deployment

Project Based Development

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

79

Page 80: SSDT Workshop @ SQL Bits X (2012-03-29)

Project Based Development

Copyright © 2012 Gert Drapers - All Rights Reserved.

80

Database Projects Database References Publishing Projects

03/29/2012

Page 81: SSDT Workshop @ SQL Bits X (2012-03-29)

Database Projects

Copyright © 2012 Gert Drapers - All Rights Reserved.

81

Creating and Importing Additional Language Services Isolated Sandbox Database Schema Snapshots Build Time Validation T-SQL Static Code Analysis SQL-CLR Support Pre & Post Deployment Scripts

03/29/2012

Page 82: SSDT Workshop @ SQL Bits X (2012-03-29)

Creating and Importing

Copyright © 2012 Gert Drapers - All Rights Reserved.

82

Single project template VSDB note:

No distinction between server or user projects Server projects are projects with target database set to master

Import from: Database DACPAC SQL Script (multi-file import enabled) Selective import by using Schema Compare

“Create New Project” from SSOX Inherits version from source database

03/29/2012

Page 83: SSDT Workshop @ SQL Bits X (2012-03-29)

Project Files

Copyright © 2012 Gert Drapers - All Rights Reserved.

83

*.sqlproj MSBuild based project file

*.sqlproj.user MSBuild based user specific settings (merged-in)

*.dbmdl <project>.dbmdl is the schema model cache file

Will get rebuild when deleted Should not get checked-in, in to source code control (SSC)

ESE (Extensible Storage Engine) database file http://technet.microsoft.com/en-us/library/bb310772.aspx Managed interface http://managedesent.codeplex.com/

03/29/2012

Page 84: SSDT Workshop @ SQL Bits X (2012-03-29)

Folder Structure

Copyright © 2012 Gert Drapers - All Rights Reserved.

84

Options on import: None Schema Object type Schema\Object type

03/29/2012

Page 85: SSDT Workshop @ SQL Bits X (2012-03-29)

Project Directory Guidelines The projects directory root path is the only thing that matters!

This is determined based on the location of the .sqlproj file You have to stay underneath this location All file paths are relative to this location

The initial project structure is a good starting point and works for all objects

You can change the complete directory structure But if you remove the default layout we do not know where the

automatically place objects when using “Add New Item” or when using Import Script Result: Objects will be placed in the project root location

Advice: Extend the base structure, do not replace

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

85

Page 86: SSDT Workshop @ SQL Bits X (2012-03-29)

Project Directory Guidelines… Be aware of MAX_PATH (260 characters)

All relative file locations must fit within MAX_PATH Your SCC environment might have problems when you

exceed MAX_PATH Choose your project root location wisely

Filenames encoding: objectname.sql or schema.objectname.sql <sysname>.<sysname>.sql sysname = max 128 characters

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

86

Page 87: SSDT Workshop @ SQL Bits X (2012-03-29)

Project Directory Guidelines…

Copyright © 2012 Gert Drapers - All Rights Reserved.

87

Be aware of empty directories when using Source Code Control Placement of new objects when using Add New Item

or when using Schema Compare end up in the project root when location is not present

Do not host more then one table inside script file Table designer will not see additional tables inside

script

03/29/2012

Page 88: SSDT Workshop @ SQL Bits X (2012-03-29)

Include files Include files

Add Existing Item As Link

Use to share files between projects VSDB note:

SQL Server Database Projects do not support partial projects

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

88

<Build Include="..\Northwind2\dbo\Tables\Categories.sql"><Link>Categories.sql</Link>

</Build>

Page 89: SSDT Workshop @ SQL Bits X (2012-03-29)

Additional Language Services

Copyright © 2012 Gert Drapers - All Rights Reserved.

89

Projects enable additional language services Go To Definition Find All References Refactoring

Rename (sp_rename) Move to Schema (ALTER SCHEMA schema_name TRANSFER) Wildcard Expansion Fully Quality Name

Invoked from Editor, Table Designer and SQL Server Object Explorer for project based schema objects

03/29/2012

Page 90: SSDT Workshop @ SQL Bits X (2012-03-29)

Isolated Sandbox

Copyright © 2012 Gert Drapers - All Rights Reserved.

90

F5 – Run by default configure to use the LocalDB instance associated with the project Data Source=(localdb)\<project name>;Initial

Catalog=<project name>;Integrated Security=True;Pooling=False;Connect Timeout=30

To retarget to a different SQL Server version and/or shared SQL Server change: Project properties\Debug\Target Connection String

03/29/2012

Page 91: SSDT Workshop @ SQL Bits X (2012-03-29)

Copyright © 2012 Gert Drapers - All Rights Reserved.

91

SQL Server 2012 Express LocalDB Simplify embedded usage of SQL Server

Simple installation Zero-admin database

Same programming surface as User Instances of SQL Express The same sqlservr.exe as in service-based SQL Server with the same language and features Connection through the same client-side APIs, using a new connection string option

SQL Server programming symmetry Installation similar to .NET Framework model

One set of installed binaries (per major version), no global configuration, no constantly-running service

Activation model similar to RANU (Access, SQL Compact, …) The application connects to a database file, SQL activates automatically No service: SQL process starts in the context of current user

Default connection string (localdb)\v11.0

http://msdn.microsoft.com/en-us/library/hh510202(v=SQL.110).aspx

03/29/2012

Page 92: SSDT Workshop @ SQL Bits X (2012-03-29)

Copyright © 2012 Gert Drapers - All Rights Reserved.

92

LocalDB Setup Packaged in a small single MSI

SqlLocalDB.msi x32 (27.5 MB) SqlLocalDB.msi x64 (32.7 MB)

Single registry key to check if LocalDB Runtime is installed

Silent installation

Side-by-side versions

[HKLM\SOFTWARE\Microsoft\Microsoft SQL Server Local DB\Installed Versions\11.0]

SQLLocalDB.msi /Quiet /IAcceptLicenseTerms

C:\ Program Files\ Microsoft SQL Server\ 110\ LocalDB\ sqlservr.exe […] 120\ LocalDB\ sqlservr.exe […]

03/29/2012

Page 93: SSDT Workshop @ SQL Bits X (2012-03-29)

93

Same Machine, Same User Context

C:\Users\johnsmith\AppData\Local\Microsoft\Microsoft SQL

Server\LocalDB\v11.0\...

C:\Program Files\Microsoft SQL Server\110\LocalDB\Binn\sqlservr.exe

LocalDB Process ModelEach user has their LocalDB instance (process)

Created and started lazily when needed

All SQL Server process infrastructure is hidden Including System Databases (master & friends)

SQL Server

App1 App1DB

Infrastructure – Hidden

User Code – Visible

tempmodel

msdb

master

new SqlConnection(

“Server=(LoacalDB)\v11.0;AttachDBFile=|DataDirectory|\App1DB.mdf“);

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

Page 94: SSDT Workshop @ SQL Bits X (2012-03-29)

Database Schema Snapshots

Copyright © 2012 Gert Drapers - All Rights Reserved.

94

Point-in-time representation of schema model Model must be “error-free” state Produces same file format (*.dacpac) as build or extract

To be used for: Versioning Comparison Sharing schema definitions or changes …

03/29/2012

Page 95: SSDT Workshop @ SQL Bits X (2012-03-29)

Build Time Validation

Copyright © 2012 Gert Drapers - All Rights Reserved.

95

Build validates the complete model Platform specific validation

Example – Enforces clustered index requirement on tables in SQL Azure Integration with Compiler Services

Engine-level build-time validation without schema deployment Leverages CDB and SQL2012 engine capabilities

Pre & Post Deployment scripts Build validates syntactical correctness of pre- and post- deployment

scripts using the SQLCMD variables Build serializes the model in to model.xml stream

inside .dacpac file

03/29/2012

Page 96: SSDT Workshop @ SQL Bits X (2012-03-29)

Copyright © 2012 Gert Drapers - All Rights Reserved.

96

Build Actions Build - This script will be interpreted as T-SQL (or an XML Schema in the

case of XML Schema Collections) and reflected in the T-SQL Model Compile - C# or VB (depending on your project properties) to be deployed

as a SQLCLR assembly None - Use this for any loose artifacts (like text files) PreDeploy - The pre deployment master script (there can only be one) PostDeploy - The post deployment master script (there can only be one) RefactorLog - This is a refactor log which notifies the DaxFx deployment

engine that particular changes should be interpreted as refactoring operations and not drop/adds

Build Extension Configuration - Reserved for future use, for this is interpreted as None

03/29/2012

Page 97: SSDT Workshop @ SQL Bits X (2012-03-29)

Build Output Build output path

.\sql\debug .\sql\release

Intermediate output path .\obj\debug .\obj\release

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

97

Page 98: SSDT Workshop @ SQL Bits X (2012-03-29)

T-SQL Static Code Analysis Rule set Suppression

File level Project level

MSBuild support

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

98

Page 99: SSDT Workshop @ SQL Bits X (2012-03-29)

SQL-CLR Support

Copyright © 2012 Gert Drapers - All Rights Reserved.

99

Supports: Embedded C# or VB.NET code artifacts

Deployed as SQL-CLR objects Binary assembly deployment and SQL-CLR

object association Example:

CREATE TYPE [ClrSchema].[IPv4]EXTERNAL NAME [IPAssembly].[IPv4];

03/29/2012

Page 100: SSDT Workshop @ SQL Bits X (2012-03-29)

Pre & Post Deployment Scripts Can be used for any “arbitrary” SQL actions that you need to

perform “before” and “after” deployment of the schema objects The pre deployment block comes after the generic database

context validation block The post deployment block comes after the schema objects

block Pre and post deployment scripts must be idempotent

They are “executed” each deployment! http://blogs.msdn.com/gertd/archive/2007/01/25/idempotent-scripts-r

equired.aspx

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

100

Page 101: SSDT Workshop @ SQL Bits X (2012-03-29)

Pre & Post Deployment Scripts… The pre or post deployment master files are identified

in the .sqlproj file through special item type tags <PreDeploy Include="Scripts\Pre-Deployment\

Script.PreDeployment.sql"> <SubType>Code</SubType></PreDeploy><PostDeploy Include="Scripts\Post-Deployment\Script.PostDeployment.sql"> <SubType>Code</SubType></PostDeploy>

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

101

Page 102: SSDT Workshop @ SQL Bits X (2012-03-29)

Pre & Post Deployment Scripts… Include files using :r (SQLCMD include) File paths of includes are relative to the pre or post

deployment master file Included files must exist, even when empty

Not existing include fail will cause an error state at build time Include files have to be tagged “NotInBuild”

<NotInBuild Include="Scripts\Post-Deployment\DatabaseObjectOptions.sql"> <SubType>NotInBuild</SubType></NotInBuild>

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

102

Page 103: SSDT Workshop @ SQL Bits X (2012-03-29)

SQLCMD Variables Restrictions Substitution order (vs. sqlcmd.exe) MSBuild mapping SQLCMD variables can be used in:

Pre & post deployment scripts Within schema objects variable have to be placed between square

brackets or double or single quotes [$(var)] or “$(var)” or ‘$(var)’

!!!Cannot be used as object identifiers!!! Variables are defined and store in ???

Configuration dependent ???

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

103

Page 104: SSDT Workshop @ SQL Bits X (2012-03-29)

Database References

Copyright © 2012 Gert Drapers - All Rights Reserved.

104

master and msdb references 3-4 part names Composition

03/29/2012

Page 105: SSDT Workshop @ SQL Bits X (2012-03-29)

Database References Literal vs. variable references

Literal can be used for 2 and 3-part names only Self vs. 3- vs. 4-part name

Same database Different database, same server Different database, different server

Build order and circular references

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

105

Page 106: SSDT Workshop @ SQL Bits X (2012-03-29)

Database References File based references (*.dacpac)

Create directory underneath the project root to store dacpac files, to ensure it is a relative path and transports correctly with the project in source code control Example: Reference\*.dacpac

Make sure to point to a v3.0 .dacpac

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

106

Page 107: SSDT Workshop @ SQL Bits X (2012-03-29)

Copyright © 2012 Gert Drapers - All Rights Reserved.

107

master and msdb references master references can be explicit or implicit

master.dbo.<object> or master.sys.<object> <Annotation Type="AliasedToDbo" />

Global namespace <Annotation Type="GloballyScoped" />

Example: select * from master.dbo.sysprocesses

select * from master.sys.sysprocessesselect * from sysprocesses

msdb references are always explicit 3-part references Example:

select job_id from msdb.dbo.sysjobs

03/29/2012

Page 108: SSDT Workshop @ SQL Bits X (2012-03-29)

3- & 4-part Names

Copyright © 2012 Gert Drapers - All Rights Reserved.

108

3-part name usage: SELECT * FROM

[LiteralDBRef].[Schema1].[Table1] SELECT *

FROM [$(DBRef)].[Schema1].[Table1] 4-part name usage:

SELECT * FROM [$(ServerRef)].[$(DBRef)].[Schema1].[Table1]

SELECT * FROM [$(ServerRef)].[LiteralDBRef].[Schema1].[Table1]

03/29/2012

Page 109: SSDT Workshop @ SQL Bits X (2012-03-29)

Composition

Copyright © 2012 Gert Drapers - All Rights Reserved.

109

Composite Projects Schema distributed across multiple

database projects Enable division of schema along security or

organizational boundaries Published as “one” logical database

Referenced objects brought into parent model on build

Controlled by “Include Composite Objects” advanced publish option

Removes the need for separate deployment of composing projects

03/29/2012

Page 110: SSDT Workshop @ SQL Bits X (2012-03-29)

Publishing Projects

Copyright © 2012 Gert Drapers - All Rights Reserved.

110

Multi-targeting Deployment Options

03/29/2012

Page 111: SSDT Workshop @ SQL Bits X (2012-03-29)

Multi-Targeting

Copyright © 2012 Gert Drapers - All Rights Reserved.

111

Target version aware: SQL Server 2005 SQL Server 2008 & SQL Server 2008 R2 SQL Server 2012 SQL Azure

Publish Direct, via SQL script, or DACPAC snapshots

03/29/2012

Page 112: SSDT Workshop @ SQL Bits X (2012-03-29)

Publishing Profiles MSBuild fragment for sharing

deployment settings

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

112

<?xml version="1.0" encoding="utf-8"?><Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <IncludeCompositeObjects>True</IncludeCompositeObjects> <TargetDatabaseName>DinnerNow</TargetDatabaseName> <DeployScriptFileName>TestSqlProj.sql</DeployScriptFileName> <TargetConnectionString>Data Source=gmf4vyn24r.database.windows.net;

User ID=gertd@gmf4vyn24r;Pooling=False</TargetConnectionString> <ProfileVersionNumber>1</ProfileVersionNumber> </PropertyGroup></Project>

Page 113: SSDT Workshop @ SQL Bits X (2012-03-29)

Deployment Options

Copyright © 2012 Gert Drapers - All Rights Reserved.

113

Options to influence deployment behavior Ignore* Deployment validation options

Do not alter Change Data Capture objects

Do not ALTER replicated objects Verify deployment

Deploy without a safety-net

03/29/2012

Page 114: SSDT Workshop @ SQL Bits X (2012-03-29)

Team development and central build services

Application Life Cycle Integration

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

114

Page 115: SSDT Workshop @ SQL Bits X (2012-03-29)

Application Life Cycle Integration MSBuild Support Source Code Control Integration Build Server Integration

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

115

Page 116: SSDT Workshop @ SQL Bits X (2012-03-29)

Copyright © 2012 Gert Drapers - All Rights Reserved.

116

MSBuild Support MSBuild tasks for:

Build Publish

Note: requires using a publishing profile T-SQL Static Code Analysis

SSDTBuildUtilities.msi Redist for MSBuild specific components http://sqlproj.com/index.php/2012/03/headless-msbuild

-support-for-ssdt-sqlproj-projects/

03/29/2012

Page 117: SSDT Workshop @ SQL Bits X (2012-03-29)

Source Code Control Support

Copyright © 2012 Gert Drapers - All Rights Reserved.

117

SQL Server Data Tools works with all Visual Studio Source Code Control (SCC) providers SQL Server Database Projects fully

integrate with SCC, no need to do anything special

03/29/2012

Page 118: SSDT Workshop @ SQL Bits X (2012-03-29)

Build Server Integration

Copyright © 2012 Gert Drapers - All Rights Reserved.

118

Building Use MSBuild

Deploying/publishing Use MSBuild (requires publish profile) Use SqlPackage.exe

Suggestion: use answer file, override from command line

03/29/2012

Page 119: SSDT Workshop @ SQL Bits X (2012-03-29)

Programmatic and Command Line

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

119

Page 120: SSDT Workshop @ SQL Bits X (2012-03-29)

SqlPackage.exe Part of DACFramework.msi (redist) Location:

%ProgramFiles(x86)%\Microsoft SQL Server\110\DAC\bin

/Action: {Extract|DeployReport|DriftReport|Publish|Script}

Supports response file @file Command line override settings in response file

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

120

Page 121: SSDT Workshop @ SQL Bits X (2012-03-29)

DacUnpack.exe C:\Program Files (x86)\Microsoft SQL

Server\110\DAC\bin Explorer file handler for

unpacking .DACPAC Note: not a command line tools

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

121

Page 122: SSDT Workshop @ SQL Bits X (2012-03-29)

MSBuild Tasks MSBuild /t:Build | Publish |

StaticCodeAnalysis Target file

C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

122

Page 123: SSDT Workshop @ SQL Bits X (2012-03-29)

MSBuild

Copyright © 2012 Gert Drapers - All Rights Reserved.

123

msbuild /t:build sqlproj-nw.sqlproj msbuild /t:publish sqlproj-nw.sqlproj

/p:SqlPublishProfilePath=sqlproj-nw.publish.xml

msbuild /t:StaticCodeAnalysis sqlproj-nw.sqlproj

03/29/2012

Page 124: SSDT Workshop @ SQL Bits X (2012-03-29)

124

Copyright © 2012 Gert Drapers - All Rights Reserved.

MSBuild Target Definitions PreBuildEvent BeforeBuild AfterBuild PostBuildEvent

BeforePublish PrePublishEvent PostPublishEvent AfterPublish

03/29/2012

<Target Name="BeforePublish">     <Message Text="BeforePublish message" />  </Target> <Target Name="AfterPublish">     <Message Text="AfterPublish message" />  </Target>

<Target Name="BeforeBuild"> <Message Text="BeforeBuild message" /></Target><Target Name="AfterBuild"> <Message Text="AfterBuild message" /></Target>

Page 125: SSDT Workshop @ SQL Bits X (2012-03-29)

Microsoft.SqlServer.Dac.dll Public API

Implements the common DAC verbs Deploy, ExportBacpac, Extract,

GenerateCreateScript, GenerateDeployReport, GenerateDeployScript, GeneratedriftReport, ImportBacpac, Register, Unregister, Unpack

C:\Program Files (x86)\Microsoft SQL erver\110\DAC\bin\Microsoft.SqlServer.Dac.dll

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

125

Page 126: SSDT Workshop @ SQL Bits X (2012-03-29)

Summary

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

126

Page 127: SSDT Workshop @ SQL Bits X (2012-03-29)

SQL Server Data Tools – Summary Developer-focused toolset to author, debug and publish

SQL Server & SQL Azure databases

Supports SQL Platform Free, web updates for SQL Server and SQL Azure releases Works in concert with other SQL Server tooling (SAMP, SSMS)

Compatible with your development environment Supports Visual Studio 2010 & Visual Studio 11 Migrates VS2010 database projects

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.

127

Page 128: SSDT Workshop @ SQL Bits X (2012-03-29)

Widescreen Test Pattern (16:9)

Aspect Ratio Test

(Should appear circular)

16x9

4x3

03/29/2012Copyright © 2012 Gert Drapers - All Rights Reserved.