Archive for the ‘ DBA Toolbox / T-SQL Scripts ’ Category

Different T-SQL Date Variations for Date Dimensions

Just another handy code snippet for generating dates for date dimensions in your data warehouse.

I am providing just the select statement here, but to generate (massive) date records, just create and set your start and end date variables, and enclose your insert and the code below in your WHILE loop.

Read the rest of this entry »

VN:F [1.9.7_1111]
Rating: 10.0/10 (9 votes cast)
VN:F [1.9.7_1111]
Rating: 0 (from 0 votes)
Share :
  • Digg
  • del.icio.us
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • TwitThis

tablediff.exe – Compare and Synchronize your SQL Server tables

Need to check if your tables in 2 different servers are out-of-sync? SQL Server comes with a command line tool that does the job. It’s called tablediff.exe (read up BOL entry for this nifty tool).

By default, you can find this in the COM directory of your SQL Server install folder.

In SQL Server 2005 by default it will be in:
C:\Program Files\Microsoft SQL Server\90

In SQL Server 2008 / SQL Server 2008 R2 by default it will be in:
C:\Program Files\Microsoft SQL Server\100

These are the switches you can specify for the tool:

Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\Administrator>cd C:\Program Files\Microsoft SQL Server\100\COM

C:\Program Files\Microsoft SQL Server\100\COM>tablediff.exe /?
Microsoft (R) SQL Server Replication Diff Tool
Copyright (c) 2008 Microsoft Corporation

User-specified agent parameter values:
/?

 Replication Diff Tool Command Line Options

        usage: tablediff

          -- Source Options --
 -sourceserver          Source Host
 -sourcedatabase        Source Database
 -sourceschema          Source Schema Name
 -sourcetable           Source Table or View
 -sourceuser            Source Login
 -sourcepassword        Source Password
 -sourcelocked          Lock the source table/view durring tablediff

          -- Destination Options --
 -destinationserver     Destination Host
 -destinationdatabase   Destination Database
 -destinationschema     Destination Schema Name
 -destinationtable      Destination Table or View
 -destinationuser       Destination Login
 -destinationpassword   Destination Password
 -destinationlocked     Lock the destination table/view durring tablediff

          -- Misc Options --
 -t             Timeout
 -c             Column Level Diff
 -f             Generate Fix SQL (You may also specify a file name and path)
 -q             Quick Row Count
 -et            Specify a table to put the errors into
 -dt            Drop the error table if it exists
 -o             Output file
 -b             Number of bytes to read for blob data types
 -strict        Strict compare of source and destination schema
 -rc            Number of retries
 -ri            Retry interval

Here is an example usage. Note this has to be all in one line at the Command Prompt (I just separated them out into different lines for clarity):
Read the rest of this entry »

VN:F [1.9.7_1111]
Rating: 9.8/10 (4 votes cast)
VN:F [1.9.7_1111]
Rating: 0 (from 0 votes)
Share :
  • Digg
  • del.icio.us
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • TwitThis

Excellent Free Ebooks from RedGate

Excellent Free Ebooks from RedGate

SQL Server eBook Selection
1. Protecting SQL Server Data by John Magnabosco
2. The Art of High Performance SQL by Grant Fritchey
3. The Art of XSD by Jacob Sebastian
4. Two Minute SQL Server Stumpers Vol. 5

DBA Ebooks Bundle
1. Mastering SQL Server Profiler
2. Brad’s Sure Guide to SQL Server 2008
3. Brad McGehee’s DBA Best Practices
4. How to Become an Exceptional DBA (2nd edition)
5. SQL Server Tacklebox

The Best of Simple Talk .NET
1. The Best of Simple Talk .NET – Jit N’ Run vol.2

You can download them from this link

1. Protecting SQL Server Data by John Magnabosco
2. The Art of High Performance SQL by Grant Fritchey
3. The Art of XSD by Jacob Sebastian
4. Two Minute SQL Server Stumpers Vol. 5
VN:F [1.9.7_1111]
Rating: 9.8/10 (5 votes cast)
VN:F [1.9.7_1111]
Rating: 0 (from 0 votes)
Share :
  • Digg
  • del.icio.us
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • TwitThis

SQL Server XML Red Gate Ebook, XQuery Labs

Jacob Sebastian is a SQL Server XML Guru!

Check out his collection for XQUery Labs. This is the first 12 of his series, and right now he has 43 and counting:

XQuery Sample Scripts

* XQuery Lab 1 – Transforming rows to columns
* XQuery Lab 2 – An example using OUTER APPLY
* XQuery Lab 3 – Filtering specific nodes
* XQuery Lab 4 – Joining XML Nodes with a Relational Table
* XQuery Lab 5 – Working with Namespaces
* XQuery Lab 6 – Processing Header-Detail information
* XQuery Lab 7 – Extracting a comma separated list of values
* XQuery Lab 8 – How to update the attribute value of an XML variable?
* XQuery Lab 9 – How to delete an attribute from an XML variable?
* XQuery Lab 10 – How to insert an attribute to an XML variable
* XQuery Lab 11 – How to insert an element to an XML variable
* XQuery Lab 12 – Different ways of reading values from an XML variable

Jacob Sebastian has also released a free ebook via RedGate – The Art of XSD – SQL Server XML Schema Collections

Check it out, all 483 pages! :)

VN:F [1.9.7_1111]
Rating: 8.0/10 (2 votes cast)
VN:F [1.9.7_1111]
Rating: 0 (from 0 votes)
Share :
  • Digg
  • del.icio.us
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • TwitThis

SQL Server PowerShell : Search for SQL Server Objects Using PowerShell

I have posted previously 3 different ways of searching for SQL Server Objects (How to Search for Columns in SQL Server ).

Here’s a fourth one!

Check out how powerful and flexible PowerShell is when you need to look for a database object. In the script below, I only search databases, tables, columns, and indexes. But in reality, really, sky is the limit!
Read the rest of this entry »

VN:F [1.9.7_1111]
Rating: 8.4/10 (5 votes cast)
VN:F [1.9.7_1111]
Rating: 0 (from 0 votes)
Share :
  • Digg
  • del.icio.us
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • TwitThis

Here is a simple script to audit your SQL Server Instance Properties.

Note that the property names are not hardcoded. We query each of these properties, and use those to display the property values.

If you prefer, you can also query directly each of the properties of an instance. If this is the case, just specify your server object and then the property name. For example:

$serverObject.BackupDirectory

The list of properties can be found at the end of the post.
Read the rest of this entry »

VN:F [1.9.7_1111]
Rating: 10.0/10 (5 votes cast)
VN:F [1.9.7_1111]
Rating: 0 (from 0 votes)
Share :
  • Digg
  • del.icio.us
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • TwitThis

A follow-up to my previous post: How to Restore SQL Server Databases Using SMO and PowerShell

In this post I will show you how you can restore your database to :

1. the existing database (same database name)
2. a different database (different database name, different mdf and ldf)

As with the previous post, to do a SQL Server restore in SQL Server, you will need to use the SMO SqlBackup method. In SQL Server 2008, you will need to load Microsoft.SqlServer.SmoExtended assembly otherwise, you will get the following error:

Cannot find type [Microsoft.SqlServer.Management.Smo.Backup]: make sure the assembly containing this type is loaded.

Other assemblies you may want to load are:


[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null
#Need SmoExtended for smo.backup
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum") | Out-Null

Read the rest of this entry »

VN:F [1.9.7_1111]
Rating: 9.1/10 (20 votes cast)
VN:F [1.9.7_1111]
Rating: +1 (from 1 vote)
Share :
  • Digg
  • del.icio.us
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • TwitThis

PowerShell makes it easier to manage even your database backups and restore.

To do a SQL Server backup in SQL Server, you will need to use the SMO SqlBackup method. In SQL Server 2008, you will need to load Microsoft.SqlServer.SmoExtended assembly otherwise, you will get the following error:

Cannot find type [Microsoft.SqlServer.Management.Smo.Backup]: make sure the assembly containing this type is loaded.

Other assemblies you may want to load are:


[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null
#Need SmoExtended for smo.backup
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum") | Out-Null

Also another point to note is the types of backup you can do. BackupActionType specifies the type of backup. Valid values for this option are Database, Files, Log

Here’s the script. This script is for one specific database. If you want to use this for several database, you will just need to use this code inside a loop.
Better yet, put this in a function, and call this in a loop. I will try to do that sometime soon.
Read the rest of this entry »

VN:F [1.9.7_1111]
Rating: 9.4/10 (29 votes cast)
VN:F [1.9.7_1111]
Rating: +6 (from 6 votes)
Share :
  • Digg
  • del.icio.us
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • TwitThis
`