SQL Server 2008 Top New Features

SQL Server 2008 Top New Features

Enterprise Data Platform

Policy-Based Management

Policy-Based Management enables the efficient management of multiple SQL Server instances from a single location. Easily create policies that control security, database options, object naming conventions, and other settings at a highly granular level. Policies can evaluate servers for compliance with a set of predefined conditions and prevent undesirable changes being made to servers.

Additional Resources

  • Management is centralized, thereby reducing the need to configure each server separately
  • Administration is simplified, reducing the effort required to maintain standardization and compliance, even in complex environments
  • Configuration is straightforward and can be done entirely within SQL Server Management Studio
  • Out-of-the-box predefined policies make it easy to get started
  • Backwards compatibility supports managing instances of SQL Server 2008, SQL Server 2005, and SQL Server 2000

Performance Data Collection (Data Collector)

The Data Collector provides a convenient way to collect, store, and view performance data automatically. It collects disk usage, server activity, and query statistics data, which it loads in a management data warehouse and performance data can be reviewed in SQL Server Management Studio or by using third-party tools.

Additional Resources

  • Setup wizard makes configuration simple
  • Data collection is automated by using set of SQL Server Agent jobs and SQL Server Integration Services packages
  • Management is centralized so data collection can be easily configured and results can be viewed in one place
  • Performance impact is minimal because the data collected can be cached and uploaded to the data warehouse later

Data Compression

Data compression reduces the amount of storage space needed to store tables and indexes, which enables more efficient storage of data. Data Compression does not require changes be made to applications in order to be enabled.

Additional Resources

  • Save disk storage
  • Enable compression option for individual tables or indexes
  • Configuration is easy using the Data Compression wizard
  • Applications do not need to be reconfigured as SQL Server handles compression and decompression of data
  • Compression can improve disk I/O and memory utilization

Resource Governor

The Resource Governor enables administrators to control and allocate CPU and memory resources to high priority applications. This enables predictable performance to be maintained and helps avoid performance from being negatively affected by resource-intense applications or processes

Additional Resources

  • Prioritize applications, users, and computers competing for the same resources
  • Prevent runaway queries that hold resources for extended periods of time
  • Limitations are not enforced when there is no competition for resources

Transparent Data Encryption

Transparent Data Encryption enables data to be stored securely by encrypting the database files. If the disks that contain database files become compromised, data in those files is protected because that data can only be de-encrypted by an authorized agent. SQL Server performs the encryption and de-encryption directly, so the process is entirely transparent to connecting applications. Applications can continue to read and write data to and from the database as they normally would. Backup copies of encrypted database files are also automatically encrypted.

Additional Resources

  • Implements strong encryption keys and certificates to secure data
  • Applications do not need to be modified to support Transparent Data Encryption
  • Enables compliance with data privacy regulations
  • Does not increase the size of the database

External Key Management / Extensible Key Management

External Key Management enables certificates and encryption keys to be stored using third-party hardware security modules that are designed specifically for this purpose. Storing the keys separately from the data enables a more extensible and robust security architecture.

Additional Resources

  • Helps organizations comply with data privacy regulations
  • Reduces administration requirements when there are multiple keys and certificates to manage
  • Standard interface supports third party hardware security modules

Data Auditing

Data Auditing provides a simple way to track and log events relating to your databases and servers. You can audit logons, password changes, data access and modification, and many other events. Tracking these events helps maintain security and can also provide valuable troubleshooting information. The results of audits can be saved to file or to the Windows Security or Application logs for later analysis or archiving.

Additional Resources

  • Enables compliance with security regulations
  • Simple configuration using SQL Server Management Studio
  • Minimal impact on performance because audit data is stored outside of SQL Server database files

Hot-Add CPUs and Hot-Add Memory

Hot-add CPUs, a feature available with the 64-bit edition SQL Server Enterprise, allows CPUs to be dynamically added to servers as needed, without the need to shut down the server or limit client connections. Hot-add memory enables memory to be added in the same way.

Additional Resources

  • Dynamically add memory and processors to servers without incurring downtime

Streamlined Installation

The SQL Server 2008 installation process has been streamlined to be easier and more efficient. Individual SQL Server components, such as Database Services, Analysis Services, and Integration Services, can be optionally selected for installation. Failover cluster support configuration has also been added to the installation.

Additional Resources

  • Easier to install SQL Server
  • Setup now advises of configuration problems such as installation pre-requisites, which helps streamline the installation process

Server Group Management

Server Group management enables T-SQL queries to be issued against multiple servers from a single Central Management Server, which simplifies administration. Stream results of multi-server queries into a single result set or into multiple result sets enables the option of evaluating policies against a server group.

Additional Resources

  • Management is centralized, so servers do not need to be configured individually
  • Provides a simpler administration model for policy evaluation

Upgrade Advisor

The Upgrade Advisor generates a report that highlights any issues that might hinder an upgrade. This provides administrators detailed information that can be used to prepare for upgrades.

Additional Resources

  • Helps streamline the upgrade process by identifying upgrade issues in advance
  • Can be used to analyze both local and remote systems
  • Upgrade Advisor reports provide links to technical information that assist in addressing upgrade problems

Partition Aligned Indexed Views

Indexed Views let SQL Server persist the results of a view, instead of having to dynamically combine the results from the individual queries in the view definition. Indexed Views can now be created to follow the partitioning scheme of the table that they reference. Indexed views that are aligned in this manner do not need to be dropped before a partition is switched out of the partitioned table, as was the case with SQL Server 2005 indexed views.

Additional Resources

  • Indexed views improve performance with very large partitioned tables, such as fact tables in data warehouses.

Backup Compression

Backup compression enables the backup of a database to be compressed without having to compress the database itself. All backup types, including log backups, are supported and data is automatically uncompressed upon restore.

Additional Resources

  • Save storage space
  • Compressed backups can be stored on tape or on disk
  • Simple configuration using SQL Server Management Studio
  • Default state of all backups on a server to be compressed can be configured

Extended Events

The extended events infrastructure provides an in-depth troubleshooting tool that enables administrators to address difficult-to-solve problems more efficiently. Administrators can investigate excessive CPU usage, deadlocks, and application time outs as well as many other issues. Extended events data can be correlated with Windows events data to obtain a more complete picture that will aid in problem resolution.

Additional Resources

  • Provides detailed information about low level events in SQL Server
  • Reduces the time required to troubleshoot complex problems
  • Provides access to event data that has previously been difficult to obtain

Dynamic Development

Grouping Sets

Use GROUPING SETS to obtain results similar to those generated by using CUBE and ROLLUP, however GROUPING SETS is more flexible, offers better performance, and is ANSI SQL 2006 compliant. GROUPING SETS enables the GROUP BY clause to generate multiple grouped aggregations in a single result set. It is equivalent to using UNION ALL to return a result set from multiple SELECT statements, each of which has a GROUP BY clause.

Additional Resources

  • Simplifies the process of writing basic reports that include aggregates
  • Offers better performance and flexibility compared with other ways of accessing the same data

MERGE Operator

The new MERGE operator streamlines the process of populating a data warehouse from a source database. For example, rows that get updated in the source database will probably already exist in the data warehouse but rows that are inserted into the source database will not already exist in the data warehouse. The MERGE statement distinguishes between the new and updated rows from the source database so that the appropriate action (insert or update) can be performed against the data warehouse in one single call.

Additional Resources

  • Provides a streamlined mechanism for performing ‘UPSERT (INSERT and UPDATE) operations
  • Reduces the need to use other less performance-efficient methods such as Lookup transformations, to achieve the same functionality
  • Provides an efficient, granular level of control

LINQ

Language Integrated Query (LINQ) is a .NET Framework version 3.5 feature that provides developers with a common syntax to query any data source from client applications. Using LINQ to SQL or LINQ to Entities, developers can select, insert, update, and delete data that is stored in SQL Server 2008 databases using any .NET programming language such as C# and VB.NET.

Additional Resources

  • Enables developers to manipulate SQL Server databases using a variety of languages, not just Transact-SQL.
  • Application developers do not need to understand Transact-SQL to access SQL Server databases
  • Queries can be run against strongly typed objects, which minimizes runtime errors

Change Data Capture

Use Change Data Capture (CDC) to track changes to the data in your tables. CDC uses a SQL Server Agent job to capture insert, update and delete activity. This information is stored in a relational table, from where it can be accessed by data consumers such as SQL Server 2008 Integration Services. Use CDC in conjunction with Integration Services to incrementally populate data warehouses, enabling you to produce more frequent reports that contain up-to-date information. It also allows sync-enabled mobile and desktop applications to perform efficient data synchronization between client and server, without requiring changes to the database.

Additional Resources

  • Simplifies the process of capturing changed data and making it available to consumers
  • Provides relatively low impact on performance because triggers and replication is not required
  • Improves accuracy of reports by making recent changes to data more readily available
  • A SQL Server Agent job automatically cleans up CDC system tables so that they do not grow too large.

Table-Valued Parameters

Table-Valued Parameters (TVPs) allows stored procedures to accept and return lists of parameters. Developers can write applications that pass sets of data into stored procedures rather than just one value at a time. Table-valued parameters make the development of stored procedures that manipulate data more straightforward and can improve performance by reducing the number of times a procedure needs to call a database.

Additional Resources

  • Eliminates the need to use less efficient methods of processing data sets, such as passing XML data into stored procedures
  • Reduces complexity and simplifies the process of working with data sets for developers

ADO.NET Entity Framework and the Entity Data Model

SQL Server 2008 databases store data in a relational format, but developers typically access the data they contain by using an application that was developed in an object-oriented programming language. Creating such applications can be made more complex if you need to build knowledge of the underlying database schema into the applications.

The ADO.NET Entity Framework allows a database to be abstracted and modeled into business objects, or entities, which can be more efficiently used by object-oriented programming languages such as C# and VB.NET. Applications can then use LINQ to query these entities without having to understand the underlying physical database schema.

Additional Resources

  • Simplifies the development of applications that require access to relational data sources
  • Applications do not need to be modified in response to changes in the database schema, so ongoing development effort is reduced
  • Developers can visually design and edit Entities using the Visual Entity Designer from within Visual Studio 2008 SP1

Synchronization Services for ADO.NET

Synchronization Services for ADO.NET enables developers to build occasionally connected systems (OCSs) such as personal digital assistants (PDAs), laptop computers, and mobile phones to synchronize with server based databases. Users can work with a copy of the data that is cached on their local device and then synchronize changes with a server when a connection becomes available.

Additional Resources

  • More efficient than replication for some scenarios
  • Only incremental changes are copied, which reduces the amount of data sent and the time it takes to synchronize
  • Develop applications that use Synchronization Services by using Visual Studio 2008
  • Applications can synchronize directly with servers or through multiple intermediate tiers

CLR Improvements

Common Language Runtime functionality in SQL Server 2008 has been improved in several areas. User-defined aggregates (UDAs) now support up to 2GB of data and can accept multiple inputs. User-defined types (UDTs) are, like UDAs, and also support up to 2GB of data. CLR table-valued functions now feature an optional ORDER clause in the CREATE FUNCTION statement, which helps the optimizer to run the query more efficiently.

Additional Resources

  • Use UDAs and UDTs with larger data since 8KB size limit restriction has been removed
  • Table-valued functions with a predefined sort order generally execute faster

Conflict Detection in Peer-to-Peer Replication

In a peer-to-peer replication scenario, all nodes in the replication topology contain the same data and any node can replicate to any other node, leading to the possibility of data conflicts. Use conflict detection to make sure that no such errors go undetected and that data remains consistent.

Additional Resources

  • Ensures that data remains consistent across nodes that have been configured to use peer-to-peer replication
  • Has a minimal performance impact if applications are configured to write changes for specific rows to specific nodes, which helps to avoid replication conflicts occurring in the first place

Service Broker Priorities and Diagnostics

Service Broker provides an asynchronous communication mechanism that allows servers to communicate by exchanging queued messages. Service Broker can be configured to prioritize certain messages so that they are sent and processed before other lower priority messages. Use the Service Broker Diagnostic Utility to investigate communication problems between participating Service Broker services.

Additional Resources

  • Priorities Service Broker messages easily
  • Troubleshooting is made easier with the dedicated diagnostic utility.

ADO.NET Data Services

Microsoft ADO.NET Data Services provides a data access infrastructure for Internet applications by enabling Web applications to expose SQL Server data as a service that can be consumed by client applications in corporate networks and across the Internet.

Additional Resources

  • Access SQL Server data from a service
  • Simpler data access code that uses common Web protocols and techniques such as HTTP GET and PUT requests with a simple, stateless REST architecture.
  • Interoperability across platforms, programming languages, and development frameworks through standard XML, JSON, and ATOM/APP data formats.

Beyond Relational

Spatial data with GEOGRAPHY and GEOMETRY data types

New GEOGRAPHY and GEOMETRY data types allow spatial data to be stored directly in a SQL Server 2008 database. Use these spatial data types to work with location-based data that describes physical locations, such as longitude and latitude.

GEOGRAPHY enables you to represent three-dimensional geodetic data such as GPS applications use. GEOMETRY enables you to represent two-dimensional planar data such as points on maps. Spatial data types help you to answer questions like ‘How many of our stores are located within 20 miles of Seattle?’

Additional Resources

  • Store spatial data natively in SQL Server2008 databases
  • Describe physical locations natively using industry standards such as WGS84
  • Integrate SQL Server 2008 with Microsoft Virtual Earth to display location data visually

Virtual Earth Integration

Use the new spatial data types in SQL Server 2008 with Microsoft Virtual Earth to deliver rich graphical representations of the physical locations stored in a database. Use Virtual Earth support to create applications that display data about locations in desktop maps or web pages. For example, SQL Server 2008 makes it easy to show the locations of all company sites that are less than 50 kilometers from Denver.

Additional Resources

  • Spatial data types enable geographical data to be stored natively in SQL Server2008 databases
  • Built-in library of industry-standard spatial functions streamlines development
  • Provide meaningful representations of geographical data

Sparse Columns

Sparse columns provide an efficient way to store NULL data in tables by not requiring NULL values to take up space. Applications that reference sparse columns can access them in the same way as they access regular columns. Multiple sparse columns in a table are supported by using a column set.

Additional Resources

  • Save significant storage space for tables contain lots of NULL data
  • Define how NULL values are handled on a column by column basis provides granular control
  • Filtered indexes built on sparse columns provide improved performance.

Filtered Indexes

A filtered index is essentially an index that supports a WHERE condition and includes only matching rows. It is a non-clustered index that is created on a subset of rows. Because filtered indexes generally do not contain all rows in the table, they are smaller and deliver faster performance for queries that reference the rows it contains.

Use filtered indexes to optimize performance for specific queries by ensuring that they contain only the rows referenced by the queries.

Additional Resources

  • Improved query performance
  • Reduced storage and maintenance costs

Integrated Full-Text Search

Full text indexes enable queries to be performed for words and phrases on text stored in your databases. The Full-Text Engine in SQL Server 2008 is fully integrated into the database and full-text indexes are stored within database files rather than externally in the file system. This allows Full text indexes to be fully backed up and restored along with the rest of the database. Full-text indexes are also integrated with the Query Processor, so they are used more efficiently.

Additional Resources

  • Search for words and phrases in text data more efficiently.
  • Manage full-text indexes as part of the database, making administration simpler.

FILESTREAM Data

FILESTREAM enables binary large object (BLOB) data to be stored in the Microsoft Windows NTFS file system instead of in a database file. Data that is stored using FILESTREAM behaves like any other data type and can be manipulated using T-SQL select, insert, update and delete statements.

Unlike traditional BLOB storage, FILESTREAM data is logically shackled to the database while being stored efficiently outside the database in the NTFS file system. FILESTREAM data participates in all SQL Server transactions and backup operations, along with the rest of the database.

Additional Resources

  • Provides improved performance for read access since FILESTREAM data is managed directly by Windows NTFS
  • No imposed maximum size for FILESTEAM data means you are only limited by the size of the volume that stores the data.
  • BLOB data participates seamlessly in transactions, rollbacks and backup operations
  • Storage and management of unstructured data is simplified

Large User-Defined Types (UDTs)

Create user-defined types (UDTs) that go beyond the traditional data types supported to describe custom data types. UDTs in SQL Server 2008 are more extensible than previous versions since the 8KB size limit has been increased to 2GB. Note that the powerful new spatial data types GEOMETRY and GEOGRAPHY in SQL Server 2008 were developed using this new UDT architecture.

Additional Resources

  • Create extensive, custom data types describe large, complex data
  • Use a variety of .NET languages to create UDTs, including VB.NET and C#.NET
  • Use familiar Visual Studio 2008 environment to create UDTs

Large User-Defined Aggregates (UDAs)

SQL Server 2008 features a set of built-in aggregate functions that can be used to perform common aggregations such as summing or averaging data. Create custom, user-defined aggregates (UDAs) to manage custom aggregations. UDAs in SQL Server 2008 are more extensible than previous versions since the 8KB size limit has been increased to 2GB.

Additional Resources

  • Process larger amounts of data
  • Use a variety of .NET languages to create UDAs, including C# and VB.NET
  • Use familiar Visual Studio 2008 environment to create UDAs

DATE / TIME Data Types

SQL Server 2008 introduces several new date and time based data types. DATETIME2 references the Coordinated Universal Time (UTC) instead of the system time for greater accuracy and can store date and time data to a precision of 100 nanoseconds. The new DATE and TIME data types enable you to store date and time data separately. The new DATETIMEOFFSET data type introduces time zone support by storing date, time and offset such as ‘plus 5 hours’.

Additional Resources

  • Greater flexibility for storing date and time data.
  • Greater accuracy and precision for date and time data.
  • DATETIME and SMALLDATETIME data types still supported.

Improved XML Support

SQL Server 2008 features several XML enhancements including Lax validation, the DATETIME data type, and union functionality for list types all provide greater flexibility for defining XML schemas. XQuery includes support for the let clause, and the modify method of the xml data type now accepts xml variables as input for an insert expression.

Additional Resources

  • Greater integration for XML improves interoperability
  • Improved options for defining xml schemas allow for more comprehensive xml data validation and promote data integrity
  • Enhancements in the manipulation of xml data allow you to manage it more like you manage your relational data

ORDPATH

Hierarchical data is organized differently to relational data, typically in the form of a tree. An example of hierarchical data is a typical organization chart that outlines the relationships between managers and the employees they manage. A column in a table that uses the HierarchyID data type contains data that describes the hierarchical relationships between rows explicitly in the form of a path. ORDPATH makes it efficient to program hierarchical data by using the HierarchyID data type.

Additional Resources

  • Efficiently represent hierarchical relationships within relational table
  • Make hierarchical relationships explicit
  • HierarchyID has a set of methods that enable you to manage hierarchical data

Pervasive Insight

Fixed Query Plan Guides (Plan Freezing)

Freezing Query Plans enables you to influence how the SQL Server query optimizer executes queries. SQL Server 2008 allows existing query execution plans to be imported. Plan Guide to force the query optimizer to always use a particular execution plan for a specific query. Using fixed query plans ensures that queries will be executed in the same way every time they run.

Additional Resources

  • Freeze query plans on previous versions of SQL Server during upgrade to SQL Server 2008 to help ensure a predictable state of operation
  • Ensure that query plans are retained when servers are upgraded
  • You can revert to a known good execution plan if query performance degenerates

Star Join Query Optimization

Data warehouses are often implemented as star schemas. A star schema has a fact table at its centre, which typically contains a very large number of rows. Star join query optimization can provide improvements in performance for queries that select a subset of those rows. When SQL Server processes queries using star join query optimization, bitmap filters eliminate rows that do not qualify for inclusion in the result set very early on, so that the rest of the query is processed more efficiently.

Additional Resources

  • Significantly quicker query processing for data warehouses.
  • Star join query optimization happens automatically, applications do not need to be configured to use it.

Enterprise Reporting Engine

The reporting engine in SQL Server 2008 Reporting Services enables the pulling together of data from multiple heterogeneous sources from across an Enterprise. Large and complex reports can be produced in various formats, including list, chart, table, matrix, and tablix (a table/matrix hybrid).

Access and manage reports through a Microsoft SharePoint Services site, simplifying administration, security, and collaboration, and making reports more easily available.

Additional Resources

  • Reports can incorporate data from Oracle, SQL Server, DB2 and many other sources.
  • Multiple report formats are available for making maximum visual impact of data
  • Simplified administration and access to reports

Report Builder Enhancements

Report Builder is an end-user tool for the creation and editing of reports. Report Builder in SQL Server 2008 has an interface that is consistent with Microsoft Office 2007 products, and because it masks the underlying complexity of report building, nontechnical users can create sophisticated reports with relative ease.

Additional Resources

  • Familiar interface eases the learning curve for end users
  • Report layout templates and drag and drop functionality simplify the report creation process
  • Users can preview, print and publish their reports from within Report Builder

Improving Rendering for Microsoft Office® Word and Excel

Reports generated by SQL Server 2008 Reporting Services can be viewed and edited by using Microsoft Office Excel and Microsoft Office Word. The Excel rendering extension produces .xls files that are compatible with versions of Microsoft Office Excel from version 97 upwards.

It offers improved options over previous versions, such as the rendering of subreports. The Word rendering extension, which new in SQL Server 2008 Reporting Services, produces .doc files that are compatible with versions of Microsoft Office Word from version 2000 upwards.

Additional Resources

  • Users can view and edit reports without having to install special software
  • Reports are generated in a format that is backwards compatible
  • More options for Microsoft Office Excel

Partitioned Table Parallelism

Parallelism refers to using multiple processors in parallel to process a query, which improves query response time. On a multiprocessor system, SQL Server 2008 uses parallel processing when you run queries that reference partitioned tables.

When SQL Server 2008 processes such a query, rather than allocating just one processor for each partition referenced by the query, it can allocate all available processors, regardless of the number of partitions referenced.

Additional Resources

  • Queries against partitioned tables run faster
  • More efficient use of processor resources than in previous versions of SQL Server
  • Operations involving partitioned indexes are optimized in the same way

IIS Agnostic Report Deployments

Reporting Services in SQL Server 2008 does not depend on IIS to provide core functionality as it did in SQL Server 2005. Reporting Services can directly generate and deliver reports by accessing the HTTP.SYS driver directly. This has the effect of simplifying the deployment and management of Reporting Services in addition to offering better performance when generating larger reports.

Additional Resources

  • No need to install IIS to use Reporting Services
  • Installation and management of Reporting Services is streamlined
  • More efficient use of resources cab result in quicker rendering of reports

Persistent Lookups

SQL Server Integration Services packages use lookups to reference external data rows in the data flow. Lookup data flow transformations load the external data into cache to improve the performance of this operation. SQL Server 2008 Integration Services uses persistent lookups so that data loaded into the lookup cache is available to other packages, or to multiple pipelines within the same package, without the need to reload the cache.

Additional Resources

  • Improved performance for lookup transformations
  • More efficient use of memory resources

Analysis Services Query and Writeback Performance

Cell writeback in SQL Server Analysis Services enables users to perform speculative analysis on data. Users can modify specific data values and then issuing queries to see the effect of the changes. This can be useful for forecasting, for example.

In SQL Server 2008 Analysis Services, the values that a user changes are stored in a MOLAP format writeback partition, which results in better query and writeback performance than the ROLAP format that was used in SQL Server 2005 Analysis Services.

Additional Resources

  • More efficient storage of writeback data provides improved performance.
  • MOLAP storage is used automatically, without the need for extra configuration.

Best Practice Design Alerts

Good design is fundamental to creating optimal Analysis Services solutions. SQL Server 2008 Analysis Services uses Analysis Management Objects (AMO) warnings to alert you when the choices you make in your design deviate from best practice.

Design problems are underlined in blue, similar to the way spelling mistakes are underlined in red in Microsoft Office Word. You can see the full text of warning by placing your arrow over the underlined object. You can disable AMO warnings if you choose.

Additional Resources

  • Provides real time guidelines to enable correct design from the start.
  • Helps to comply with best practice.
  • Helps inexperienced designers learn best practice.

Analysis Services Dimension Design

Various new features in SQL Server 2008 Analysis Services contribute to improving and simplifying the dimension design process. Analysis Management Objects (AMO) warnings help ensure designs comply with best practice, the Attribute Relationship Designer is a visual tool for defining attribute relationships, and key column management is easier with the key columns dialog box.

Additional Resources

  • Design process is more straightforward
  • Attribute relationships are represented visually, making them easier to understand.
  • Helps comply with best practice

Analysis Services Time Series

Microsoft Time Series enables trends over time to be forecasted. For example, you can use it to predict product sales over the coming 12 month period. SQL Server 2008 Analysis Services includes the same algorithm for short term analysis that SQL Server 2005 Analysis Services used, and additionally introduces an algorithm for long term trend analysis. Both algorithms are used by default and you can also choose to use just one or the other.

Additional Resources

  • Improved future trend analysis capabilities.
  • Take account of seasonal variations when making predictions.
  • Weight one algorithm over another to achieve either short or long term analysis.

MS SQL Server Security Best Practices

SQL Server 2005 Security Best Practices – Operational and Administrative Tasks

SQL Server Technical Article

Writers: Bob Beauchemin, SQLskills

Technical Reviewers: Laurentiu Cristofor, Al Comeau, Sameer Tejani, Devendra Tiwari, Rob Walters, Niraj Nagrani

Published: March 2007

Applies To: SQL Server 2005 SP2

Summary: Security is a crucial part of any mission-critical application. This paper describes best practices for setting up and maintaining security in SQL Server 2005.


Copyright

The information contained in this document represents the current view of Microsoft Corporation on the issues discussed as of the date of publication.  Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information presented after the date of publication.

This White Paper is for informational purposes only.  MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS DOCUMENT.

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 Microsoft Corporation.

Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject matter in this document.  Except as expressly provided in any written license agreement from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other intellectual property.

Unless otherwise noted, the example companies, organizations, products, domain names, e-mail addresses, logos, people, places and events depicted herein are fictitious, and no association with any real company, organization, product, domain name, email address, logo, person, place or event is intended or should be inferred.

Ó 2007 Microsoft Corporation.  All rights reserved.

Microsoft, Active Directory, Windows, Windows Server, and Windows Vista are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries.

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

Table of Contents

Introduction. 4

Surface Area Reduction. 4

Service Account Selection and Management 6

Authentication Mode. 9

Network Connectivity. 10

Lockdown of System Stored Procedures. 13

Password Policy. 14

Administrator Privileges. 16

Database Ownership and Trust 17

Schemas. 18

Authorization. 19

Catalog Security. 21

Remote Data Source Execution. 22

Execution Context 23

Encryption. 24

Auditing. 26

Microsoft Baseline Security Analyzer and SQL Server Best Practices Analyzer 29

Patching. 29

Conclusion. 29

Introduction

This white paper covers some of the operational and administrative tasks associated with Microsoft® SQL Server™ 2005 security and enumerates best practices and operational and administrative tasks that will result in a more secure SQL Server system. Each topic describes a feature and best practices. For additional information on the specifics of utilities, features, and DDL statements referenced in this white paper, see SQL Server 2005 Books Online. Features and options that are new or defaults that are changed for SQL Server 2005 are identified. Coding examples for operational tasks use Transact-SQL, so understanding Transact-SQL is required for you to get the most out of this paper.

Surface Area Reduction

SQL Server 2005 installation minimizes the “attack surface” because by default, optional features are not installed. During installation the administrator can choose to install:

  • Database Engine
  • Analysis Services Engine
  • Reporting Services
  • Integration Services
  • Notification Services
  • Documentation and Samples

It is a good practice to review which product features you actually need and install only those features. Later, install additional features only as needed. SQL Server 2005 includes sample databases for OLTP, data warehousing, and Analysis Services. Install sample databases on test servers only; they are not installed by default when you install the corresponding engine feature. SQL Server 2005 includes sample code covering every feature of the product. These samples are not installed by default and should be installed only on a development server, not on a production server. Each item of sample code has undergone a review to ensure that the code follows best practices for security. Each sample uses Microsoft Windows® security principals and illustrates the principal of least privilege.

SQL Server has always been a feature-rich database and the number of new features in SQL Server 2005 can be overwhelming. One way to make a system more secure is to limit the number of optional features that are installed and enabled by default. It is easier to enable features when they are needed than it is to enable everything by default and then turn off features that you do not need. This is the installation policy of SQL Server 2005, known as “off by default, enable when needed.” One way to ensure that security policies are followed is to make secure settings the default and make them easy to use.

SQL Server 2005 provides a “one-stop” utility that can be used to enable optional features on a per-service and per-instance basis as needed. Although there are other utilities (such as Services in Control Panel), server configuration commands (such as sp_configure), and APIs such as WMI (Windows Management Instrumentation) that you can use, the SQL Server Surface Area Configuration tool combines this functionality into a single utility program. This program can be used either from the command line or via a graphic user interface.

SQL Server Service Area Configuration divides configuration into two subsets: services and connections, and features. Use the Surface Area Configuration for Services and Connections tool to view the installed components of SQL Server and the client network interfaces for each engine component. The startup type for each service (Automatic, Manual, or Disabled) and the client network interfaces that are available can be configured on a per-instance basis. Use the Surface Area Configuration for Features tool to view and configure instance-level features.

The features enabled for configuration are:

  • CLR Integration
  • Remote use of a dedicated administrator connection
  • OLE Automation system procedures
  • System procedures for Database Mail and SQL Mail
  • Ad hoc remote queries (the OPENROWSET and OPENDATASOURCE functions)
  • SQL Server Web Assistant
  • xp_cmdshell availability

The features enabled for viewing are:

  • HTTP endpoints
  • Service Broker endpoint

The SQL Server Surface Area Configuration command-line interface, sac.exe, permits you to import and export settings. This enables you to standardize the configuration of a group of SQL Server 2005 instances. You can import and export settings on a per-instance basis and also on a per-service basis by using command-line parameters. For a list of command-line parameters, use the -? command-line option. You must have sysadmin privilege to use this utility. The following code is an example of exporting all settings from the default instance of SQL Server on server1 and importing them into server2:

sac out server1.out –S server1 –U admin –I MSSQLSERVER

sac in server1.out –S server2

When you upgrade an instance of SQL Server to SQL Server 2005 by performing an in-place upgrade, the configuration options of the instance are unchanged. Use SQL Server Surface Area Configuration to review feature usage and turn off features that are not needed. You can turn off the features in SQL Server Surface Area Configuration or by using the system stored procedure, sp_configure. Here is an example of using sp_configure to disallow the execution of xp_cmdshell on a SQL Server instance:

– Allow advanced options to be changed.

EXEC sp_configure ‘show advanced options’, 1

GO

– Update the currently configured value for advanced options.

RECONFIGURE

GO

– Disable the feature.

EXEC sp_configure ‘xp_cmdshell’, 0

GO

– Update the currently configured value for this feature.

RECONFIGURE

GO

In SQL Server 2005, SQL Server Browser functionality has been factored into its own service and is no longer part of the core database engine. Additional functions are also factored into separate services. Services that are not a part of the core database engine and can be enabled or disabled separately include:

  • SQL Server Active Directory Helper
  • SQL Server Agent
  • SQL Server FullText Search
  • SQL Server Browser
  • SQL Server VSS Writer

The SQL Server Browser service needs to be running only to connect to named SQL Server instances that use TCP/IP dynamic port assignments. It is not necessary to connect to default instances of SQL Server 2005 and named instances that use static TCP/IP ports. For a more secure configuration, always use static TCP/IP port assignments and disable the SQL Server Browser service. The VSS Writer allows backup and restore using the Volume Shadow Copy framework. This service is disabled by default. If you do not use Volume Shadow Copy, disable this service. If you are running SQL Server outside of an Active Directory® directory service, disable the Active Directory Helper.

Best practices for surface area reduction

  • Install only those components that you will immediately use. Additional components can always be installed as needed.
  • Enable only the optional features that you will immediately use.
  • Review optional feature usage before doing an in-place upgrade and disable unneeded features either before or after the upgrade.
  • Develop a policy with respect to permitted network connectivity choices. Use SQL Server Surface Area Configuration to standardize this policy.
  • Develop a policy for the usage of optional features. Use SQL Server Surface Area Configuration to standardize optional feature enabling. Document any exceptions to the policy on a per-instance basis.
  • Turn off unneeded services by setting the service to either Manual startup or Disabled.

Service Account Selection and Management

SQL Server 2005 executes as a set of Windows services. Each service can be configured to use its own service account. This facility is exposed at installation. SQL Server provides a special tool, SQL Server Configuration Manager, to manage these accounts. In addition, these accounts can be set programmatically through the SQL Server WMI Provider for Configuration. When you select a Windows account to be a SQL Server service account, you have a choice of:

  • Domain user that is not a Windows administrator
  • Local user that is not a Windows administrator
  • Network Service account
  • Local System account
  • Local user that is a Windows administrator
  • Domain user that is a Windows administrator

When choosing service accounts, consider the principle of least privilege. The service account should have exactly the privileges that it needs to do its job and no more privileges. You also need to consider account isolation; the service accounts should not only be different from one another, they should not be used by any other service on the same server. Only the first two account types in the list above have both of these properties. Making the SQL Server service account an administrator, at either a server level or a domain level, bestows too many unneeded privileges and should never be done. The Local System account is not only an account with too many privileges, but it is a shared account and might be used by other services on the same server. Any other service that uses this account has the same set up privileges as the SQL Server service that uses the account. Although Network Service has network access and is not a Windows superuser account, it is a shareable account. This account is useable as a SQL Server service account only if you can ensure that no other services that use this account are installed on the server.

Using a local user or domain user that is not a Windows administrator is the best choice. If the server that is running SQL Server is part of a domain and must access domain resources such as file shares or uses linked server connections to other computers running SQL Server, a domain account is the best choice. If the server is not part of a domain (for example, a server running in the perimeter network (also known as the DMZ) in a Web application) or does not need to access domain resources, a local user that is not a Windows administrator is preferred.

Creating the user account that will be used as a SQL Server service account is easier in SQL Server 2005 than in previous versions. When SQL Server 2005 is installed, a Windows group is created for each SQL Server service, and the service account is placed in the appropriate group. To create a user that will serve as a SQL Server service account, simply create an “ordinary” account that is either a member of the Users group (non-domain user) or Domain Users group (domain user). During installation, the user is automatically placed in the SQL Server service group and the group is granted exactly the privileges that are needed.

If the service account needs additional privileges, the privilege should be granted to the appropriate Windows group, rather than granted directly to the service user account. This is consistent with the way access control lists are best managed in Windows in general. For example, the ability to use the SQL Server Instant File Initialization feature requires that the Perform Volume Maintenance Tasks user rights be set in the Group Policy Administration tool. This privilege should be granted to SQLServer2005MSSQLUser$MachineName$MSSQLSERVER group for the default instance of SQL Server on server “MachineName.”

SQL Server service accounts should be changed only by using SQL Server Configuration Manager, or by using the equivalent functionality in the WMI APIs. Using Configuration Manager ensures that the new service account is placed in the appropriate Windows group, and is thus granted exactly the correct privileges to run the service. In addition, using SQL Server Configuration Manager also re-encrypts the service master key that is using the new account. For more information on the service master key, see Encryption later in this paper. Because SQL Server service accounts also abide by Windows password expiration policies, it is necessary to change the service account passwords at regular intervals. In SQL Server 2005, it is easier to abide by password expiration policies because changing the password of the service account does not require restarting SQL Server.

SQL Server 2005 requires that the service account have less privilege than in previous versions. Specifically, the privilege Act As Part of the Operating System (SE_TCB_NAME) is not required for the service account unless SQL Server 2005 is running on the Microsoft Windows Server™ 2000 SP4 operating system. After doing an upgrade in place, use the Group Policy Administration tool to remove this privilege.

The SQL Server Agent service account requires sysadmin privilege in the SQL Server instance that it is associated with. In SQL Server 2005, SQL Server Agent job steps can be configured to use proxies that encapsulate alternate credentials. A CREDENTIAL is simply a database object that is a symbolic name for a Windows user and password. A single CREDENTIAL can be used with multiple SQL Server Agent proxies. To accommodate the principal of least privilege, do not give excessive privileges to the SQL Server Agent service account. Instead, use a proxy that corresponds to a CREDENTIAL that has just enough privilege to perform the required task. A CREDENTIAL can also be used to reduce the privilege for a specific task if the SQL Server Agent service account has been configured with more privileges than needed for the task. Proxies can be used for:

  • ActiveX scripting
  • Operating system (CmdExec)
  • Replication agents
  • Analysis Services commands and queries
  • SSIS package execution (including maintenance plans)

Best practices for SQL Server service accounts

  • Use a specific user account or domain account rather than a shared account for SQL Server services.
  • Use a separate account for each service.
  • Do not give any special privileges to the SQL Server service account; they will be assigned by group membership.
  • Manage privileges through the SQL Server supplied group account rather than through individual service user accounts.
  • Always use SQL Server Configuration Manager to change service accounts.
  • Change the service account password at regular intervals.
  • Use CREDENTIALs to execute job steps that require specific privileges rather than adjusting the privilege to the SQL Server Agent service account.
  • If an agent user needs to execute a job that requires different Windows credentials, assign them a proxy account that has just enough permissions to get the task done.

Authentication Mode

SQL Server has two authentication modes: Windows Authentication and Mixed Mode Authentication. In Windows Authentication mode, specific Windows user and group accounts are trusted to log in to SQL Server. Windows credentials are used in the process; that is, either NTLM or Kerberos credentials. Windows accounts use a series of encrypted messages to authenticate to SQL Server; no passwords are passed across the network during the authentication process. In Mixed Mode Authentication, both Windows accounts and SQL Server-specific accounts (known as SQL logins) are permitted. When SQL logins are used, SQL login passwords are passed across the network for authentication. This makes SQL logins less secure than Windows logins.

It is a best practice to use only Windows logins whenever possible. Using Windows logins with SQL Server achieves single sign-on and simplifies login administration. Password management uses the ordinary Windows password policies and password change APIs. Users, groups, and passwords are managed by system administrators; SQL Server database administrators are only concerned with which users and groups are allowed access to SQL Server and with authorization management.

SQL logins should be confined to legacy applications, mostly in cases where the application is purchased from a third-party vendor and the authentication cannot be changed. Another use for SQL logins is with cross-platform client-server applications in which the non-Windows clients do not possess Windows logins. Although using SQL logins is discouraged, there are security improvements for SQL logins in SQL Server 2005. These improvements include the ability to have SQL logins use the password policy of the underlying operating system and better encryption when SQL passwords are passed over the network. We’ll discuss each of these later in the paper.

SQL Server 2005 uses standard DDL statements to create both Windows logins and SQL logins. Using the CREATE LOGIN statement is preferred; the sp_addlogin and sp_grantlogin system stored procedures are supported for backward compatibility only. SQL Server 2005 also provides the ability to disable a login or change a login name by using the ALTER LOGIN DDL statement. For example, if you install SQL Server 2005 in Windows Authentication mode rather than Mixed Mode, the sa login is disabled. Use ALTER LOGIN rather than the procedures sp_denylogin or sp_revokelogin, which are supported for backward compatibility only.

If you install SQL Server in Windows Authentication mode, the sa login account is disabled and a random password is generated for it. If you later need to change to Mixed Mode Authentication and re-enable the sa login account, you will not know the password. Change the sa password to a known value after installation if you think you might ever need to use it.

Best practices for authentication mode

  • Always use Windows Authentication mode if possible.
  • Use Mixed Mode Authentication only for legacy applications and non-Windows users.
  • Use the standard login DDL statements instead of the compatibility system procedures.
  • Change the sa account password to a known value if you might ever need to use it. Always use a strong password for the sa account and change the sa account password periodically.
  • Do not manage SQL Server by using the sa login account; assign sysadmin privilege to a knows user or group.
  • Rename the sa account to a different account name to prevent attacks on the sa account by name.

Network Connectivity

A standard network protocol is required to connect to the SQL Server database. There are no internal connections that bypass the network. SQL Server 2005 introduces an abstraction for managing any connectivity channel—entry points into a SQL Server instance are all represented as endpoints. Endpoints exist for the following network client connectivity protocols:

  • Shared Memory
  • Named Pipes
  • TCP/IP
  • VIA
  • Dedicated administrator connection

In addition, endpoints may be defined to permit access to the SQL Server instance for:

  • Service Broker
  • HTTP Web Services
  • Database mirroring

Following is an example of creating an endpoint for Service Broker.

CREATE ENDPOINT BrokerEndpoint_SQLDEV01

AS TCP

( LISTENER_PORT = 4022  )

FOR SERVICE_BROKER

( AUTHENTICATION = WINDOWS )

SQL Server 2005 discontinues support for some network protocols that were available with earlier versions of SQL Server, including IPX/SPX, Appletalk, and Banyon Vines.

In keeping with the general policy of “off by default, enable only when needed,” no Service Broker, HTTP, or database mirroring endpoints are created when SQL Server 2005 is installed, and the VIA endpoint is disabled by default. In addition, in SQL Server 2005 Express Edition, SQL Server 2005 Developer Edition, and SQL Server 2005 Evaluation Edition, the Named Pipes and TCP/IP protocols are disabled by default. Only Shared Memory is available by default in those editions. The dedicated administrator connection (DAC), new with SQL Server 2005, is available only locally by default, although it can be made available remotely. Note that the DAC is not available in SQL Server Express Edition by default and requires that the server be run with a special trace flag to enable it. Access to database endpoints requires the login principal to have CONNECT permission. By default, no login account has CONNECT permission to Service Broker or HTTP Web Services endpoints. This restricts access paths and blocks some known attack vectors. It is a best practice to enable only those protocols that are needed. For example, if TCP/IP is sufficient, there is no need to enable the Named Pipes protocol.

Although endpoint administration can be accomplished via DDL, the administration process is made easier and policy can be made more uniform by using the SQL Server Surface Area Configuration tool and SQL Server Configuration Manager. SQL Server Surface Area Configuration provides a simplified user interface for enabling or disabling client protocols for a SQL Server instance, as shown in Figure 1 and Figure 2. Configuration is described in Knowledge Base article KB914277, How to configure SQL Server 2005 to allow remote connections, as well as in SQL Server 2005 Books Online. A screenshot showing the remote connections configuration dialog box is shown in Figure 1.

Figure 1   Configuring remote connections

In the Surface Area Configuration for Services and Connections dialog box, you can see if any HTTP or Service Broker endpoints are defined for the instance. New endpoints must be defined by using DDL statements; SQL Server Surface Area Configuration cannot be used to define these. You can use the Surface Area Configuration for Features tool to enable remote access to the dedicated administrator connection.

SQL Server Configuration Manager provides more granular configuration of server protocols. With Configuration Manager, you can:

  • Choose a certificate for SSL encryption.
  • Allow only encryption connections from clients.
  • Hide an instance of SQL Server from the server enumeration APIs.
  • Enable and disable TCP/IP, Shared Memory, Named Pipes, and VIA protocols.
  • Configure the name of the pipe each instance of SQL Server will use.
  • Configure a TCP/IP port number that each instance listens on for TCP/IP connections.
  • Choose whether to use TCP/IP dynamic port assignment for named instances.

The dialog for configuring TCP/IP address properties such as port numbers and dynamic port assignment is shown in Figure 2.

Figure 2   TCP/IP Addresses configuration page in SQL Server Configuration Manager

SQL Server 2005 can use an encrypted channel for two reasons: to encrypt credentials for SQL logins, and to provide end-to-end encryption of entire sessions. Using encrypted sessions requires using a client API that supports these. The OLE DB, ODBC, and ADO.NET clients all support encrypted sessions; currently the Microsoft JDBC client does not. The other reason for using SSL is to encrypt credentials during the login process for SQL logins when a password is passed across the network. If an SSL certificate is installed in a SQL Server instance, that certificate is used for credential encryption. If an SSL certificate is not installed, SQL Server 2005 can generate a self-signed certificate and use this certificate instead. Using the self-signed certificate prevents passive man-in-the-middle attacks, in which the man-in-the-middle intercepts network traffic, but does not provide mutual authentication. Using an SSL certificate with a trusted root certificate authority prevents active man-in-the-middle attacks and provides mutual authentication.

In SQL Server 2005, you can GRANT, REVOKE, or DENY permission to CONNECT to a specific endpoint on a per-login basis. By default, all logins are GRANTed permission on the Shared Memory, Named Pipes, TCP/IP, and VIA endpoints. You must specifically GRANT users CONNECT permission to other endpoints; no users are GRANTed this privilege by default. An example of granting this permission is:

GRANT CONNECT ON MyHTTPEndpoint TO MyDomain\Accounting

Best practices for network connectivity

  • Limit the network protocols supported.
  • Do not enable network protocols unless they are needed.
  • Do not expose a server that is running  SQL Server to the public Internet.
  • Configure named instances of SQL Server to use specific port assignments for TCP/IP rather than dynamic ports.
  • If you must support SQL logins, install an SSL certificate from a trusted certificate authority rather than using SQL Server 2005 self-signed certificates.
  • Use “allow only encrypted connections” only if needed for end-to-end encryption of sensitive sessions.
  • Grant CONNECT permission only on endpoints to logins that need to use them. Explicitly deny CONNECT permission to endpoints that are not needed by users or groups.

Lockdown of System Stored Procedures

SQL Server uses system stored procedures to accomplish some administrative tasks. These procedures almost always begin with the prefix xp_ or sp_. Even with the introduction of standard DDL for some tasks (for example, creating logins and users), system procedures remain the only way to accomplish tasks such as sending mail or invoking COM components. System extended stored procedures in particular are used to access resources outside the SQL Server instance. Most system stored procedures contain the relevant security checks as part of the procedure and also perform impersonation so that they run as the Windows login that invoked the procedure. An example of this is sp_reserve_http_namespace, which impersonates the current login and then attempts to reserve part of the HTTP namespace (HTTP.SYS) by using a low-level operating system function.

Because some system procedures interact with the operating system or execute code outside of the normal SQL Server permissions, they can constitute a security risk. System stored procedures such as xp_cmdshell or sp_send_dbmail are off by default and should remain disabled unless there is a reason to use them. In SQL Server 2005, you no longer need to use stored procedures that access the underlying operating system or network outside of the SQL Server permission space. SQLCLR procedures executing in EXTERNAL_ACCESS mode are subject to SQL Server permissions, and SQLCLR procedures executing in UNSAFE mode are subject to some, but not all, security checks. For example, to catalog a SQLCLR assembly categorized as EXTERNAL_ACCESS or UNSAFE, either the database must be marked as TRUSTWORTHY (see Database Ownership and Trust) or the assembly must be signed with a certificate or asymmetric key that is cataloged to the master database. SQLCLR procedures should replace user-written extended stored procedures in the future.

Some categories of system stored procedures can be managed by using SQL Server Surface Area Configuration. These include:

  • xp_cmdshell – executes a command in the underlying operating system
  • Database Mail procedures
  • SQL Mail procedures
  • COM component procedures (e.g. sp_OACreate)

Enable these procedures only if necessary.

Some system stored procedures, such as procedures that use SQLDMO and SQLSMO libraries, cannot be configured by using SQL Server Surface Area Configuration. They must be configured by using sp_configure or SSMS directly. SSMS or sp_configure can also be used to set most of the configuration feature settings that are set by using SQL Server Surface Area Configuration.

The system stored procedures should not be dropped from the database; dropping these can cause problems when applying service packs. Removing the system stored procedures results in an unsupported configuration. It is usually unnecessary to completely DENY all users access to the system stored procedures, as these stored procedures have the appropriate permission checks internal to the procedure as well as external.

Best practices for system stored procedures

  • Disable xp_cmdshell unless it is absolutely needed.
  • Disable COM components once all COM components have been converted to SQLCLR.
  • Disable both mail procedures (Database Mail and SQL Mail) unless you need to send mail from SQL Server. Prefer Database Mail as soon as you can convert to it.
  • Use SQL Server Surface Area Configuration to enforce a standard policy for extended procedure usage.
  • Document each exception to the standard policy.
  • Do not remove the system stored procedures by dropping them.
  • Do not DENY all users/administrators access to the extended procedures.

Password Policy

Windows logins abide by the login policies of the underlying operating system. These policies can be set using the Domain Security Policy or Local Security Policy administrator Control Panel applets. Login policies fall into two categories: Password policies and Account Lockout policies. Password policies include:

  • Enforce Password History
  • Minimum and Maximum Password Age
  • Minimum Password Length
  • Password Must Meet Complexity Requirements
  • Passwords are Stored Using Reversible Encryption (Note: this setting does not apply to SQL Server)

Account Lockout policies include:

  • Account Lockout Threshold (Number of invalid logins before lockout)
  • Account Lockout Duration (Amount of time locked out)
  • Reset Lockout Counter After n Minutes

In SQL Server 2005, SQL logins can also go by the login policies of the underlying operating system if the operating system supports it. The operating system must support the system call NetValidatePasswordPolicy. Currently, the only operating system that supports this is Windows Server 2003 and later versions. If you use SQL logins, run SQL Server 2005 on a Windows Server 2003 or later operating system. CREATE LOGIN parameters determine whether the login goes by the operating system policies. These parameters are:

  • CHECK_POLICY
  • CHECK_EXPIRATION
  • MUST_CHANGE

CHECK_POLICY specifies that the SQL login must abide by the Windows login policies and Account Lockout policies, with the exception of password expiration. This is because, if SQL logins must go by the Windows password expiration policy, underlying applications must be outfitted with a mechanism for password changing. Most applications currently do not provide a way to change SQL login passwords. In SQL Server 2005, both SSMS and SQLCMD provide a way to change SQL Server passwords for SQL logins. Consider outfitting your applications with a password-changing mechanism as soon as possible. Having built-in password changing also allows logins to be created with the MUST_CHANGE parameter; using this parameter requires the user to change the password at the time of the first login.  Administrators should be aware of the fact that password length and complexity policies, but not expiration policies, apply to passwords used with encryption keys as well as to passwords used with SQL logins. For a description of encryption keys, see Encryption.

When SQL logins are used on pre-Windows 2003 operating systems, there is a series of hard-coded password policies in lieu of the domain or operating system policies if CHECK_POLICY = ON. These policies are enumerated in SQL Server Books Online.

Best practices for password policy

  • Mandate a strong password policy, including an expiration and a complexity policy for your organization.
  • If you must use SQL logins, ensure that SQL Server 2005 runs on the Windows Server 2003 operating system and use password policies.
  • Outfit your applications with a mechanism to change SQL login passwords.
  • Set MUST_CHANGE for new logins.

Administrator Privileges

SQL Server 2005 makes all permissions grantable and also makes grantable permissions more granular than in previous versions. Privileges with elevated permissions now include:

  • Members of the sysadmin server role.
  • The sa built-in login, if it is enabled.
  • Any login with CONTROL SERVER permission.

CONTROL SERVER permission is new in SQL Server 2005. Change your auditing procedures to include any login with CONTROL SERVER permission.

SQL Server automatically grants the server’s Administrators group (BUILTIN\administrators) the sysadmin server role. When running SQL Server 2005 under Microsoft Windows Vista™, the operating system does not recognize membership in the BUILTIN\Administrators group unless the user has elevated themselves to a full administrator. In SP2, you can use SQL Server Surface Area Configuration to enable a principal to act as administrator by selecting Add New Administrator from the main window as shown in Figure 3.

Figure 3   Adding a new administrator in SP2 SQL Server Surface Area Configuration

Clicking on this link opens the SQL Server 2005 User Provisioning Tool for Vista as shown in Figure 4. This tool can also be automatically invoked as the last step of an SQL Server 2005 SP2 installation.

Figure 4   The SQL Server 2005 User Provisioning Tool for Vista

When running SQL Server Express SP2 under the Vista operating system, Set Up incorporates the specification of a specific principal to act as administrator. SQL Server Express SP2 Set Up also allows command-line options to turn user instances on or off (ENABLERANU) and to add the current Set Up user to the SQL Server Administrator role (ADDUSERASADMIN). For more detailed information, see Configuration Options (SQL Server Express) in SQL Server 2005 SP2 Books Online. For additional security-related considerations when running SQL Server 2005 with the Windows Vista operating system, see the SQL Server 2005 SP2 Readme file. In particular, see section 5.5.2 “Issues Caused by User Account Control in Windows Vista.”

For accountability in the database, avoid relying on the Administrators group and add only specific database administrators to the sysadmin role. Another option is to have a specific DatabaseAdministrators role at the operating system level. Minimizing the number of administrators who have sysadmin or CONTROL SERVER privilege also makes it easier to resolve problems; fewer logins with administrator privilege means fewer people to check with if things go wrong. The permission VIEW SERVER STATE is useful for allowing administrators and troubleshooters to view server information (dynamic management views) without granting full sysadmin or CONTROL SERVER permission.

Best practices for administrator privileges

  • Use administrator privileges only when needed.
  • Minimize the number of administrators.
  • Provision admin principals explicitly.
  • Have multiple distinct administrators if more than one is needed.
  • Avoid dependency on the builtin\administrators Windows group.

Database Ownership and Trust

A SQL Server instance can contain multiple user databases. Each user database has a specific owner; the owner defaults to the database creator. By definition, members of the sysadmin server role (including system administrators if they have access to SQL Server through their default group account) are database owners (DBOs) in every user database. In addition, there is a database role, db_owner, in every user database. Members of the db_owner role have approximately the same privileges as the dbo user.

SQL Server can be thought of as running in two distinct modes, which can be referred to as IT department mode and ISV mode. These are not database settings but simply different ways to manage SQL Server. In an IT department, the sysadmin of the instance manages all user databases. In an Internet service provider environment (say, a Web-hosting service), each customer is permitted to manage their own database and is restricted from accessing system databases or other user databases. For example, the databases of two competing companies could be hosted by the same Internet service provider (ISV) and exist in the same SQL Server instance. Dangerous code could be added to a user database when attached to its original instance, and the code would be enabled on the ISV instance when deployed. This situation makes controlling cross-database access crucial.

If each database is owned and managed by the same general entity, it is still not a good practice to establish a “trust relationship” with a database unless an application-specific feature, such as cross-database Service Broker communication, is required. A trust relationship between databases can be established by allowing cross-database ownership chaining or by marking a database as trusted by the instance by using the TRUSTWORTHY property. An example of setting the TRUSTWORTHY property follows:

ALTER DATABASE pubs SET TRUSTWORTHY ON

Best practices for database ownership and trust

  • Have distinct owners for databases; not all databases should be owned by sa.
  • Minimize the number of owners for each database.
  • Confer trust selectively.
  • Leave the Cross-Database Ownership Chaining setting off unless multiple databases are deployed at a single unit.
  • Migrate usage to selective trust instead of using the TRUSTWORTHY property.

Schemas

SQL Server 2005 introduces schemas to the database. A schema is simply a named container for database objects. Each schema is a scope that fits into the hierarchy between database level and object level, and each schema has a specific owner. The owner of a schema can be a user, a database role, or an application role. The schema name takes the place of the owner name in the SQL Server multi-part object naming scheme. In SQL Server 2000 and previous versions, a table named Employee that was part of a database named Payroll and was owned by a user name Bob would be payroll.bob.employee. In SQL Server 2005, the table would have to be part of a schema. If payroll_app is the name of the SQL Server 2005 schema, the table name in SQL Server 2005 is payroll.payroll_app.employee.

Schemas solve an administration problem that occurs when each database object is named after the user who creates it. In SQL Server versions prior to 2005, if a user named Bob (who is not dbo) creates a series of tables, the tables would be named after Bob. If Bob leaves the company or changes job assignments, these tables would have to be manually transferred to another user. If this transfer were not performed, a security problem could ensue. Because of this, prior to SQL Server 2005, DBAs were unlikely to allow individual users to create database objects such as tables. Each table would be created by someone acting as the special dbo user and would have a user name of dbo. Because, in SQL Server 2005, schemas can be owned by roles, special roles can be created to own schemas if needed—every database object need not be owned by dbo. Not having every object owned by dbo makes for more granular object management and makes it possible for users (or applications) that need to dynamically create tables to do so without dbo permission.

Having schemas that are role-based does not mean that it’s a good practice to have every user be a schema owner. Only users who need to create database objects should be permitted to do so. The ability to create objects does not imply schema ownership; GRANTing Bob ALTER SCHEMA permission in the payroll_app schema can be accomplished without making Bob a schema owner. In addition, granting CREATE TABLE to a user does not allow that user to create tables; the user must also have ALTER SCHEMA permission on some schema in order to have a schema in which to create the table. Objects created in a schema are owned by the schema owner by default, not by the creator of the object. This makes it possible for a user to create tables in a known schema without the administrative problems that ensue when that user leaves the company or switches job assignments.

Each user has a default schema. If an object is created or referenced in a SQL statement by using a one-part name, SQL Server first looks in the user’s default schema. If the object isn’t found there, SQL Server looks in the dbo schema. The user’s default schema is assigned by using the CREATE USER or ALTER USER DDL statements. If the default schema is specified, the default is dbo. Using named schemas for like groups of database objects and assigning each user’s default schema to dbo is a way to mandate using two-part object names in SQL statements. This is because objects that are not in the dbo schema will not be found when a one-part object name is specified. Migrating groups of user objects out of the dbo schema is also a good way to allow users to create and manage objects if needed (for example, to install an application package) without making the installing user dbo.

Best practices for using schemas

  • Group like objects together into the same schema.
  • Manage database object security by using ownership and permissions at the schema level.
  • Have distinct owners for schemas.
  • Not all schemas should be owned by dbo.
  • Minimize the number of owners for each schema.

Authorization

Authorization is the process of granting permissions on securables to users. At an operating system level, securables might be files, directories, registry keys, or shared printers. In SQL Server, securables are database objects. SQL Server principals include both instance-level principals, such as Windows logins, Windows group logins, SQL Server logins, and server roles and database-level principals, such as users, database roles, and application roles. Except for a few objects that are instance-scoped, most database objects, such as tables, views, and procedures are schema-scoped. This means that authorization is usually granted to database-level principals.

In SQL Server, authorization is accomplished via Data Access Language (DAL) rather than DDL or DML. In addition to the two DAL verbs, GRANT and REVOKE, mandated by the ISO-ANSI standard, SQL Server also contains a DENY DAL verb. DENY differs from REVOKE when a user is a member of more than one database principal. If a user Fred is a member of three database roles A, B, and C and roles A and B are GRANTed permission to a securable, if the permission is REVOKEd from role C, Fred still can access the securable. If the securable is DENYed to role C, Fred cannot access the securable. This makes managing SQL Server similar to managing other parts of the Windows family of operating systems.

SQL Server 2005 makes each securable available by using DAL statements and makes permissions more granular than in previous versions. For example, in SQL Server 2000 and earlier versions, certain functions were available only if a login was part of the sysadmin role. Now sysadmin role permissions are defined in terms of GRANTs. Equivalent access to securables can be achieved by GRANTing a login the CONTROL SERVER permission.

An example of better granularity is the ability to use SQL Server Profiler to trace events in a particular database. In SQL Server 2000, this ability was limited to the special dbo user. The new granular permissions are also arranged in a hierarchy; some permissions imply other permissions. For example, CONTROL permission on a database object type implies ALTER permission on that object as well as all other object-level permissions. SQL Server 2005 also introduces the concept of granting permissions on all of the objects in a schema. ALTER permission on a SCHEMA includes the ability to CREATE, ALTER, or DROP objects in that SCHEMA. The DAL statement that grants access to all securables in the payroll schema is:

GRANT SELECT ON schema::payroll TO fred

The advantage of granting permissions at the schema level is that the user automatically has permissions on all new objects created in the schema; explicit grant after object creation is not needed. For more information on the permission hierarchy, see the Permission Hierarchy section of SQL Server Books Online.

A best practice for authorization is to encapsulate access through modules such as stored procedures and user-defined functions. Hiding access behind procedural code means that users can only access objects in the way the developer and database administrator (DBA) intend; ad hoc changes to objects are disallowed. An example of this technique would be permitting access to the employee pay rate table only through a stored procedure “UpdatePayRate.” Users that need to update pay rates would be granted EXECUTE access to the procedure, rather than UPDATE access to the table itself. In SQL Server 2000 and earlier versions, encapsulating access was dependent on a SQL Server feature known as ownership chains. In an ownership chain, if the owner of stored procedure A and the owner of table B that the stored procedure accesses are the same, no permission check is done. Although this works well most of the time, even with multiple levels of stored procedures, ownership chains do not work when:

  • The database objects are in two different databases (unless cross-database ownership chaining is enabled).
  • The procedure uses dynamic SQL.
  • The procedure is a SQLCLR procedure.

SQL Server 2005 contains features to address these shortcomings, including signing of procedural code, alternate execution context, and a TRUSTWORTHY database property if ownership chaining is desirable because a single application encompasses multiple databases. All of these features are discussed in this white paper.

A login only can only be granted authorization to objects in a database if a database user has been mapped to the login. A special user, guest, exists to permit access to a database for logins that are not mapped to a specific database user. Because any login can use the database through the guest user, it is suggested that the guest user not be enabled.

SQL Server 2005 contains a new type of user, a user that is not mapped to a login. Users that are not mapped to logins provide an alternative to using application roles. You can invoke selective impersonation by using the EXECUTE AS statement (see Execution Context later in this paper) and allow that user only the privileges needed to perform a specific task. Using users without logins makes it easier to move the application to a new instance and limits the connectivity requirements for the function. You create a user without a login using DDL:

CREATE USER mynewuser WITHOUT LOGIN

Best practices for database object authorization

  • Encapsulate access within modules.
  • Manage permissions via database roles or Windows groups.
  • Use permission granularity to implement the principle of least privilege.
  • Do not enable guest access.
  • Use users without logins instead of application roles

Catalog Security

Information about databases, tables, and other database objects is kept in the system catalog. The system metadata exists in tables in the master database and in user databases. These metadata tables are exposed through metadata views. In SQL Server 2000, the system catalog was publicly readable and, the instance could be configured to make the system tables writeable as well. In SQL Server 2005, the system metadata tables are read-only and their structure has changed considerably. The only way that the system metadata tables are readable at all is in single-user mode. Also in SQL Server 2005, the system metadata views were refactored and made part of a special schema, the sys schema. So as not to break existing applications, a set of compatibility metadata views are exposed. The compatibility views may be removed in a future release of SQL Server.

SQL Server 2005 makes all metadata views secured by default. This includes:

  • The new metadata views (for example, sys.tables, sys.procedures).
  • The compatibility metadata views (for example, sysindexes, sysobjects).
  • The INFORMATION_SCHEMA views (provided for SQL-92 compliance).

The information in the system metadata views is secured on a per-row basis. In order to be able to see system metadata for an object, a user must have some permission on the object. For example, to see metadata about the dbo.authors table, SELECT permission on the table is sufficient. This prohibits browsing the system catalog by users who do not have appropriate object access. Discovery is often the first level of prevention. There are two exceptions to this rule: sys.databases and sys.schemas are public-readable. These metadata views may be secured with the DENY verb if required.

Some applications present lists of database objects to the user through a graphic user interface. It may be necessary to keep the user interface the same by permitting users to view information about database objects while giving them no other explicit permission on the object. A special permission, VIEW DEFINITION, exists for this purpose.

Best practices for catalog security

  • The catalog views are secure by default. No additional action is required to secure them.
  • Grant VIEW DEFINITION selectively at the object, schema, database, or server level to grant permission to view system metadata without conferring additional permissions.
  • Review legacy applications that may depend on access to system metadata when migrating the applications to SQL Server 2005.

Remote Data Source Execution

There are two ways that procedural code can be executed on a remote instance of SQL Server: configuring a linked server definition with the remote SQL Server and configuring a remote server definition for it. Remote servers are supported only for backward compatibility with earlier versions of SQL Server and should be phased out in preference to linked servers. Linked servers allow more granular security than remote servers. Ad hoc queries through linked servers (OPENROWSET and OPENDATASOURCE) are disabled by default in a newly installed instance of SQL Server 2005.

When you use Windows to authenticate to SQL Server, you are using a Windows network credential. Network credentials that use both NTLM and Kerberos security systems are valid for one network “hop” by default. If you use network credentials to log on to SQL Server and attempt to use the same credentials to connect via a linked server to a SQL Server instance on a different computer, the credentials will not be valid. This is known as the “double hop problem” and also occurs in environments that use Windows authentication to connect to a Web server and attempt to use impersonation to connect to SQL Server. If you use Kerberos for authentication, you can enable constrained delegation, that is, delegation of credentials constrained to a specific application, to overcome the “double hop problem.” Only Kerberos authentication supports delegation of Windows credentials. For more information, see Constrained Delegation in SQL Server Books Online.

Best practices for remote data source execution

  • Phase out any remote server definitions.
  • Replace remote servers with linked servers.
  • Leave ad hoc queries through linked servers disabled unless they are absolutely needed.
  • Use constrained delegation if pass-through authentication to a linked server is necessary.

Execution Context

SQL Server always executes SQL statements and procedural code as the currently logged on user. This behavior is a SQL Server-specific behavior and is made possible, in the case of procedural code, by the concept of ownership chains. That is, although a stored procedure executes as the caller of the stored procedure rather than as the owner, if ownership chaining is in place, permissions are not checked for object access and stored procedures can be used to encapsulate tables, as mentioned previously in this paper. In SQL Server 2005, the creator of a procedure can declaratively set the execution context of the procedure by using the EXECUTE AS keyword in the CREATE PROCEDURE, FUNCTION, and TRIGGER statements. The execution context choices are:

  • EXECUTE AS CALLER – the caller of the procedure (no impersonation). This is the only pre-SQL Server 2005 behavior.
  • EXECUTE AS OWNER – the owner of the procedure.
  • EXECUTE AS SELF – the creator of the procedure.
  • EXECUTE AS ‘username’ – a specific user.

To maintain backward compatibility, EXECUTE AS CALLER is the default. The distinction between AS OWNER and AS SELF is needed because the creator of the procedure may not be the owner of the schema in which the procedure resides. In this case, AS SELF refers to the procedure owner, AS OWNER refers to the object owner (the schema owner). In order to use EXECUTE AS ‘username’, the procedure creator must have IMPERSONATE permission on the user named in the execution context.

One reason to use an alternate execution context would be when a procedure executes without a particular execution context. An example of this is a service broker queue activation procedure. In addition, EXECUTE AS OWNER can be used to circumvent problems that are caused when ownership chains are broken. For example, ownership chains in a procedure are always broken when dynamic SQL statements (such as sp_executeSQL) are used.

Often what is needed is to grant the appropriate permissions to the procedural code itself, rather than either changing the execution context or relying on the caller’s permissions. SQL Server 2005 offers a much more granular way of associating privileges with procedural code—code signing. By using the ADD SIGNATURE DDL statement, you can sign the procedure with a certificate or asymmetric key. A user can then be created for the certificate or asymmetric key itself and permissions assigned to that user. When the procedure is executed, the code executes with a combination of the caller’s permissions and the key/certificate’s permissions. An example of this would be:

CREATE CERTIFICATE HRCertificate

WITH ENCRYPTION BY PASSWORD = ‘HacdeNj162kqT’

CREATE USER HRCertificateUser

FOR CERTIFICATE HRCertificate WITHOUT LOGIN

GRANT UPDATE ON pension_criteria TO HRCertificate

– this gives the procedure update_pension_criteria

– additional privileges of HRCertificate

ADD SIGNATURE TO update_pension_criteria BY CERTIFCATE HRCertificate

– backup the private key and remove it from the certificate,

– so that the procedure cannot be re-signed without permission

BACKUP CERTIFICATE HRCertificate

TO FILE = ‘c:\certs_backup\HRCertificate.cer’

WITH PRIVATE KEY (FILE = ‘c:\certs_backup\ HRCertificate.pvk’,

ENCRYPTION BY PASSWORD = ‘jBjebfP43j1!’,

DECRYPTION BY PASSWORD = ‘eWyveyYqW96A@!q’)

ALTER CERTIFICATE HRCertificate REMOVE PRIVATE KEY

EXECUTE AS can also be used to set the execution context within an SQL batch. In this form, the SQL batch contains an EXECUTE AS USER=’someuser’ or EXECUTE AS LOGIN=’somelogin’ statement. This alternate execution context lasts until the REVERT statement is encountered. EXECUTE AS and REVERT blocks can also be nested; REVERT reverts one level of execution context. As with EXECUTE AS and procedural code, the user changing the execution context must have IMPERSONATE permission on the user or login being impersonated. EXECUTE AS in SQL batches should be used as a replacement for the SETUSER statement, which is much less flexible.

If the execution context is set but should not be reverted without permission, you can use EXECUTE AS … WITH COOKIE or EXECUTE AS … WITH NO REVERT. When WITH COOKIE is specified, a binary cookie is returned to the caller of EXECUTE AS and the cookie must be supplied in order to REVERT back to the original context.

When a procedure or batch uses an alternate execution context, the system functions normally used for auditing, such as SUSER_NAME(), return the name of the impersonated user rather than the name of the original user or original login. A new system function, ORIGINAL_LOGIN(), can be used to obtain the original login, regardless of the number of levels of impersonation used.

Best practices for execution context

  • Set execution context on modules explicitly rather than letting it default.
  • Use EXECUTE AS instead of SETUSER.
  • Use WITH NO REVERT/COOKIE instead of Application Roles.
  • Consider using code signing of procedural code if a single granular additional privilege is required for the procedure.

Encryption

SQL Server 2005 has built-in data encryption. The data encryption exists at a cell level and is accomplished by means of built-in system procedures. Encrypting data requires secure encryption keys and key management. A key management hierarchy is built into SQL Server 2005. Each instance of SQL Server has a built-in service master key that is generated at installation; specifically, the first time that SQL Server is started after installation. The service master key is encrypted by using both the SQL Server Service account key and also the machine key. Both encryptions use the DPAPI (Data Protection API). A database administrator can define a database master key by using the following DDL.

CREATE MASTER KEY

WITH ENCRYPTION BY PASSWORD = ’87(HyfdlkRM?_764#GRtj*(NS£”_+^$(‘

This key is actually encrypted and stored twice by default. Encryption that uses a password and storage in the database is required. Encryption that uses the service master key and storage in the master database is optional; it is useful to be able to automatically open the database master key without specifying the password. The service master key and database master keys can be backed up and restored separately from the rest of the database.

SQL Server 2005 can use DDL to define certificates, asymmetric keys, and symmetric keys on a per-database basis. Certificates and asymmetric keys consist of a private key/public key pair. The public key can be used to encrypt data that can be decrypted only by using the private key. Or, for the sake of performance, the public key can be used to encrypt a hash that can be decrypted only by using the private key. Encrypted checksum generation to ensure non-repudiation is known as signing.

Alternatively, the private key can be used to encrypt data that can be decrypted by the receiver by using the public key. A symmetric key consists of a single key that is used for encryption and decryption. Symmetric keys are generally used for data encryption because they are orders of magnitude faster than asymmetric keys for encryption and decryption. However, distributing symmetric keys can be difficult because both parties must have the same copy of the key. In addition, it is not possible with symmetric key encryption to determine which user encrypted the data. Asymmetric keys can be used to encrypt and decrypt data but ordinarily they are used to encrypt and decrypt symmetric keys; the symmetric keys are used for the data encryption. This is the preferred way to encrypt data for the best security and performance. Symmetric keys can also be protected by individual passwords.

SQL Server 2005 makes use of and also can generate X.509 certificates. A certificate is simply an asymmetric key pair with additional metadata, including a subject (the person the key is intended for), root certificate authority (who vouches for the certificate’s authenticity), and expiration date. SQL Server generates self-signed certificates (SQL Server itself is the root certificate authority) with a default expiration date of one year. The expiration date and subject can be specified in the DDL statement. SQL Server does not use certificate “negative lists” or the expiration date with data encryption. A certificate can be backed up and restored separately from the database; certificates, asymmetric keys, and symmetric keys are backed up with the database. A variety of block cipher encryption algorithms are supported, including DES, Triple DES, and AES (Rijndael) algorithms for symmetric keys and RSA for asymmetric keys. A variety of key strengths are supported for each algorithm. Stream cipher algorithms, such as RC4 are also supported but should NOT be used for data encryption. Some algorithms (such as AES) are not supported by all operating systems that can host SQL Server. User-defined algorithms are not supported. The key algorithm and key length choice should be predicated on the sensitivity of the data.

SQL Server encrypts data on a cell level—data is specifically encrypted before it is stored into a column value and each row can use a different encryption key for a specific column. To use data encryption, a column must use the VARBINARY data type. The length of the column depends on the encryption algorithm used and the length of the data to be encrypted (see Choosing an Encryption Algorithm in SQL Server Books Online). The KEY_GUID of the key that is used for encryption is stored with the column value. When the data is decrypted, this KEY_GUID is checked against all open keys in the session. The data uses initialization vectors (also known as salted hashes). Because of this, it is not possible to determine if two values are identical by looking at the encrypted value. This means, for example, that I cannot determine all of the patients who have a diagnosis of Flu if I know that Patient A has a diagnosis of Flu. Although this means that data is more secure, it also means that you cannot use a column that is encrypted by using the data encryption routines in indexes, because data values are not comparable.

Data encryption is becoming more commonplace with some vendors and industries (for example, the payment card industry). Use data encryption only when it is required or for very high-value sensitive data. In some cases, encrypting the network channel or using SQL Server permissions is a better choice because of the complexity involved in managing keys and invoking encryption/decryption routines.

Because unencrypted data must be stored in memory buffers before being transmitted to clients, it is impossible to keep data away from an administrator who has the ability to debug the process or to patch the server. Memory dumps can also be a source of unintended data leakage. If symmetric keys are protected by asymmetric keys and the asymmetric keys are encrypted by using the database master key, a database administrator could impersonate a user of encrypted data and access the data through the keys. If protection from the database administrator is preferred, encryption keys must be secured by passwords, rather than by the database master key. To guard against data loss, encryption keys that are secured by passwords must have an associated disaster recovery policy (offsite storage, for example) in case of key loss. You can also require users to specify the database master key by dropping encryption of the database master key by the instance master key. Remember to back up the database in order to back up the symmetric keys, because there are no specific DDL statements to back up symmetric and asymmetric keys, just as there are specific DDL statements to back up certificates, the database master key, and the service master key.

Best practices for data encryption

  • Encrypt high-value and sensitive data.
  • Use symmetric keys to encrypt data, and asymmetric keys or certificates to protect the symmetric keys.
  • Password-protect keys and remove master key encryption for the most secure configuration.
  • Always back up the service master key, database master keys, and certificates by using the key-specific DDL statements.
  • Always back up your database to back up your symmetric and asymmetric keys.

Auditing

SQL Server 2005 supports login auditing, trigger-based auditing, and event auditing by using a built-in trace facility. Password policy compliance is automatically enforceable through policy in SQL Server 2005 for both Windows logins and SQL logins. Login auditing is available by using an instance-level configuration parameter. Auditing failed logins is the default, but you can specify to audit all logins. Although auditing all logins increases overhead, you may be able to deduce patterns of multiple failed logins followed by a successful login, and use this information to detect a possible login security breech. Auditing is provided on a wide variety of events including Add Database User, Add Login, DBCC events, Change Password, GDR events (Grant/Deny/Revoke events), and Server Principal Impersonation events. SQL Server 2005 SP2 also supports login triggers.

SQL Server 2005 introduces auditing based on DDL triggers and event notifications. You can use DDL triggers not only to record the occurrence of DDL, but also to roll back DDL statements as part of the trigger processing. Because a DDL trigger executes synchronously (the DDL does not complete until the trigger is finished), DDL triggers can potentially slow down DDL, depending on the content and volume of the code. Event notifications can be used to record DDL usage information asynchronously. An event notification is a database object that uses Service Broker to send messages to the destination (Service Broker-based) service of your choosing. DDL cannot be rolled back by using event notifications.

Because the surface area of SQL Server 2005 is larger than previous versions, more auditing events are available in SQL Server 2005 than in previous versions. To audit security events, use event-based auditing, specifically the events in the security audit event category (listed in SQL Server Books Online). Event-based auditing can be trace-based, or event notifications-based. Trace-based event auditing is easier to configure, but may result in a large event logs, if many events are traced. On the other hand, event notifications send queued messages to Service Broker queues that are in-database objects. Trace-based event auditing cannot trace all events; some events, such as SQL:StmtComplete events, are not available when using event notifications.

There is a WMI provider for events that can be used in conjunction with SQL Server Agent alerts. This mechanism provides immediate notification through the Alert system that a specific event has occurred. To use the WMI provider, select a WMI-based alert and provide a WQL query that produces the event that you want to cause the alert. WQL queries use the same syntax for naming as does event notifications. An example of a WQL query that looks for database principal impersonation changes would be:

SELECT * FROM AUDIT_DATABASE_PRINCIPAL_IMPERSONATION_EVENT

SQL Server can be configured to support auditing that is compliant with C2 certification under the Trusted Database Interpretation (TDI) of the Trusted Computer System Evaluation Criteria (TCSEC) of the United States National Security Agency. This is known as C2 auditing. C2 auditing is configured on an instance level by using the C2 audit mode configuration option in sp_configure.

When C2 auditing is enabled, data is saved in a log file in the Data subdirectory in the directory in which SQL Server is installed. The initial log file size for C2 auditing is 200 megabytes. When this file is full, another 200 megabytes is allocated. If the volume on which the log file is stored runs out of space, SQL Server shuts down until sufficient space is available or until the system is manually started without auditing. Ensure that there is sufficient space available before enabling C2 auditing and put a procedure in place for archiving the log files.

SQL Server 2005 SP2 allows configuring an option that provides three elements required for Common Criteria compliance. The Common Criteria represents the outcome of efforts to develop criteria for evaluation of IT security that are widely useful within the international community. It stems from a number of source criteria: the existing European, US, and Canadian criteria (ITSEC, TCSEC, and CTCPEC respectively). The Common Criteria resolves the conceptual and technical differences between the source criteria. The three Common Criteria elements that can be configured by using an instance configuration option are:

  • Residual Information Protection, which overwrites memory with a known bit pattern before it is reallocated to a new resource.
  • The ability to view login statistics.
  • A column-level GRANT does not override table-level DENY.

You can configure an instance to provide these three elements for Common Criteria compliance by setting the configuration option common criteria compliance enabled as shown in the following code.

sp_configure ‘show advanced options’, 1;

GO

RECONFIGURE;

GO

sp_configure ‘common criteria compliance enabled’, 1;

GO

RECONFIGURE;

GO

In addition to enabling the Common Criteria options in a SQL Server instance, you can use login triggers in SQL Server 2005 SP2 to limit logins based upon time of day or based on an excessive number of existing connections. The ability to limit logins based on these criteria is required for Common Criteria compliance.

Best practices for auditing

  • Auditing is scenario-specific. Balance the need for auditing with the overhead of generating addition data.
  • Audit successful logins in addition to unsuccessful logins if you store highly sensitive data.
  • Audit DDL and specific server events by using trace events or event notifications.
  • DML must be audited by using trace events.
  • Use WMI to be alerted of emergency events.
  • Enable C2 auditing or Common Criteria compliance only if required.

Microsoft Baseline Security Analyzer and SQL Server Best Practices Analyzer

Microsoft Baseline Security Analyzer (MBSA) is a utility that scans for common insecurities in a SQL Server configuration. Run MBSA on a regularly scheduled basis, either locally or across the network. MBSA scans for Windows operating system, security principal, network, and file system insecurities and tests for SQL Server 2000 and MSDE-specific insecurities, but does not incorporate SQL Server 2005-specific checks yet. It will check to see if SQL Server 2005 is patched to the latest Service Pack version.

SQL Server 2005 Best Practices Analyzer 2.0 CTP has been released in conjunction with Service Pack 2. You can download it from the Microsoft Download Center,  SQL Server 2005 Best Practices Analyzer (February 2007 CTP) page. SQL Server 2005 Best Practices Analyzer (BPA) gathers data from Microsoft Windows and SQL Server configuration settings.  Best Practices Analyzer uses a predefined list of SQL Server 2005 recommendations and best practices to determine if there are potential security issues in the database environment.

Best practice analysis utilities recommendations

  • Run BPA against SQL Server 2005.
  • Regularly run MBSA 2.0 to ensure latest SQL Server 2005 patch level
  • Regularly run MBSA 2.0 for SQL Server 2000 instances

Patching

The best way to ensure the security of the server software and to ensure the security of SQL Server 2005 is to install security hotfixes and service packs as soon as possible. Use manual updates on an operating system basis by using Windows Update or Microsoft Update. You can enable automatic updates using Windows Update or Microsoft Update as well, but updates should be tested before they are applied to production systems. SQL Server 2005 incorporates SQL Server hotfixes and service packs into Windows Update. All hotfixes should be installed immediately and service packs should be tested and installed as soon as possible. This requirement cannot be emphasized enough. For suggestions on minimizing downtime when installing hotfixes and service packs, see Preventing Reboots, Installing Multiple Updates, and More on Microsoft TechNet.

Best practices for patching SQL Server

  • Always stay as current as possible.
  • Enable automatic updates whenever feasible but test them before applying to production systems.

Conclusion

Security is a crucial part of any mission-critical application. To implement security for SQL Server 2005 in a way that is not prone to mistakes, security setup must be relatively easy to implement. The “correct” security configuration should be the default configuration. This paper describes how it is a straightforward task to start from the SQL Server 2005 security defaults and create a secure database configuration according to the Trustworthy Computing Initiative guidelines.

For more information:

http://www.microsoft.com/technet/prodtechnol/sql/themes/security.mspx

Did this paper help you? Please give us your feedback. On a scale of 1 (poor) to 5 (excellent), how would you rate this paper?

ms sql 2008 Rebuilding the log when attaching multiple .MDF files

Part 1 of this article series illustrated the use of the “Create Database” statement “For Attach” and “for ATTACH_REBUILD_LOG” for a single .MDF file and single .LDF file. Part 2 illustrated how to handle multiple .MDF files and .LDF files when attaching the database. Part 3 of the series illustrates how to rebuild the log when attaching multiple .MDF files.

Now let’s assume that we have the database, MyDB1, with eighteen .MDF files and seventeen .LDF files. Execute the following command.

USE [master]
GO

/****** Object:  Database [MyDB1]    Script Date: 08/11/2009 11:54:20 ******/
IF  EXISTS (SELECT name FROM sys.databases WHERE name = N'MyDB1')
DROP DATABASE [MyDB1]
GO

CREATE DATABASE MyDB1
ON
( NAME = MyDB1_dat1,FILENAME = 'C:\Data\MyDB1.mdf'),
( NAME = MyDB1_dat2,FILENAME = 'C:\Data\MyDB2.mdf'),
( NAME = MyDB1_dat3,FILENAME = 'C:\Data\MyDB3.mdf'),
( NAME = MyDB1_dat4,FILENAME = 'C:\Data\MyDB4.mdf'),
( NAME = MyDB1_dat5,FILENAME = 'C:\Data\MyDB5.mdf'),
( NAME = MyDB1_dat6,FILENAME = 'C:\Data\MyDB6.mdf'),
( NAME = MyDB1_dat7,FILENAME = 'C:\Data\MyDB7.mdf'),
( NAME = MyDB1_dat8,FILENAME = 'C:\Data\MyDB8.mdf'),
( NAME = MyDB1_dat9,FILENAME = 'C:\Data\MyDB9.mdf'),
( NAME = MyDB1_dat10,FILENAME = 'C:\Data\MyDB10.mdf'),
( NAME = MyDB1_dat11,FILENAME = 'C:\Data\MyDB11.mdf'),
( NAME = MyDB1_dat12,FILENAME = 'C:\Data\MyDB12.mdf'),
( NAME = MyDB1_dat13,FILENAME = 'C:\Data\MyDB13.mdf'),
( NAME = MyDB1_dat14,FILENAME = 'C:\Data\MyDB14.mdf'),
( NAME = MyDB1_dat15,FILENAME = 'C:\Data\MyDB15.mdf'),
( NAME = MyDB1_dat16,FILENAME = 'C:\Data\MyDB16.mdf'),
( NAME = MyDB1_dat17,FILENAME = 'C:\Data\MyDB17.mdf'),
( NAME = MyDB1_dat18,FILENAME = 'C:\Data\MyDB18.mdf')
LOG ON
( NAME = MyDB1_log1,FILENAME = 'C:\Data\MyDB1.ldf'),
( NAME = MyDB1_log2,FILENAME = 'C:\Data\MyDB2.ldf'),
( NAME = MyDB1_log3,FILENAME = 'C:\Data\MyDB3.ldf'),
( NAME = MyDB1_log4,FILENAME = 'C:\Data\MyDB4.ldf'),
( NAME = MyDB1_log5,FILENAME = 'C:\Data\MyDB5.ldf'),
( NAME = MyDB1_log6,FILENAME = 'C:\Data\MyDB6.ldf'),
( NAME = MyDB1_log7,FILENAME = 'C:\Data\MyDB7.ldf'),
( NAME = MyDB1_log8,FILENAME = 'C:\Data\MyDB8.ldf'),
( NAME = MyDB1_log9,FILENAME = 'C:\Data\MyDB9.ldf'),
( NAME = MyDB1_log10,FILENAME = 'C:\Data\MyDB10.ldf'),
( NAME = MyDB1_log11,FILENAME = 'C:\Data\MyDB11.ldf'),
( NAME = MyDB1_log12,FILENAME = 'C:\Data\MyDB12.ldf'),
( NAME = MyDB1_log13,FILENAME = 'C:\Data\MyDB13.ldf'),
( NAME = MyDB1_log14,FILENAME = 'C:\Data\MyDB14.ldf'),
( NAME = MyDB1_log15,FILENAME = 'C:\Data\MyDB15.ldf'),
( NAME = MyDB1_log16,FILENAME = 'C:\Data\MyDB16.ldf'),
( NAME = MyDB1_log17,FILENAME = 'C:\Data\MyDB17.ldf')
GO

Let’s detach the database and delete all of the .MDF files.

use master
go
sp_detach_db MyDB1
go
exec master..xp_cmdshell 'Del c:\data\mydb1.ldf'
go
exec master..xp_cmdshell 'Del c:\data\mydb2.ldf'
go
exec master..xp_cmdshell 'Del c:\data\mydb3.ldf'
go
exec master..xp_cmdshell 'Del c:\data\mydb4.ldf'
go
exec master..xp_cmdshell 'Del c:\data\mydb5.ldf'
go
exec master..xp_cmdshell 'Del c:\data\mydb6.ldf'
go
exec master..xp_cmdshell 'Del c:\data\mydb7.ldf'
go
exec master..xp_cmdshell 'Del c:\data\mydb8.ldf'
go
exec master..xp_cmdshell 'Del c:\data\mydb9.ldf'
go
exec master..xp_cmdshell 'Del c:\data\mydb10.ldf'
go
exec master..xp_cmdshell 'Del c:\data\mydb11.ldf'
go
exec master..xp_cmdshell 'Del c:\data\mydb12.ldf'
go
exec master..xp_cmdshell 'Del c:\data\mydb13.ldf'
go
exec master..xp_cmdshell 'Del c:\data\mydb14.ldf'
go
exec master..xp_cmdshell 'Del c:\data\mydb15.ldf'
go
exec master..xp_cmdshell 'Del c:\data\mydb16.ldf'
go
exec master..xp_cmdshell 'Del c:\data\mydb17.ldf'
go

Note: I am using xp_cmdshell to delete the .ldf file. You will get the following error, if xp_cmdshell is not enabled.

Error:

Msg 15281, Level 16, State 1, Procedure xp_cmdshell, Line 1
SQL Server blocked access to procedure 'sys.xp_cmdshell' of component 'xp_cmdshell'
because this component is turned off as part of the security configuration for this server.
A system administrator can enable the use of 'xp_cmdshell' by using sp_configure. For more
information about enabling 'xp_cmdshell', see "Surface Area Configuration" in SQL Server Books Online.

You can enable xp_cmdshell using the following Transact SQL Statement.

use master
go
sp_configure 'show advanced options',1
go
reconfigure with override
go
sp_configure 'xp_cmdshell',1
go
reconfigure with override
go

Alternatively, you could delete the .ldf file using the Windows Explorer “Del” command at the MS-DOS command prompt.

Now let’s try to attach only .MDF files using sp_attach_db. Execute the following command.

sp_attach_db 'MyDB1','C:\Data\MyDB1.mdf',
'C:\Data\MyDB2.mdf','C:\Data\MyDB3.mdf',
'C:\Data\MyDB4.mdf','C:\Data\MyDB5.mdf',
'C:\Data\MyDB6.mdf','C:\Data\MyDB7.mdf',
'C:\Data\MyDB8.mdf','C:\Data\MyDB9.mdf',
'C:\Data\MyDB10.mdf','C:\Data\MyDB11.mdf',
'C:\Data\MyDB12.mdf','C:\Data\MyDB13.mdf',
'C:\Data\MyDB14.mdf','C:\Data\MyDB15.mdf',
'C:\Data\MyDB16.mdf','C:\Data\MyDB17.mdf',
'C:\Data\MyDB18.mdf'

Result:

Msg 8144, Level 16, State 2, Procedure sp_attach_db, Line 0
Procedure or function sp_attach_db has too many arguments specified.

Since we are attaching more files, we could not use the sp_attach_single_file_db system stored procedure. So let’s try to attach them using the “Create database” statement with “for ATTACH_REBUILD_LOG”. Execute the following transact SQL statement.

CREATE DATABASE MyDB1
ON
(FILENAME = 'C:\Data\MyDB1.mdf'),
(FILENAME = 'C:\Data\MyDB2.mdf'),
(FILENAME = 'C:\Data\MyDB3.mdf'),
(FILENAME = 'C:\Data\MyDB4.mdf'),
(FILENAME = 'C:\Data\MyDB5.mdf'),
(FILENAME = 'C:\Data\MyDB6.mdf'),
(FILENAME = 'C:\Data\MyDB7.mdf'),
(FILENAME = 'C:\Data\MyDB8.mdf'),
(FILENAME = 'C:\Data\MyDB9.mdf'),
(FILENAME = 'C:\Data\MyDB10.mdf'),
(FILENAME = 'C:\Data\MyDB11.mdf'),
(FILENAME = 'C:\Data\MyDB12.mdf'),
(FILENAME = 'C:\Data\MyDB13.mdf'),
(FILENAME = 'C:\Data\MyDB14.mdf'),
(FILENAME = 'C:\Data\MyDB15.mdf'),
(FILENAME = 'C:\Data\MyDB16.mdf'),
(FILENAME = 'C:\Data\MyDB17.mdf'),
(FILENAME = 'C:\Data\MyDB18.mdf')
for ATTACH_REBUILD_LOG
GO

Result:

File activation failure. The physical file name "C:\Data\MyDB1.ldf" may be incorrect.
File activation failure. The physical file name "C:\Data\MyDB2.ldf" may be incorrect.
File activation failure. The physical file name "C:\Data\MyDB3.ldf" may be incorrect.
File activation failure. The physical file name "C:\Data\MyDB4.ldf" may be incorrect.
File activation failure. The physical file name "C:\Data\MyDB5.ldf" may be incorrect.
File activation failure. The physical file name "C:\Data\MyDB6.ldf" may be incorrect.
File activation failure. The physical file name "C:\Data\MyDB7.ldf" may be incorrect.
File activation failure. The physical file name "C:\Data\MyDB8.ldf" may be incorrect.
File activation failure. The physical file name "C:\Data\MyDB9.ldf" may be incorrect.
File activation failure. The physical file name "C:\Data\MyDB10.ldf" may be incorrect.
File activation failure. The physical file name "C:\Data\MyDB11.ldf" may be incorrect.
File activation failure. The physical file name "C:\Data\MyDB12.ldf" may be incorrect.
File activation failure. The physical file name "C:\Data\MyDB13.ldf" may be incorrect.
File activation failure. The physical file name "C:\Data\MyDB14.ldf" may be incorrect.
File activation failure. The physical file name "C:\Data\MyDB15.ldf" may be incorrect.
File activation failure. The physical file name "C:\Data\MyDB16.ldf" may be incorrect.
File activation failure. The physical file name "C:\Data\MyDB17.ldf" may be incorrect.
New log file 'C:\Data\MyDB1_log.LDF' was created.

Conclusion

This article has illustrated how to rebuild the log when attaching multiple .MDF files. The next part of the series is going to illustrate attaching multiple .MDF and .LDF files of the database with file groups.

MY SQL tutorial

Comparing MySQL Statement-Based and Row-Based Replication 2/01
There are many ways you can replicate MySQL data, whether it be a simple two-node cluster, chain or master/master. This article explains the two types of data replication (Statement-Based Replication and Row-Based Replication) available in MySQL.

Identify Slow Queries using MySQL’s Analysis Tools 1/28
Using MySQL analysis tools, you can identify and optimize slow queries in order to eliminate the bottlenecks that they cause. Rob Gravelle examines the Slow Query Log and Explain command, which can both help identify slow queries.

Top 5 Considerations for MySQL Administration in the Cloud 1/19
Cloud deployments of internet sites are becoming much more prevalent every day. Let’s take a look at what type of special considerations we’ll need to make for hosting MySQL successfully in the cloud.

Top 10 MySQL Best Practices 1/12
Rob Gravelle lists his top 10 MySQL best practices for database administrators, architects, developers, and security personnel.

MySQL Workbench – Top 13 things you should know 1/05
MySQL Workbench has come a long way and keeps getting better! I honestly don’t think that millage may vary on this one as it is one of the best no-cost GUI tools created for MySQL.

Writing Reusable Queries in MySQL 1/04
While stored procedures are an integral part of writing reusable code, they are only one ingredient in a bigger pie. Other key players include views and native as well as user-defined functions. This article explains how all of these elements fit together so that your SELECT statements can be applied to a greater variety of queries.

Limiting the Number of Rows in MySQL Result Sets 12/22
In the Web 2.0 world where smart phone and Web applications are competing to access terabytes of data, there is a new imperative for speeding up retrieval times as much as the technology permits. Rob Gravelle lists a few coding techniques that we as database administrators and developers can use to whittle down unwieldy results sets into manageable chunks.

Troubleshooting MySQL Slow Queries with Tcpdumps 12/21
MySQL has a wonderful facility for capturing slow queries called the slow query log. However if yours is not enabled when trouble occurs, and restarting the database is not an option, you’ll need another solution. This is where a tcpdump can come in handy.

Optimizing MySQL Query Retrieval Speed Through Table Joins 12/10
A poorly optimized query can become the bottleneck in an otherwise highly optimized process. In many cases, the source of the slow performance is improper table joining in the WHERE clause. With that in mind, this article looks at ways to optimize table joins for speedy data retrieval.

MySQL Point in Time Backups 12/07
Point in time backup and recovery is a crucial component of any part of any MySQL environment. This article describes how to implement the basic point in time recovery and describes a few mechanisms to accomplish this goal.

Use Derived Tables in Your MySQL Queries to Improve Database Performance 12/03
There has been much debate as to whether derived tables, views, or temporary tables are actually faster in terms of database performance. The fact is that there is no simple answer, as different queries require different optimizations. Rob Gravelle presents and evaluates some uses for derived tables that highlight their value in many applications.

MySQL Hotbackups with XtraBackup 11/16
Hotbackups are a staple of many different database platforms, but they remain sadly absent from the core MySQL distribution. Not to worry, the open-source utility has just arrived to rescue you.

Eliminating Duplicate Rows from MySQL Result Sets 11/12
The go to solution for removing duplicate rows from your result sets is to include the distinct keyword in your select statement. It tells the query engine to remove duplicates to produce a result set in which every row is unique. The group by clause can also be used to remove duplicates.

Simple Automation – Examining your MySQL Index Usage 11/02
In the majority of MySQL systems, the schema is always updated and changed based on the needs and focus of the application, making it somewhat difficult to keep up with all of the changes that come down the pipe. Chris Schneider shows you how to gauge the index usage in a system, using grep, awk and sed.

Clearing a Path through the 3NF Join Jungle 10/22
Rob Gravelle explores the pros and cons of normalization with a particular focus on the ramifications of third normal form normalization on data extraction by SELECT query.

iis server tricks
sql server tricks
dedicated server
server hardware
computer help

Test SQL Connectivity ms sql script

Test SQL Connectivity

>>Script Language and Platform: VBScript
This script tests connectivity to SQL Server box from a client machine when there is no SQL Client installed.

Usage:

testconnectivity.vbs Servername dbname Loginname Password

MS SQL Export output to XML

Part 1 and Part 2 of this series discussed Power Shell installation and simple SMO, WMI cmdlets. Part 3 covered how to script PowerShell and connect to SQL Server. Part 4 explained how to use a PowerShell script to loop through the content of a file and connect to different servers. Part 5 examined creating a SQL Server database using PowerShell and SMO. Part 6 talked about backing up a SQL Server database using PowerShell and SMO. Part 7 illustrated how to list all of the objects in a database and part 8 demonstrated how to list all of the properties of the objects in a database using PowerShell and SMO. Part 9 of this article series illustrated how to use PowerShell and SMO to generate a script for database and tables. Part 10 illustrated creating the PowerShell script to generate a script for the database and tables.

This installment of the series illustrates how to use PowerShell cmdlets in conjunction with the SQL Server client and output redirection to export to a text file or XML file.

Let us assume we want to query any SQL Server table using transact sql and store the output in text format or in XML format. Using PowerShell cmdlets, SQL Server client connection and output redirection, this can be achieved very easily.

Let us create c:\ps\output.ps1 as shown below. [Refer Fig 1.1]

param
(
  [string] $SQLServer,
  [string] $Database,
  [string] $outputType,
  [string] $filename,
  [string] $Query
)

$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString =
"Server=$SQLSERVER;Database=$DATABASE;Integrated Security=True"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand

$SqlCmd.CommandText = $Query
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$SqlConnection.Close()

if ($outputType  -eq "Text")
{
$DataSet.Tables[0] | format-table -auto > $filename
}

if ($outputType  -eq "xml")
{
$DataSet.Tables[0] |Export-Clixml $filename
}

Click for larger image

Fig 1.1

This script can be executed as shown below. [Refer Fig 1.2]

./output "HOME\SQLEXPRESS" "VixiaTrack" "TEXT" "c:\test.txt" "Select dbid,name from sys.sysdatabases"


Fig 1.2

Parameters explained:

  • output is actually the output.ps1 script in the folder c:\ps
  • HOME is the hostname
  • SQLEXPRESS is the sql server instance name on the host HOME
  • VixiaTrack is the database name that resides in the SQLEXPRESS instance
  • TEXT is the ouput format desired. It could be TEXT or XML

  • C:\test.txt is the filename and its location
  • Select dbid,name from sys.sysdatabases is the actual Transact SQL query executed against the database

When the PowerShell script is executed, it queries the database and saves the output to a text file that has been passed as parameter. [Refer Fig 1.3 and Fig 1.4]


Fig 1.3

Content of the test.txt file

dbid name
---- ----
   1 master
   2 tempdb
   3 model
   4 msdb
   5 test
   6 VixiaTrack
   7 XMLTest
   8 admin
   9 AdventureWorks


Fig 1.4

The same PowerShell script can be executed using XML as the parameter in order to generate the result in XML format.

This script can be executed as shown below. [Refer Fig 1.5]

./output "HOME\SQLEXPRESS" "VixiaTrack" "XML" "c:\test.xml" "Select dbid,name from sys.sysdatabases"


Fig 1.5

Parameters explained:

  • output is actually the output.ps1 script in the folder c:\ps
  • HOME is the hostname
  • SQLEXPRESS is the sql server instance name on the host HOME
  • VixiaTrack is the database name that resides in SQLEXPRESS instance
  • XML is the ouput format desired. It could be TEXT or XML.

  • C:\test.txt is the filename and its location.
  • Select dbid,name from sys.sysdatabases is the actual Transact SQL query executed against the database

When the PowerShell script is executed, it queries the database and saves the output to a XML file that has been passed as parameter. [Refer Fig 1.6 and Fig 1.7]


Fig 1.6

Content of the test.xml file

- <Objs Version="1.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
- <Obj RefId="RefId-0">
- <TN RefId="RefId-0">
  <T>System.Data.DataRow</T>
  <T>System.Object</T>
  </TN>
- <Props>
  <I16 N="dbid">1</I16>
  <S N="name">master</S>
  </Props>
  </Obj>
- <Obj RefId="RefId-0">
  <TNRef RefId="RefId-0" />
- <Props>
  <I16 N="dbid">2</I16>
  <S N="name">tempdb</S>
  </Props>
  </Obj>
- <Obj RefId="RefId-0">
  <TNRef RefId="RefId-0" />
- <Props>
  <I16 N="dbid">3</I16>
  <S N="name">model</S>
  </Props>
  </Obj>
- <Obj RefId="RefId-0">
  <TNRef RefId="RefId-0" />
- <Props>
  <I16 N="dbid">4</I16>
  <S N="name">msdb</S>
  </Props>
  </Obj>
- <Obj RefId="RefId-0">
  <TNRef RefId="RefId-0" />
- <Props>
  <I16 N="dbid">5</I16>
  <S N="name">test</S>
  </Props>
  </Obj>
- <Obj RefId="RefId-0">
  <TNRef RefId="RefId-0" />
- <Props>
  <I16 N="dbid">6</I16>
  <S N="name">VixiaTrack</S>
  </Props>
  </Obj>
- <Obj RefId="RefId-0">
  <TNRef RefId="RefId-0" />
- <Props>
  <I16 N="dbid">7</I16>
  <S N="name">XMLTest</S>
  </Props>
  </Obj>
- <Obj RefId="RefId-0">
  <TNRef RefId="RefId-0" />
- <Props>
  <I16 N="dbid">8</I16>
  <S N="name">admin</S>
  </Props>
  </Obj>
- <Obj RefId="RefId-0">
  <TNRef RefId="RefId-0" />
- <Props>
  <I16 N="dbid">9</I16>
  <S N="name">AdventureWorks</S>
  </Props>
  </Obj>
  </Objs>


Fig 1.7

Conclusion

Part 11 of this article series illustrated how to use PowerShell cmdlets in conjunction with the SQL Server client and output redirection to export to a text file or XML file.

iis server tricks
sql server tricks
dedicated server
server hardware
computer help

MS Sql tutorial

Learn SQL Server 2000 Administration in 15 Minutes a Week Series

Part 11: SQL Server 2000 Table Basics (Part One)

Part 10: SQL Server 2000 Restoring and Recovering Databases
Part 9: SQL Server 2000 Creating a Disaster Recovery Plan (Part Two)
Part 8: SQL Server 2000 Creating a Disaster Recovery Plan (Part One)
Part 7: SQL Server 2000 Database Backups
Part 6: SQL Server 2000 Database Creation Topics (Part Two)
Part 5: SQL Server 2000 Database Creation Basics
Part 4: SQL Server 2000 Enterprise Manager Introduction
Part 3: SQL Server 2000 Advanced Installations
Part 2: SQL Server 2000 Basic Installation
Part 1: SQL Server 2000 Administration Fundamentals

Alexander Chigrik

Troubleshooting

Troubleshooting SQL Server Jobs
Troubleshooting SQL Mail
Troubleshooting SQL Server Alerts
Troubleshooting SQL Server 2000 installation issues
Troubleshooting OLAP Problems
Troubleshooting Backup/Restore Problems
Troubleshooting DTS Problems
Troubleshooting Replication Problems
How to Troubleshoot Full-Text Search Problems


Comparisons

The comparison of SQL Server 2000 with MySQL v4.1
A Comparison of Oracle 9i with DB2 v8.1
A Comparison of SQL Server 2000 with Access 2000
A Comparison of SQL Server 2000 With Sybase ASE 12.5
A Comparison of SQL Server 2000 with DB2 v8.1
The comparison of SQL Server 2000 with Oracle 9i


Optimization Tips

Application Design Optimization Tips
SQL Server Bulk Copy Optimization Tips
Analysis Services optimization tips
SQL Server Optimization Tips for Designing Tables
SQL Server Stored Procedures Optimization Tips
Backup/Restore Optimization Tips
DTS Optimization Tips
Replication Optimization Tips
Full-Text Search Optimization Tips
Miscellaneous SQL Server Optimization Tips
Transact-SQL Optimization Tips
SQL Server 6.5 Optimization Tips
Operation System Optimization Tips
SQL Server 2000 Optimization Tips
Database Settings Optimization Tips
SQL Server Settings Optimization Tips
Optimization Tip for SQL 6.5: Using UNION ALL Statement
Index Optimization Tips


Step-by-Step Guides

Setting Up Full-Text Search: A Step-by-step Guide
Setting up Merge Replication: A Step-by-step Guide
Setting up Snapshot Replication: A Step-by-step Guide
Setting up Transactional Replication: A Step-by-step Guide

Sumit Dhingra

SQL “How To’s”

Part 4
Part 3
Part 2
Part 1

Rob Garrison

SqlCredit – Developing a Complete SQL Server OLTP Database Project

Part 24: Performance Testing SQL 2008’s Transparent Data Encryption
Part 23: SQL Server 2008’s Change Data Capture – Tracking the Moving Parts
Part 22: Performance Testing – SQL Server 2008 versus SQL Server 2005
Part 21: Exploring SQL Server’s Index INCLUDEs
Part 20: Exploring SQL 2005’s Ranking Functions – NTILE() and ROW_NUMBER()
Part 19: Exploring SQL 2005’s Ranking Functions – RANK() and DENSE_RANK()
Part 18: Exploring the Performance of SQL 2005’s OUTPUT Clause
Part 17: Exploring SQL 2005’s OUTPUT Clause
Part 16: The Cost of Bloat
Part 15: The Cost of Distribution
Part 14: The Cost of Translation
Part 13: More on Indexed Persisted Computed Columns
Part 12: Exploring Indexed Persisted Computed Columns
Part 11: Change Tracking Using History Records
Part 10: MAC Performance and Updating SqlCredit
Part 9: Message Authentication Codes
Part 8: Comparing Encrypt/DecryptByCert and Encrypt/DecryptByKey
Part 7: Performance Impact of EncryptByCert and DecryptByCert
Part 6: Exploring EncryptByCert and DecryptByCert
Part 5: Adding Card, Vendor, and Purchase, Plus Much Refactoring
Part 4: Schema and Procedure Security
Part 3: Schema/Proc Updates and Automated Unit Testing
Part 2: Creating the Database, Tables, CRUD Procedures
Introduction

Gregory A. Larsen

T-SQL Best Practices

Don’t Use Scalar Value Functions in Column List or WHERE Clauses - 10/29/09
T-SQL Best Practices – Parameter Sniffing - 9/30/09
Parameterized Queries - 8/27/09
T-SQL Best Practices – Part 2 - 7/31/09
T-SQL Best Practices - 6/29/09

T-SQL Programming

Using a Correlated Subquery in a T-SQL Statement - 2/28/05
Using a Subquery in a T-SQL Statement - 1/31/05
Part 5 – Using the CASE Function - 1/7/04
Part 4 – Setting Variables in Calling T-SQL Code While Using sp_executesql - 12/17/03
Part 3 – Processing Sequentially Through a Set of Records - 11/19/03
Part 2 – Building a T-SQL Loop - 11/5/03
Part 1 – Defining Variables, and IF…ELSE logic - 10/15/03

Working with SQL Server Date/Time Variables

Part Four – Date Math and Universal Time – 6/3/03
Part Three – Searching for Particular Date Values and Ranges – 5/21/03
Part Two – Displaying Dates and Times in Different Formats – 5/07/03
Working with SQL Server Date/Time Variables – 4/16/03

MAK

Auditing in SQL Server 2008

SQL Server 2008 : Accessing Audit Details
Configuring Granular Settings for a Database Level Audit
Database Level Auditing with Microsoft SQL Server 2008
Creating a Server Level Audit the Easy Way
Auditing made easy by Microsoft SQL Server 2008

Working with .MDF and .LDF files in SQL Server 2008

Attaching .MDF files and .LDF files of a database with FileGroups
Rebuilding the log when attaching multiple .MDF files
How to handle multiple .MDF files and .LDF files in SQL Server 2008
Attaching a database in Microsoft SQL Server 2008

Database Mirroring in SQL Server 2008

Database Mirroring Using T-SQL
How data is mirrored
Database Mirroring in SQL Server 2008

Microsoft Windows PowerShell and SQL Server 2008 AMO

Script a cube using Windows PowerShell and AMO
Script dimensions using Windows PowerShell and AMO
Detach and attach Analysis Service Database using Windows PowerShell and SQL Server 2008 AMO
Processing an Analysis Service Cube using Windows PowerShell and SQL Server 2008 AMO
Process Dimensions in an Analysis Service database
Restore Analysis Services database using Windows PowerShell and SQL Server 2008 AMO
How to Restore an SSAS database using Windows PowerShell and SQL Server 2008 AMO
Backing up Analysis Service database using Windows PowerShell and SQL Server 2008 AMO
Using Windows PowerShell and AMO to create an SSAS Database
Get SSAS Database Properties with PowerShell and AMO
Discover Analysis Service’s Properties using Windows PowerShell and AMO
Microsoft Windows PowerShell and SQL Server 2008 AMO

Check your SQL Server using Windows PowerShell

Part 8 – Using Windows PowerShell to get SQL Server connection information
Part 7
Part 6
Part 5
Part 4
Part 3
Part 2
Part 1

Microsoft SQL Server 2008 – Change Data Capture


Part 4
Part 3
Part 2
Part I

Data Encryption in SQL Server 2005

Hacking password Encryption
SQL Server 2005 Encryption types

Microsoft Windows PowerShell and SQL Server 2005 SMO

Part 11 – Export output to XML
Part 10 – Using PowerShell Script to Generate SQL Server Scripts for Database and Tables
Part 9 – Generating SQL Server Scripts
Part 8 – Microsoft Windows PowerShell and SQL Server 2005 SMO
Part 7 – How to use PowerShell in conjunction with SMO to display SQL Server Objects
Part 6 – How to use PowerShell and PowerShell script to backup databases
Part 5 – How to use PowerShell and PowerShell script to create databases
Part 4 – Loop through the content of a file
Part 3 – Script Windows Power Shell
Part 2 – Windows PowerShell and Features
Part 1 – Install Windows Power Shell

Case Functions in SQL Server

Part IV
Part III
Part II
Part I

SQL Server 2005 Command Line Tool “SQLCMD”

Part III
Part II
Part I

Data partitioning in SQL Server 2005

Part V
Part IV
Part III
Part II
Part I

SQL Server 2005 – Unattended installation

Part V – Service pack install – Server and Client Components
Part IV – Service pack install – Client Components
Part III – Server and Client Components using .ini File
Part II – Server Components
Part I – Client Components


Alexzander Nepomnjashiy

SQL Server 7.0

Part 2 – Administrative Command Line Utilities
Part 1 – Administrative Command Line Utilities
Administrative Utilities (With Graphic Interface)

OLAP and Data Warehousing

Part 2 – Data Warehouse Solution Architecture
Part 1 – Data Warehouse Solution Architecture
Part 2 – DW Technology Review
Part 1 – DW Technology Review
Data in Jail
The Problem and Solution

Yan Pan

Setting up a Two-NODE SQL Server 2008 Cluster from the Command Prompt

Installing a Two-node SQL Server 2008 Cluster – Advanced option
Setting up a Two-NODE SQL Server 2008 Cluster from the Command Prompt – Integrated Installation
Setting up a Two-node SQL Server 2008 Cluster from the Command Prompt – Preparation

William E. Pearson

Introduction to MSSQL Server Analysis Services Series

Part 88: Introduction to Security in Analysis Services
Part 87: Cube Storage: Planning Partitions from a SQL Server Management Studio Perspective
Part 86: Cube Storage: Planning Partitions (Business Intelligence Development Studio Perspective)
Part 85: Cube Storage: Introduction to Partitions
Part 84: Introduction to Cube Storage
Part 83: Attribute Discretization: Customize Grouping Names
Part 82: Attribute Discretization: Using the “Clusters” Method
Part 81: Attribute Discretization: Using the “Equal Areas” Method
Part 80: Attribute Discretization: Using the Automatic Method
Part 79: Introduction to Attribute Discretization
Part 78: More Exposure to Settings and Properties in Analysis Services Attribute Relationships
Part 77: Attribute Relationships: Settings and Properties
Part 76: Introduction to Attribute Relationships in MSSQL Server Analysis Services
Part 75: Attribute Member Values in Analysis Services
Part 74: MSSQL Analysis Services – Attribute Member Names
Part 73: Attribute Member Keys – Pt 2: Composite Keys
Part 72: Attribute Member Keys – Pt 1: Introduction and Simple Keys
Part 71: Dimension Attributes: Introduction and Overview, Part V
Part 70: Dimension Attributes: Introduction and Overview, Part IV
Part 69: Dimension Attributes: Introduction and Overview, Part III
Part 68: Dimension Attributes: Introduction and Overview, Part II
Part 67: Dimension Attributes: Introduction and Overview, Part I
Part 66: Dimensional Model Components: Dimensions Part II
Part 65: Dimensional Model Components: Dimensions Part I
Part 64: Manage Unknown Members in Analysis Services 2005, Part II
Part 63: Manage Unknown Members in Analysis Services 2005, Part I
Part 62: Alternatively Sorting Attribute Members in Analysis Services 2005
Part 61: Introduction to Linked Objects in Analysis Services 2005
Part 60: Distinct Counts in Analysis Services 2005
Part 59: Positing the Intelligence: Conditional Formatting in the Analysis Services Layer
Part 58: Administration and Optimization: SQL Server Profiler for Analysis Services Queries
Part 57: Mastering Enterprise BI: Time Intelligence Pt. II
Part 56: Mastering Enterprise BI: Time Intelligence Pt. I
Part 55: Design and Documentation: Introducing the Visio 2007 PivotDiagram
Part 54: Actions in Analysis Services 2005: The URL Action
Part 53: Actions in Analysis Services 2005: The Drillthrough Action
Part 52: Mastering Enterprise BI: Introducing Actions in Analysis Services 2005
Part 51: Mastering Enterprise BI: Introduction to Translations
Part 50: Mastering Enterprise BI: Introduction to Perspectives
Part 49: Introduction to the Analysis Services 2005 Query Log
Part 48: Mastering Enterprise BI: Working with Measure Groups
Part 47: Mastering Enterprise BI: Introduction to Key Performance Indicators
Part 46: Mastering Enterprise BI: Extend the Data Source with Named Calculations, Pt. II
Part 45: Mastering Enterprise BI: Extend the Data Source with Named Calculations, Pt. I
Part 44: Process Analysis Services Objects with Integration Services
Part 43: Usage-Based Optimization in Analysis Services 2005
Part 42: Named Sets Revisited
Part 41: Migrating an Analysis Services 2000 Database to Analysis Services 2005
Part 40: Introducing Data Source Views
Part 39: Reporting Options for Analysis Services Cubes: MS Excel 2003 and More …
Part 38: Mastering Enterprise BI: Create Aging “Buckets” in a Cube
Part 37: Mastering Enterprise BI: Relative Time Periods in an Analysis Services Cube, Part II
Part 36: Mastering Enterprise BI: Relative Time Periods in an Analysis Services Cube
Part 35: Process Analysis Services Cubes with DTS
Part 34: Presentation Nuances: CrossTab View – Same Dimension
Part 33: Point-and-Click Cube Schema Simplification
Part 32: Manage Distinct Count with a Virtual Cube
Part 31: Distinct Count Basics: Two Perspectives
Part 30: Semi-Additive Measures and Periodic Balances
Part 29: Performing Incremental Cube Updates – An Introduction
Part 28: Partitioning a Cube in Analysis Services – An Introduction
Part 27: Basic Storage Design
Part 26: Derived Measures vs. Calculated Measures
Part 25: Creating a Dynamic Default Member
Part 24: Another Approach to Local Cube Design and Creation
Part 23: Introduction to Local Cubes
Part 22: Actions in Virtual Cubes
Part 21: Putting Actions to Work in Regular Cubes
Part 20: Reporting Options for Analysis Services Cubes: ProClarity Part II
Part 19: Reporting Options for Analysis Services Cubes: ProClarity Professional, Part I
Part 18: Using Calculated Cells in Analysis Services , Part II
Part 17: Using Calculated Cells in Analysis Services, Part I
Part 16: MSAS Administration and Optimization: Toward More Sophisticated Analysis
Part 15: MSAS Administration and Optimization: Simple Cube Usage Analysis
Part 14: Build a Web Site Traffic Analysis Cube: Part II
Part 13: Build a Web Site Traffic Analysis Cube: Part I
Part 12: Reporting Options for Analysis Services Cubes: Cognos PowerPlay
Part 11: Reporting Options for Analysis Services Cubes: MS FrontPage 2002
Part 10: Reporting Options for Analysis Services Cubes: MS Excel 2002
Part 9: Drilling Through to Details: From Two Perspectives
Part 8: Custom Cubes: Financial Reporting – Part II
Part 7: Custom Cubes: Financial Reporting
Part 6: Exploring Virtual Cubes
Part 5: Working with the Cube Editor
Part 4: Parent-Child Dimensions
Part 3: Handling Time Dimensions
Part 2: Working with Dimensions
Part 1: Creating Our First Cube


MDX Essentials Series

Part 77: The LEVEL_NUMBER Member Property
Part 76: The LEVEL_UNIQUE_NAME Intrinsic Member Property
Part 75: Intrinsic Member Properties: The HIERARCHY_UNIQUE_NAME Property
Part 74: Intrinsic Member Properties: The DIMENSION_UNIQUE_NAME Property
Part 73: Further Combination of BottomCount() with Other MDX Functions
Part 72: Combine BottomCount() with Other MDX Functions to Add Sophistication
Part 71: Basic Set Functions: The BottomCount() Function, Part I
Part 70: Intrinsic Member Properties: The MEMBER_VALUE Property
Part 69: Intrinsic Member Properties: The MEMBER_UNIQUE_NAME Property
Part 68: Intrinsic Member Properties: The MEMBER_NAME Property
Part 67: Intrinsic Member Properties: The MEMBER_KEY Property
Part 66: Intrinsic Member Properties: The MEMBER_CAPTION Property
Part 65: Set Functions: The StripCalculatedMembers() Function
Part 64: Set Functions: The AddCalculatedMembers() Function
Part 63: MDX Numeric Functions: The Min() Function
Part 62: MDX Numeric Functions: The Max() Function
Part 61: Set Functions: The .AllMembers Function
Part 60: MDX Essentials: Set Functions: The MeasureGroupMeasures() Function
Part 59: String Functions: The .Properties Function, Part II
Part 58: String Functions: The .Properties Function
Part 57: Logical Functions: IsGeneration(): Conditional Logic within Filter Expressions
Part 56: MDX Scripting Statements: Introducing the Simple CASE Statement
Part 55: Logical Functions: IsGeneration(): Conditional Logic within Calculations
Part 54: Logical Functions: IsAncestor(): Conditional Logic within Filter Expressions
Part 53: MDX Clauses and Keywords: Use HAVING to Filter an Axis
Part 52: Logical Functions: IsAncestor(): Conditional Logic within Calculations
Part 51: Logical Functions: IsSibling(): Conditional Logic within Filter Expressions
Part 50: Logical Functions: IsSibling(): Conditional Logic within Calculations
Part 49: MDX Operators: The IsLeaf() Operator: Conditional Logic within Filter Expressions
Part 48: MDX Operators: The IsLeaf() Operator: Conditional Logic within Calculations
Part 47: MDX Numeric Functions: The .Ordinal Function
Part 46: Other MDX Entities: Perspectives
Part 45: MDX Operators: The IS Operator
Part 44: MDX Set Functions: The Distinct() Function
Part 43: MDX Set Functions: The ToggleDrillState() Function
Part 42: Set Functions: The DrillUpLevel() Function
Part 41: Set Functions: The DrillDownLevelTop() and DrillDownLevelBottom() Functions
Part 40: MDX Set Functions: DrillDownLevel()
Part 39: MDX Set Functions: The DRILLUPMEMBER() Function
Part 38: Set Functions: The DRILLDOWNMEMBERTOP() and DRILLDOWNMEMBERBOTTOM() Functions
Part 37: Set Functions: The DRILLDOWNMEMBER() Function
Part 36: Drilling Through with MDX: The DRILLTHROUGH Statement
Part 35: String Functions: The .UniqueName Function
Part 34: String Functions: The .Name Function
Part 33: String / Numeric Functions: The CoalesceEmpty() Function
Part 32: Basic Set Functions: The TopCount() Function, Part II
Part 31: Basic Set Functions: The TopCount() Function, Part I
Part 30: Enhancing CROSSJOIN() with Calculated Members
Part 29: Set and String Functions: The GENERATE() Function
Part 28: The CROSSJOIN() Function: Breaking Bottlenecks
Part 27: String / Numeric Functions: More on the IIF() Function
Part 26: String / Numeric Functions: Introducing the IIF() Function
Part 25: Logical Functions: The IsEmpty() Function
Part 24: Basic Set Functions: The EXTRACT() Function
Part 23: Numeric Functions: Introduction to the AVG() Function
Part 22: Basic Member Functions: The .Item() Function
Part 21: Basic Set Functions: Subset Functions: The Subset() Function
Part 20: Basic Set Functions: Subset Functions: The Tail() Function
Part 19: Basic Set Functions: Subset Functions: The Head() Function
Part 18: Basic Set Functions: The CrossJoin() Function
Part 17: Basic Numeric Functions: The Count() Function
Part 16: Basic Set Functions: The Filter() Function
Part 15: Basic Set Functions: The EXCEPT() Function
Part 14: Basic Set Functions: The Intersect() Function
Part 13: Basic Set Functions: The Union() Function
Part 12: Basic Set Functions: The Order() Function
Part 11: MDX Time Series Functions, Part III: The LastPeriods() and ParallelPeriod() Functions
Part 10: MDX Time Series Functions, Part II: The OpeningPeriod () and ClosingPeriod() Functions
Part 9: MDX Time Series Functions, Part I: PeriodsToDate() and Kindred Functions
Part 8: MDX Member Functions: “Relative” Member Functions
Part 7: MDX Member Functions: The Cousin () Function
Part 6: MDX Member Functions: More “Family” Functions
Part 5: MDX Member Functions: The “Family” Functions
Part 4: MDX Members: Introducing Members and Member
Part 3: MDX Operators: The Basics
Part 2: Structure of the MDX Data Model
Part 1: MDX at First Glance: Introduction to MDX Essentials


MDX in Analysis Services

Part 20: Enhancing CROSSJOIN() with Calculated Members
Part 19: Mastering Time: Moving Averages – Another Approach
Part 18: Mastering Time: Introduction to Moving Averages
Part 17: Create a Cube-Based Hierarchical Picklist
Part 16: Mastering Time: Period – to – Date Aggregations
Part 15: Mastering Time: Change across Periods
Part 14: Introducing DISTINCT COUNT
Part 13: Named Sets in MDX: An Introduction
Part 12: Optimizing MDX: Caching and Other Considerations
Part 11: Optimizing MDX: More on Location, and the Importance of Arrangement
Part 10: Optimizing MDX: Control Location of Processing
Part 9: Calculated Members: Leveraging Member Properties
Part 8: Calculated Members: Further Considerations and Perspectives
Part 7: Calculated Members: Introduction
Part 6: Using Sets in MDX Queries
Part 5: Measuring Change over Time
Part 4: Retrieve Data from Multiple Cubes
Part 3: Intermediate Concepts – Part 2
Part 2: Intermediate Concepts – Part 1
Part 1: MDX Concepts and Navigation


MSSQL Server Reporting Services

Part 71: Introducing the Tablix Data Region: Basic Grouping Concepts
Part 70: Introducing the Tablix Data Region in Reporting Services 2008
Part 69: 100% Stacked Column Chart for Analysis Services Data
Part 68: XY (Scatter) Chart for Analysis Services Data
Part 67: Simple Doughnut Chart for Analysis Services Data
Part 66: Exploded Pie Chart for Analysis Services Data
Part 65: Stacked Bar Chart for Analysis Services Data
Part 64: Line Chart for Analysis Services Data
Part 63: Stacked Column Chart for Analysis Services Data
Part 62: A More Advanced Pie Chart for Analysis Services Data
Part 61: Simple Pie Chart for Analysis Services Data
Part 60: Simple Bar Chart for Analysis Services Data
Part 59: Simple Column Chart for Analysis Services Data
Part 58: Introducing Reporting Services Charts for Analysis Services
Part 57: Mastering OLAP Reports: Parameterized Grouping
Part 56: Mastering OLAP Reports: Parameterizing Number of “Top” Items with the MDX TopCount() Function, Part II
Part 55: Mastering OLAP Reports: Parameterizing Number of “Top” Items with the MDX TopCount() Function, Part I
Part 54: Mastering OLAP Reports: Parameterizing Number of “Look Back” Periods with the MDX LastPeriods() Function, Part II
Part 53: Mastering OLAP Reports: Parameterizing Number of “Look Back” Periods with the MDX LastPeriods() Function, Part I
Part 52: Support Parameterization from Analysis Services – Parameter Defaults
Part 51: Parameterization from Analysis Services – Cascading Picklists
Part 50: Support Parameterization from Analysis Services
Part 49: Parameter Support Objects, Pt II: Support OLAP Parameter Defaults with Datasets
Part 48: Reporting Services: Customize Automatically Created Parameter Support Objects
Part 47: Snapshot Reports II: SQL Server Management Studio Perspective
Part 46: Snapshot Reports I: Report Manager Perspective
Part 45: Report Execution Caching II: Report Manager Perspective
Part 44: Black Belt Administration: Report Execution Caching I: SQL Server Management Studio Perspective
Part 43: Black Belt Administration: Caching Options: Report Session Caching
Part 42: Black Belt Administration: Reporting Services Configuration Manager
Part 41: Intelligent Layering: Leverage Conditional Formatting Logic from Analysis Services
Part 40: Black Belt Administration: Performance Dashboard for Microsoft SQL Server, Part II
Part 39: Black Belt Administration: Performance Dashboard for Microsoft SQL Server, Part I
Part 38: Mastering OLAP Reports: Extend Reporting Services with Custom Code
Part 37: Mastering OLAP Reports: Parameters for Analysis Services Reporting, Pt. II
Part 36: Black Belt Components: Support Simple Navigation with a Document Map
Part 35: Mastering OLAP Reports: Parameters for Analysis Services Reporting, Pt. I
Part 34: Black Belt Components: Interactive Sorts within a Matrix Data Region
Part 33: BlackBelt Authoring: Conditional Drillthrough to Multiple Reports
Part 32: Mastering OLAP Reporting: Prototype KPIs in Reporting Services
Part 31: BlackBelt Administration: Linked Reports in SQL Server Management Studio
Part 30: BlackBelt Administration: Linked Reports in Report Manager
Part 29: Mastering OLAP Reporting: Reporting with Analysis Services KPIs
Part 28: Report Builder: Creating a Report Model
Part 27: Mastering OLAP Reporting: Meet Business Needs with Matrix Dynamics, Part II
Part 26: Mastering OLAP Reporting: Meet Business Needs with Matrix Dynamics, Part 1
Part 25: Mastering OLAP Reporting: Display a Dataset Field in a Report Page Header
Part 24: Interactive Sorting Within Reporting Services
Part 23: Mastering OLAP Reporting: Multiple Value Selection in a Parameter Picklist
Part 22: Mastering OLAP Reporting: Drilling Through Using MDX
Part 21: Mastering OLAP Reporting: Relationally-Based Picklists for OLAP Reporting
Part 20: Mastering OLAP Reporting: Extending Conditional Formatting: SWITCH and Drilldown Defaults
Part 19: Mastering OLAP Reporting: Percent of Total – Chart Presentation Nuances
Part 18: Mastering OLAP Reporting: Percent of Total – Two Perspectives
Part 17: Mastering OLAP Reporting: Ad Hoc TopCount and BottomCount Parameters
Part 16: Black Belt Components: Ad Hoc Sorting with Parameters
Part 15: Black Belt Administration: “Governor” Capabilities: Report Execution Timeout
Part 14: Black Belt Administration: Execution Log Performance and Audit Reports
Part 13: Black Belt Administration: Prepare the Execution Log for Reporting
Part 12: Black Belt Components: Ad Hoc Conditional Formatting for OLAP Reports
Part 11: Black Belt Components: Manage Nulls in OLAP Reports
Part 10: Reporting Services Basics: Create a Reusable Template Report
Part 9: Master Chart Reports: Track Exchange Rates in a Line Chart
Part 8: Master Chart Reports: Pie Charts in Reporting Services
Part 7: Mastering OLAP Reporting: Cascading Prompts
Part 6: Managing Reporting Services: Data-driven Subscriptions, and External Data Sources for Subscriber Data
Part 5: Managing Reporting Services: Report Execution and Standard Subscriptions
Part 4: Managing Reporting Services: Data Connections and Uploads
Part 3: The Authoring Phase: Overview Part II
Part 2: The Authoring Phase: Overview Part I
Part 1: A New Paradigm for Enterprise Reporting


Reference Articles and Setup Procedures

Marcin Policht

SQL Server 2008 and 2008 R2 Integration Services

Part 21 – Querying Active Directory using SSIS Script Task
Part 20 – Introduction to SSIS Script Task
Part 19 – Using SSIS Logging to Resolve Runtime Errors and Performance Issues
Part 18 – SSIS Events and Handlers
Part 17 – Protecting the Integrity of SSIS Packages Using Digital Signatures
Part 16 – Securing the Content of SSIS Packages
Part 15 – Executing SSIS Packages Using DTExec
Part 14 – Implementing SSIS Package Configurations
Part 13 – How to Implement Package Variables in SQL Server 2008
Part 12 – The Advantages of Using Variables in SSIS Package Configurations
Part 11 – Characteristics and Common Usage Scenarios of SSIS Variables
Part 10 – Exploring Derived Column Transformation in SQL Server Integration Services
Part 9 – SQL Server Integration Services 2008: Importing Excel Data Using Derived Column Transformation
Part 8 – Importing Excel data into SSIS 2008 using Data Conversion Transformation
Part 7 – SSIS 2008 Import and Export Wizard and Excel-based Data
Part 6 – New and Improved Functionality in SSIS 2008
Part 5 – Introduction to Control Flow and Data Flow of SSIS 2008
Part 4 – SQL Server – Introduction to Business Intelligence Development Studio
Part 3 – SSIS Packages – the Simplest Way to Create Them
Part 2 – Installing and configuring SQL Server 2008 and 2008 R2 Integration Services
Part 1 – Introducing SQL Server 2008 and 2008 R2 Integration Services

SQL Server 2005 Express Edition

Part 61 – Full Text Index Queries using CONTAINS and CONTAINSTABLE
Part 60 – Data searches that leverage existing indexes
Part 59 – Full Text Index Management in SQL Server 2005 Express Edition
Part 58 – Full Text Index Implementation in SQL Server 2005 Express Edition
Part 57 – Full Text Catalog Configuration in SQL Server 2005 Express Edition
Part 56 – Troubleshooting Reporting Services Performance Issues
Part 55 – Troubleshooting Reporting Services
Part 54 – Securing Reporting Services
Part 53 – Securing Reporting Services Data Sources
Part 52 – Securing Data using Linked and Parameterized Reports
Part 51 – Reporting Services System and Item Security
Part 50 – Reporting Services Content Management
Part 49 – Introduction to SQL Server 2005 Express Edition Reporting Services Content Management
Part 48 – Taking Advantage of Sample Reporting Services Reports
Part 47 – The Basic Functionality of Report Designer
Part 46 – IBasic Server Reporting Functionality in SQL Server 2005 Express Edition
Part 45 – Initial Configuration of Reporting Services on SQL Server 2005 Express Edition
Part 44 – Installing Reporting Services on SQL Server 2005 Express Edition Service Broker Poison Message Handling
Part 43 – Service Broker Poison Message Handling
Part 42 – Handling Service Broker Errors
Part 41 – Service Broker Transactional Support in SQL Server 2005 Express Edition
Part 40 – Security Context of Service Broker Internal Activation
Part 39 – Service Broker Activation in SQL Server 2005 Express Edition
Part 38 – Configuring Anonymous Dialog Security in SQL Server 2005 Express Service Broker Conversation
Part 37 – Conducting Service Broker Conversation Using Full Dialog Security in SQL Server 2005 Express Service
Part 36 – Configuring Full Dialog Security in SQL Server 2005 Express Service Broker Conversation
Part 35 – Configuring Transport Encryption in SQL Server 2005 Express Service Broker Conversation
Part 34 – Establishing Distributed SQL Server Express’ Service Broker Conversations Using Certificate-based Authentication
Part 33 – Configuring Certificate-based Authentication in SQL Server Express’ Distributed Service Broker Environment
Part 32 – Distributed Service Broker Environment – Conducting Dialogs
Part 31 – Distributed Service Broker Environment – Routing
Part 30 – Distributed Service Broker Environment – Endpoints
Part 29 – Implementing Service Broker Conversation
Part 28 – Implementing Service Broker Conversation
Part 27 – Implementing Basic Service Broker Objects
Part 26 – Introduction to Service Broker
Part 25 – Implementing Upgrade of SQL Server 2005 Express Edition
Part 24 – Planning Upgrade of SQL Server 2005 Express Edition (MSDE)
Part 23 – Manual Upgrade from Microsoft SQL Server Desktop Engine (MSDE)
Part 22 – Upgrading from Microsoft SQL Server Desktop Engine (MSDE)
Part 21 – Using Replication Management Objects
Part 20 – Authenticating Merge Web Synchronization
Part 19 – Authenticating Merge Web Synchronization
Part 18 – Merge Web Synchronization Setup
Part 17 – Merge Web Synchronization
Part 16 – Transactional and Merge Replication
Part 15 – Snapshot Replication
Part 14 – Replication Support
Part 13 – ClickOnce Deployment and Security
Part 12 – ClickOnce Deployment and Updates
Part 10 – Working with User Instances using Visual Basic 2005 Express Edition
Part 11 – ClickOnce Deployment and Maintenance Options
Part 10 – Working with User Instances using Visual Basic 2005 Express Edition
Part 9 – Managing User Instances
Part 8 – XCopy Deployment
Part 7 – Post Installation Configuration Tasks (Management)
Part 6 – Post Installation Configuration Tasks (Encryption)
Part 5 – Post-Installation Network Configuration Tasks
Part 4 – Surface Area Configration
Part 3 – Installation
Part 2
Part 1

SQL Server 2005 Integration Services

Part 50 – Templates, Data Sources, and Data Source Views
Part 49 – Maintenance Plan Tasks
Part 48 – Maintenance Plan Designer tasks
Part 47 – Maintenance Plan Tasks
Part 46 – Maintenance Plan Tasks
Part 45 – Bulk Insert Task and Format Files
Part 44 – Bulk Insert Task
Part 43 – XML Task continued
Part 42 – XML Task continued
Part 41 – XML Task
Part 40 – Web Service Task
Part 39 – Send Mail Task
Part 38 – Pivot Transformation
Part 37 – Derived Column Transformation
Part 36 – Term Extraction and Term Lookup
Part 35 – Fuzzy Grouping
Part 34 – Fuzzy Lookup
Part 33 – Lookup Transformation
Part 32 – Message Queue Task
Part 31 – Performance
Part 30 – Performance
Part 29 – Digital Signing of Packages
Part 28 – Security
Part 27 – Working with SQL Server 2000 DTS Packages
Part 26 – Checkpoints
Part 25 – Transaction Support
Part 24 – Package Execution
Part 23 – Packages Deployment
Part 22 – Package Management
Part 21 – Import and Export Wizard
Part 20 – Migration Tasks
Part 19 – Database Management Tasks
Part 18 – Destination Script Component
Part 17 – Script Transformation Component
Part 16 – Data Flow Script Component
Part 15 – Debugging Script Component
Part 14 – Debugging Data Flow
Part 13 – Debugging
Part 12 – Logging
Part 11 – SSIS Events and Event Handlers
Part 10 – WMI Event Task
Part 9 – WMI Data Reader
Part 8 – Foreach Loop Container
Part 7 – Foreach Loop Container
Part 6 – Foreach Loop Container
Part 5 – Foreach ADO
Loop Containers
Part 4 – Variables
Part 3 – SSIS Features
Part 2 – SSIS Features
Part 1 – SSIS Features


SQL Server 2005


SQL Server 2005 Security


SQL Server 2005 High Availability and Scalability Enhancements


SQL Server 2000 Security


SQL Server 2000 DTS


XML and SQL 2000


Scalability and high availability of Microsoft SQL Server 2000


Automating SQL Server Management with WMI

Don Schlichting

Storing Images and BLOB files in SQL Server

What’s new in SQL 2008

Microsoft SQL 2005 Maintenance Wizard

MS SQL Joins


Executing SQL Stored Procedures from inside a Web Application


Controlling Transactions and Locks


Full Text Search on SQL 2000


Linked Severs on MS SQL

Bruce Szabo

SQL Maintenance Plans and Backed Up Databases

Steven Warren

High Availability with SQL Server 2000 Failover and DoubleTake

Database Mirroring in SQL Server 2008

By Muthusamy Anantha Kumar aka The MAK

What is Database Mirroring?

Database mirroring is the feature in SQL Server 2005 and SQL Server 2008 that provides a high availability solution for Databases. This feature can be enabled and used only on a database with Full recovery models. The database can be mirrored from one SQL Server instance to another SQL Server instance. The source instance is called Principal server; the target instance is called Mirrored server. We could have one more server called Witness server–we will talk about that in later part of this article series.

How does database mirroring work?

The principle server sends the active transaction log record to the mirrored server. The mirrored server applies the transaction log record one by one in sequence.

Modes of Database Mirroring

Database mirroring can be configured in two different modes, High-Safety mode also known as synchronous mode and High-Performance mode also known as asynchronously. The term synchronous and asynchronous says it all.

In the synchronous mode, the principal server sends the transaction and waits until the transaction is committed on the mirrored server. Then the transaction is committed on the principal server.

In Asynchronous mode, the principal server sends the transaction to the mirrored server and does not wait for the transaction on the mirrored server to commit.

We will discuss transaction safety in detail in a future installment of this series.

Now let’s setup database mirroring between the SQL Server instance PowerPC\SQL2008 [our principal server] and PowerPC\SQL2k8 [our mirrored server].

What are the Pre-Requisites of database mirroring?

The following are the pre-requisites for database mirroring.

  • Edition of SQL Server should be Standard, Enterprise or Developer edition
  • Principal Database involved in database mirroring should be in full recovery mode
  • Before configuring database mirroring, take a full backup, transactional log backup on the principal server and restored it on the mirrored server with NORECOVERY option.

Now let’s create a database DB1 on the principal server, PowerPC\SQL2008, using the following transact SQL statement. In this part of article series, we are going to discuss database mirroring with synchronous mode and with no witness server.

USE [master]
GO

/****** Object:  Database [DB1]    Script Date: 06/20/2009 21:10:33 ******/
IF  EXISTS (SELECT name FROM sys.databases WHERE name = N'DB1')
DROP DATABASE [DB1]
GO

USE [master]
GO

/****** Object:  Database [DB1]    Script Date: 06/20/2009 21:10:13 ******/
CREATE DATABASE [DB1] ON  PRIMARY
( NAME = N'DB1', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008\MSSQL\DATA\DB1.mdf' , \
	SIZE = 1280KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
 LOG ON
( NAME = N'DB1_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008\MSSQL\DATA\DB1_log.LDF' ,
	SIZE = 504KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO

Now let’s create a database DB1 on the mirrored server, PowerPC\SQL2K8, using the following transact SQL statement.

USE [master]
GO

/****** Object:  Database [DB1]    Script Date: 06/20/2009 21:10:33 ******/
IF  EXISTS (SELECT name FROM sys.databases WHERE name = N'DB1')
DROP DATABASE [DB1]
GO

USE [master]
GO

/****** Object:  Database [DB1]    Script Date: 06/20/2009 21:10:13 ******/
CREATE DATABASE [DB1] ON  PRIMARY
( NAME = N'DB1', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.SQL2K8\MSSQL\DATA\DB1.mdf' ,
	SIZE = 1280KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
 LOG ON
( NAME = N'DB1_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.SQL2K8\MSSQL\DATA\DB1_log.LDF' ,
	SIZE = 504KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO

If the target server does not have the database with same name, you will get the following error when configuring database mirroring. [Refer Fig 1.0]

Database does not exist on the mirror server instance
Fig 1.0

Note: Instead of creating the DB1 database on the mirrored server, you could restore the database backup and tranlog backup using the with replace option to create and restore at the same time.

Now let’s backup the database and transaction on the principal server using the following transact SQL statement.

use master
go
Backup database DB1 to disk ='C:\Backups\DB1.Bak' with init
go
Backup log DB1 to disk ='C:\Backups\DB1.trn' with init
go

Restore the database on the target server using the following transact SQL statement.

use master
go
restore database DB1 from disk ='C:\Backups\DB1.Bak' with norecovery,
replace,
move 'DB1' to 'C:\Program Files\Microsoft SQL Server\MSSQL10.SQL2K8\MSSQL\DATA\DB1.mdf',
move 'DB1_log' to 'C:\Program Files\Microsoft SQL Server\MSSQL10.SQL2K8\MSSQL\DATA\DB1_log.ldf'
go
restore log DB1 from disk ='C:\Backups\DB1.trn' with norecovery, replace,
move 'DB1' to 'C:\Program Files\Microsoft SQL Server\MSSQL10.SQL2K8\MSSQL\DATA\DB1.mdf',
move 'DB1_log' to 'C:\Program Files\Microsoft SQL Server\MSSQL10.SQL2K8\MSSQL\DATA\DB1_log.ldf'
go

On the target server, if the database is not in restore mode you will get the following error. [Refer Fig 1.2]

Alter failed for database
Fig 1.2

Configure the database DB1 on the principal server for database mirroring. Using SQL Server management studio, expand the databases and click on the Database DB1. Right click on the database DB1 and select properties. In the properties window select the “Mirroring” option as shown below. [Refer Fig 1.3]

In the properties window select the
Fig 1.3

Now click on the “Configure Security” button and you will see the following screen. Since we are not going to setup the witness server, select the option “No” and click next. [Refer Fig 1.4]

click on the
Fig 1.4

Select the default port and the endpoint name chosen by the SQL server management studio and click Next. [Refer Fig 1.5] If you are choosing some other port, then make sure that port is open and available.

configure database mirroring security wizard
Fig 1.5

Now select the mirrored server name, click on the “Connect” button and make sure you can connect to the mirrored server. Select the default port and the endpoint name chosen by the SQL server management studio and click Next. [Refer Fig 1.6] If you are choosing some other port, then make sure that port is open and available.

configure database mirroring security wizard
Fig 1.6

Type the appropriate service account you want to use for the database mirroring. [Refer Fig 1.7]

Type the appropriate service account you want to use for the database mirroring
Fig 1.7

Double check the summary details and click finish. This will configure database mirroring. [Refer Fig 1.8, 1.9, 1.10]

Double check the summary details and click finish
Fig 1.8

Configuring Endpoints - in progress\
Fig 1.9

Configuring Endp;oints - success\
Fig 1.10

On the next screen, click on the button “Start Mirroring”. [Refer Fig 1.11]

click on the button
Fig 1.11

On the next screen, click on the “Yes” button. [Refer Fig 1.12]

On the next screen, click on the \
Fig 1.12

The following screen shows that database mirroring is configured and running. [Refer Figure 1.13]

database mirroring is configured and running
Fig 1.13

Click OK and refresh the databases. You can see the caption of the DB1 database has changed in both principal and mirrored server. [Refer Fig 1.14]

Click OK and refresh the databases\
Fig 1.14

Conclusion:

Part I of this series explained the basics of the Database mirroring feature in SQL Server 2008. It also illustrated a step-by-step process on how to create database mirroring. Part 2 of this series we will discuss how to add data on the principal server and how data is mirrored to the mirrored server.

Attaching .MDF files and .LDF files of a database with FileGroups

Part 1 of this series illustrated the use of the “Create Database” statement “For Attach” and “for ATTACH_REBUILD_LOG” for a single .MDF file and single .LDF file. Part 2 illustrated how to handle multiple .MDF files and .LDF files when attaching it. Part 3 illustrated how to rebuild the log when attaching multiple .MDF files. This installment illustrates how to attach .MDF files and .LDF files of a database with FileGroups.

Let’s assume that we have the database, MyDB2, with one .MDF file on the primary file group and two .NDF files on the secondary file group and one .LDF file. Execute the following transact SQL statement to create the database MyDB2.

USE [master]
GO

/****** Object:  Database [MyDB2]
	Script Date: 08/11/2009 12:07:59 ******/
IF  EXISTS (SELECT name FROM sys.databases WHERE name = N'MyDB2')
DROP DATABASE [MyDB2]
GO

CREATE DATABASE MyDB2
ON PRIMARY
  ( NAME='MyDB2_Primary',
    FILENAME=
       'c:\data\MyDB2_Prm.mdf',
    SIZE=2MB,
    MAXSIZE=10MB,
    FILEGROWTH=1MB),
FILEGROUP MyDB2_FG1
  ( NAME = 'MyDB2_FG1_Dat1',
    FILENAME =
       'c:\data\MyDB2_FG1_1.ndf',
    SIZE = 1MB,
    MAXSIZE=10MB,
    FILEGROWTH=1MB),
  ( NAME = 'MyDB2_FG1_Dat2',
    FILENAME =
       'c:\data\MyDB2_FG1_2.ndf',
    SIZE = 1MB,
    MAXSIZE=10MB,
    FILEGROWTH=1MB)
LOG ON
  ( NAME='MyDB2_log',
    FILENAME =
       'c:\data\MyDB2.ldf',
    SIZE=1MB,
    MAXSIZE=10MB,
    FILEGROWTH=1MB);
GO

Now let’s detach the MyDB2 database using the sp_deatch_db system stored procedure and attach it using the sp_attach_db system stored procedure as shown below. Execute the following Transact SQL statement.

use master
go
sp_detach_db [MyDB2]
go
sp_attach_db 'MyDB2','c:\data\MyDB2_Prm.mdf',
'c:\data\MyDB2_FG1_1.ndf',
'c:\data\MyDB2_FG1_2.ndf',
'c:\data\MyDB2.ldf'
go

You could attach the same database files using the “Create database” command with the “For Attach” clause as shown below.

use master
go
sp_detach_db [MyDB2]
go

CREATE DATABASE MyDB2
ON PRIMARY
(FILENAME =       'c:\data\MyDB2_Prm.mdf'),
(FILENAME =       'c:\data\MyDB2_FG1_1.ndf'),
(FILENAME =       'c:\data\MyDB2_FG1_2.ndf'),
(FILENAME =       'c:\data\MyDB2.ldf')
for attach
go

In Figure 1.0, you see that the file group, FILEGROUP MyDB2_FG1, is still intact after attaching it using the sp_attach_db system stored procedure and using the “Create Database” with “for attach” clause.

FILEGROUP MyDB2_FG1, is still intact
Fig 1.0

Now let us detach the “MyDB2″ database using the sp_detach_db system stored procedure, delete the .LDF file and try to rebuild the log using sp_attach_db and using “Create Database” with the “for ATTACH_REBUILD_LOG ” clause.

use master
go
sp_detach_db 'MyDB2'
go
exec master..xp_cmdshell 'del c:\data\MyDB2.ldf'
go

Note: I am using xp_cmdshell to delete the .ldf file. You will get the following error, if xp_cmdshell is not enabled.

Error:

Msg 15281, Level 16, State 1, Procedure xp_cmdshell, Line 1
SQL Server blocked access to procedure 'sys.xp_cmdshell' of
component 'xp_cmdshell' because this component is turned off
as part of the security configuration for this server. A system
administrator can enable the use of 'xp_cmdshell' by using
sp_configure. For more information about enabling 'xp_cmdshell',
see "Surface Area Configuration" in SQL Server Books Online.

You can enable xp_cmdshell using the following Transact SQL Statement.

use master
go
sp_configure 'show advanced options',1
go
reconfigure with override
go
sp_configure 'xp_cmdshell',1
go
reconfigure with override
go

Alternatively, you could delete the .ldf file using the Windows Explorer “Del” command at the MS-DOS command prompt.

Now let’s attach the .MDF file and .NDF files using the sp_attach_db system stored procedure. Execute the following command.

sp_attach_db 'MyDB2',
'c:\data\MyDB2_Prm.mdf','c:\data\MyDB2_FG1_1.ndf'
,'c:\data\MyDB2_FG1_2.ndf'

Result:

File activation failure. The physical file name "c:\data\MyDB2.ldf" may be incorrect.
New log file 'c:\data\MyDB2_log.LDF' was created.

Note: When the log file was recreated, SQL Server automatically suffixed “_log” to the log file name.

You can attach the same database .MDF file only using “Create database” command with

use master
go
sp_detach_db 'MyDB2'
go
exec master..xp_cmdshell 'del c:\data\MyDB2_log.LDF'
go

CREATE DATABASE MyDB2
ON PRIMARY
(FILENAME =       'c:\data\MyDB2_Prm.mdf'),
(FILENAME =       'c:\data\MyDB2_FG1_1.ndf'),
(FILENAME =       'c:\data\MyDB2_FG1_2.ndf')
for ATTACH_REBUILD_LOG
go

Result:

File activation failure. The physical file name "c:\data\MyDB2_log.LDF" may be incorrect.
New log file 'c:\data\MyDB2_log.LDF' was created.

Keep in mind that when attaching .MDF files and .LDF files from previous versions such as SQL Server 2000 and SQL Server 2005, SQL server automatically upgrades the database to SQL Server 2008.

However, you cannot attach SQL Server 2008′s .MDF file and .LDF files to SQL Server 2005 and SQL Server 2000 servers.

Warning: Please try all of the above-illustrated commands from all installments of this series on your test or QA servers first. Do not try to execute system stored procedures like sp_detach_db and xp_cmdshell on a production machine without understanding the outcome.

Conclusion

Part 1 of this article series illustrated the use of the “Create Database” statement “For Attach” and “for ATTACH_REBUILD_LOG” for a single .MDF file and single .LDF file. Part 2 illustrated how to handle multiple .MDF files and .LDF files when attaching it. Part 3 illustrated how to rebuild the log when attaching multiple .MDF files. This installment illustrated how to attach .MDF files and .LDF files of a database with FileGroups.

ms sql Setting Up Snapshot Replication: A Step-by-step Guide

Setting Up Snapshot Replication: A Step-by-step Guide

By Alexander Chigrik


Introduction
General concepts
Replication topologies
Replication types
Replication agents
Checking necessary conditions
Step by step example
Backup and restore strategies
Literature


Introduction

In this article, I want to tell you about some general Microsoft SQL Server replication topics: replication topologies, replication types, and replication agents. I will also discuss Snapshot replication: how to check necessary conditions for this type of replication and how to backup and restore the databases in this replication scenario. To illustrated these concepts, I’ll also be providing a step by step guide to setting up a Snapshot replication process.

Because it’s only a test example, I used only one server to replicate data: Publisher, Subscriber and Distributor databases resided on the same machine.


General concepts

Replication is the process whereby data is copied between databases on the same server or different servers connected by LANs, WANs, or the Internet.

Microsoft SQL Server replication uses a publisher, distributor and subscriber metaphor.

Publisher is the server or database that sends its data to another server or database.

Subscriber is the server or database that receives data from another server or database.

Distributor is the server that manages the flow of data through the replication system. This server contains the distribution database.

Publisher contains publication/publications. Publication is a collection of one or more articles that is sent to a subscriber server or database.

Article is the basic unit of replication and can be a table or a subset of a table.

Subscription is the group of data that a server or database will receive.

There are push and pull subscriptions. Push subscription is subscription when the publishing server will periodically push transactions out to the subscribing server or database.

Pull subscription is subscription when the subscribing server will periodically connect to the distribution database and pull information.

The Distribution database is a system database, which is stored on the Distributor and does not contain any user tables. This database is used to store snapshot jobs and all transactions waiting to be distributed to Subscribers.


Replication Topologies

Microsoft SQL Server supports the following replication topologies:

  • Central publisher
  • Central subscriber
  • Central publisher with remote distributor
  • Central distributor
  • Publishing subscriber

Central publisher

This is one of the most used replication topologies. In this scenario, one server is configured as Publisher and Distributor and another server/servers is/are configured as Subscriber/Subscribers.

Central subscriber

This is a common topology in data warehousing. Many servers or databases replicate their data to a single central server in one or more databases.

Central publisher with remote distributor

In this topology, the distribution database resides on another server than publisher. This topology is used for performance reasons when the level of replication activity increases or the server or network resources become constrained. It reduces Publisher loading, but it increases overall network traffic.

This topology requires separate Microsoft SQL Server installations, one for the Publisher and one for the Distributor.

Central distributor

In this topology, several publishers use only one distributor, which resides on another server than publishers. This is one of the most rarely used replication topologies because it has only single point of failure (on the single server with central distributor), and if distributor’s server will fail, the entire replication scenario will be destroyed.

Publishing subscriber

This is a dual role topology. In this topology, two servers publish the same data. One publishing server sends data to the subscriber, and then this subscriber publishes data to any number of other subscribers. This is useful when a Publisher must send data to Subscribers over a slow or expensive communications link.


Replication Types

Microsoft SQL Server 7.0/2000 supports the following replication types:

  • Snapshot
  • Transactional
  • Merge

Snapshot replication is the simplest type of replication. With this kind of replication, all replicated data (replica) will be copied from the Publisher database to the Subscriber’s/Subscribers’ database(s) on a periodic basis. Snapshot replication is best used as a method for replicating data that changes infrequently and when the size of replicated data is not very large.

With Transactional replication, SQL Server captures all changes that were made in an article and stores INSERT, UPDATE, and DELETE statements in the distribution database. These changes are then sent to subscribers from the distribution database and applied in the same order. Transactional replication is best used when the replicated data changes frequently or when the size of replicated data is not small and is not necessary to support autonomous changes of the replicated data on the Publisher and on the Subscriber.

Merge replication is the most difficult replication type. It makes possible autonomous changes to replicated data on the Publisher and on the Subscriber. With Merge replication, SQL Server captures all incremental data changes in the source and in the target databases, and reconciles conflicts according to rules you configure or using a custom resolver you create. Merge replication is best used when you want to support autonomous changes of the replicated data on the Publisher and on the Subscriber.


Replication Agents

Microsoft SQL Server 7.0/2000 supports the following replication agents:

  • Snapshot Agent
  • Log Reader Agent
  • Distribution Agent
  • Merge Agent

The Snapshot Agent is a replication agent that make snapshot files, stores the snapshot on the Distributor, and records information about the synchronization status in the distribution database. The Snapshot Agent is used in all replication types (Snapshot, Transactional, and Merge replications) and can be administered by using SQL Server Enterprise Manager.

The Log Reader Agent is a replication agent that moves transactions marked for replication from the transaction log on the Publisher to the distribution database. This replication agent is not used in Snapshot replication.

The Distribution Agent is a replication agent that moves the snapshot jobs from the distribution database to Subscribers, and moves all transactions waiting to be distributed to Subscribers. The Distribution Agent is used in Snapshot and Transactional replications and can be administered by using SQL Server Enterprise Manager.

The Merge Agent is a replication agent that applies initial snapshot jobs from the publication database tables to Subscribers, and merges incremental data changes that have occurred since the initial snapshot was created. The Merge Agent is used only in Merge replication.


Checking Necessary Conditions

Check the following before setting up Snapshot replication:

1. The Localsystem account has no access to shares on the network as it isn’t an authenticated network account.

So, if you want to set up replication you must change the account the MSSQLServer and SQLServerAgent services runs under to a account with the Windows NT/Windows 2000 administrator’s rights. If your Microsoft SQL Server runs on Windows NT or Windows 2000, you can create a Windows NT/Windows 2000 account and include it in the local Administrators group and the Domain Users group, and set Log in as a service permission for this account.

Windows 9x does not support Windows NT services, so if your Microsoft SQL Server runs on Windows 9x, you do not need to create an SQL account.

2. Only members of the sysadmin server role can set up and configure replication, so if you do not have these rights, you cannot set up replication.

3. Don’t forget to start the SQLServerAgent service (and the MSSQLServer service, of course).

4. You should allocate adequate disk space in the snapshot folder.

5. You should allocate adequate disk space for the distribution database.

6. You should ensure that the server being replicated to is defined as a remote server.


Step-by-step Example

In this example, I will use only one server to replicate data: Publisher, Subscriber and Distributor databases will be residing on the same machine.

I will use Snapshot replication with push subscription. To set up Snapshot replication, you can use GUI interface (from the SQL Server Enterprise Manager), or you can run SQL Server system stored procedures. The first way is easier and more understandable, so I will use it.

First of all, you should register the remote server to be replicated. Because I use only one server to replicate data, I don’t need to make this step. Figure 1 shows the remote server name (the same as the local server name, in this case).


Figure 1.

In this example, I will replicate data from the pubs database into pubs_copy database.


Figure 2.

Select Tools => Replication => Configure Publishing, Subscribers, and Distribution… as shown in Figure 3.


Figure 3.

This will launch the Configure Publishing and Distribution Wizard, as shown in Figure 4.


Figure 4.

Select the Next button to create the Distributor, as shown in Figure 5.


Figure 5.

Here you can configure SQLServerAgent service to start automatically when the computer is started. Check Yes, configure the SQL Server Agent service to start automatically and click the Next button, as shown in Figure 6.


Figure 6.

Specify snapshot folder using a network path and click the Next button, as shown in Figure 7.


Figure 7.

Now you can customize the publishing and distribution settings, or you can choose the default settings. Check No, use the following default settings and click the Next button, as shown in Figure 8.


Figure 8.

Click the Finish button, as shown in Figure 9.


Figure 9.

Microsoft SQL Server created the distribution database, enabled publishing, and installed the distributor. Once completed, you should see Figure 10.


Figure 10.

Click OK button and see Figure 11. As we installed CHIGRIK as Distributor, so Replication monitor has been added to the console tree on CHIGRIK server. Click Close button.


Figure 11.

Now we are ready to start creating publications and articles. Select Tools => Replication => Create and Manage Publications as shown in Figure 12.


Figure 12.

You will see Create and Manage Publications dialog box, as shown in Figure 13. Choose pubs database and click the Create Publication button.


Figure 13.

The Create Publication wizard will be launch. Click the Next button, as shown in Figure 14.


Figure 14.

Choose the pubs database and click the Next button, as shown in Figure 15.


Figure 15.

Select Snapshot publication and click the Next button, as shown in Figure 16.


Figure 16.

Select all of the types of Subscribers that you expect to subscribe to this publication and click the Next button, as shown in Figure 17.


Figure 17.

Choose authors table to publish as article and click the Next button, as shown in Figure 18.


Figure 18.

Specify pubs_article as the publication name and click the Next button, as shown in Figure 19.


Figure 19.

You can specify data filters on this step, but in this example, we don’t use any data filters. Check No, create the publication as specified and click the Next button, as shown in Figure 20.


Figure 20.

Click the Finish button to create the publication, as shown in Figure 21.


Figure 21.

Now the ‘pubs_article’ publication was created, so click the Close button, as shown in Figure 22.


Figure 22.

Now you can create new subscription. Click the Push New Subscription button, as shown in Figure 23.


Figure 23.

This will launch the Push Subscription wizard shown in Figure 24. Click the Next button.


Figure 24.

Select CHIGRIK to select all subscribers in that group and click the Next button, as shown in Figure 25.


Figure 25.

Select pubs_copy database as the subscription database and click the Next button, as shown in Figure 26.


Figure 26.

Specify how frequently Distribution Agent updates the subscription (in this example, every 1 day(s), every 20 minute(s) between 9:00:00 and 18:00:00) and click the Next button, as shown in Figure 27.


Figure 27.

Check Start the Snapshot Agent to begin the initialization process immediately and click the Next button, as shown in Figure 28.


Figure 28.

Click the Next button, as shown in Figure 29.


Figure 29.

Click the Finish button to subscribe with the options you specified on the previous steps, as shown in Figure 30.


Figure 30.

Click the Close button, as shown in Figure 31.


Figure 31.

Click the Close button to close the Create and Manage Publication dialog, as shown in Figure 23. Note. On the last step we got the following message:

The subscription at Subscriber ‘CHIGRIK’ cannot be initialized immediately because the snapshot for this publication is not yet available. To initialize the subscription, start the Distribution Agent after the snapshot is available.

To work around it, you should start the Distribution Agent after the snapshot is available. See Figure 32.


Figure 32.

Backup and Restore Strategies

The backup and restore strategies differ for each replication types. Here, I want to describe the backup and restore strategies for the Snapshot replication.

There are four main strategies for backing up and restoring Snapshot replication:

  • Backup Publisher, master and model databases.
  • Backup Publisher, Distributor, master and model databases.
  • Backup Publisher, Subscriber(s), master and model databases.
  • Backup Publisher, Distributor, Subscriber(s), master and model databases.

Backing up Publisher, master and model databases is a simplest strategy. This strategy has its own advantages and disadvantages. The advantages are that it requires the least amount of storage resources and does not require coordinating the backup with the backup of any other servers. The main disadvantage of this strategy is that you may need to setup replication from the beginning in the event of a Publisher or Distributor failure. With this strategy, you should backup publication database after changing existing publications or after the new publications were added.

Backing up Publisher, Distributor, master and model databases is a more frequently used strategy than the first one, because in this case you don’t need to reestablish replication in the event of a Publisher or Distributor failure. The main disadvantage of this strategy is that you need to backup Publisher and Distributor’s databases simultaneously (or as closely as possible). It also requires more computing and storage resources than the first way.

Backing up Publisher, Subscriber(s), master and model databases reduces the amount of time required to recover a Subscriber(s) by avoiding the need to reinitialize the Subscriber(s) with a new snapshot, but in the event of a Distributor failure, you should setup replication from the beginning.

Backup Publisher, Distributor, Subscriber(s), master and model databases is the most complex backup strategy. The main advantage of this strategy is that in the event of a Publisher, Distributor or Subscriber(s) failure, you can quickly restore fail database without setting up replication from the beginning. The disadvantage of this strategy is that you need to backup Publisher and Distributor’s databases simultaneously (or as closely as possible), and this strategy requires the most computing and storage resources.

For each of the strategies you should backup msdb and master databases on the Publisher, Distributor and Subscriber(s). msdb database is used by SQL Server Agent for scheduling alerts and jobs (it also contains snapshot jobs), and master database is a main system database contains entries for each Subscriber(s), each login account, about system configuration settings and so on.

iis server tricks
sql server tricks
dedicated server
server hardware
computer help