Twitter

Follow SQLQuill on Twitter

Subscribe

MN Pass

TSQL Tip O’ the Week – 7/5/11 – Keyboard Shortcuts

This weeks tip:

Again….What’s the fastest land animal?  The Cheetah.

What’s the fastest way to test a query?  Don’t use your mouse and learn lots of keyboard shortcuts!

Some of my favorites in the query editor of SQL Server Management Studio are:

  • All the text selection short cuts – standard in most Windows applications
  • F5 – Execute the highlighted query
  • CTRL + T – Results to Text
  • CTRL + D – Results to Grid
  • CTRL + R – Show or hide the results pane
  • CTRL + Shift + R – Refresh the Intellisense cache
  • CTRL+K then CTRL+C – Comment out the highlighted text
  • CTRL+K then CTRL+U – Uncomment the highlighted text

Pick one or two of these for a day and work on them.  In no time you will be writing, testing and understanding more code than ever before – and if your mouse runs out of batteries – it’s just not a problem.

Another good way to embed these in your mind is to hang a cheat sheet in your office for a quick and easy reference.

Here are some related posts from the intraweb:

Previous Tips O’ the Week

TSQL Tip O’ the Week – 6/28/11 – SP_HELPTEXT

This weeks tip:

What’s the fastest animal on earth?  The Cheetah.

What’s the fastest way to get a definition of a unencrypted stored procedure?  Use SP_HELPTEXT

If you don’t know, SP_HELPTEXT returns, in the results pane (use ALT+T to put the results to text), the TSQL of the following:

  • unencrypted stored procedure
  • view
  • trigger
  • user-defined function
  • user-defined rule
  • default
  • computed column
  • CHECK constraint
  • system object

Here’s an example from the AdventureWorks database…..

SP_HELPTEXT uspGetBillOfMaterials

Here are some related posts from the intraweb:

Previous Tips O’ the Week

TSQL Tip O’ the Week – 6/21/11 – Copy a Table Structure

This weeks tip:

Often I’ll need to copy a tables structure, i.e. all the columns and data types, to a new table.  Some examples of times when this is needed are when you are migrating data, staging data, etc.  A quick and easy way to do this is to select an empty record set into a new table.  For Example:

Select * into Table2 from Table1 where 1 = 2

Obviously one never equals two, so there will be no rows affected, but this will create Table2 with the same columns as Table1.

Note this method will include:

  • All columns
  • All data types and sizes
  • Identity columns
  • Nullability of all columns

Note this method will NOT include:

  • Indexes
  • Foreign keys
  • Constraints
  • Defaults
  • Triggers

Here’s what it looks like in SSMS with the AdventureWorks database – I copied the Production.BillOfMaterials table to a new table called Production.BillOfMaterials2:

BilloMaterials

Here are some related posts from the intraweb:

Previous Tips O’ the Week

TSQL Tip O’ the Week – 6/14/11 – The Empty GUID

This weeks tip:

In the programming world, there are a few times when dealing with uniqueidentifiers and/or GUIDs that you need an empty GUID – a GUID of all zeros (in fact .NET has a Guid.Empty field).

In TSQL, there are a few ways to get ‘00000000-0000-0000-0000-000000000000’

  • You could bookmark this page and every time you need it, come back here and copy and paste the value above into all your code (fine by me, but not the most efficient).
  • You could copy and paste any GUID into text pad and line up all the 0s below it, then paste that value into your code (that’s how I used to do it).
  • You could replicate the 0s a few times, throw in a few ‘-‘ and cast the entire thing as a uniqueidentier like this:
Cast(replicate('0', 7) + '0-' + replicate('0', 4) + '-' + replicate('0', 4) + '-' + replicate('0', 4) + '-' + replicate('0', 12) as UNIQUEIDENTIFIER)

Or….

The way I recommend, CAST 0 as a binary, then CAST that binary as a uniqueidentifier.  This seems the most versatile and easiest to remember –> turn 0 into a binary, then turn that into a uniqueidentifier, you don’t have to count zeros, or line up any dashes in notepad.  Here is the syntax:

Select CAST(CAST(0 as BINARY) as UNIQUEIDENTIFIER)

Here are some related posts from the intraweb:

Previous Tips O’ the Week

TSQL Tip O’ the Week – 6/6/11 – Declare and Assign Variables

I’m starting a new series of posts that will have a TSQL tip.  Some of them will be simple, some will be complicated, but hopefully they will all be helpful.

This weeks tip:Tip

When using SQL 2008 use the feature that allows you to declare and set your variables in the same line.  Here’s an example of how to do it:

Declare @tester varchar(100) = ‘This saves time’

Note – This saves a ton of time for debugging stored procedures – any default value can be left as is.

Here are some related posts from the blog-o-sphere: