Wednesday, 23 April 2014

SSMS Queries on Function Keys

By David Postlethwaite

At my presentation on SQL Server Management Studio at SQL Saturday in Exeter I promised to write some articles on Gethyn’s blog about the bits I didn’t manage to cover.

If you have simple queries or functions that you use regularly you can assign them to a function key for quick running in a query window

Microsoft have already added a few
If you hit  Ctrl-1 in a query window it runs sp_who
The results are displayed in the results window but the query window doesn’t display the query which can be a bit confusing.

You can set your own up at
Tools ->Options -> Environment -> keyboard -> Query Shortcuts

You’ll see there are already three already listed

Alt-F1    sp_help
Ctrl-1    sp_who
Ctrl-2    sp_lock

The function key Alt-F1 can be very useful.
In a query window type in the name of a system stored procedure such as sp_MSforeachdb
Now highlight the word and hit Alt-F1
The results window will show information on the stored procedure and also the parameters used by the procedure. Very handy.

As well as complete queries you can add partial statements to a function key.
Let me explain
Add the statement  “Select top 100 * from” to Ctrl-4

You must restart SSMS before the function key will work
Now open query Window

Type in the name of a table in the query window.
Now highlight the table name and hit Ctrl-4
The partial query will use the highlighted table name and run the full select query.

Tuesday, 22 April 2014

SQL Server Training – Spring 2014

I have just returned from a trip to Washington DC where I was delivering course 2105 – SQL Server 2012 DBA skills upgrade class in an onsite class to one of Learning Tree’s clients. For those of you that don’t know, a Learning Tree onsite is event is when Learning Tree come to you and deliver a course at your site just for you  and can even be tailored or customized to your specific requirements. You can follow the link to read more information about onsite events and what they can include.

SQL Server 2012 Database Administration

This spring I will be delivering two public courses that are scheduled to run in the London Education Centre (LEC)

Both of these events are guaranteed to run, so you can enroll safe in the knowledge that the course will not cancel. Maybe I’ll see you in-class

Monday, 21 April 2014

Central Management Studio CMS

By David Postlethwaite

At my presentation on SQL Server Management Studio at SQL Saturday in Exeter I promised to write some articles on Gethyn’s blog about the bits I didn’t manage to cover.

One of the subjects I covered was Registered Servers and the Central Management Studio
These are two features in SSMS that allow you to register your SQL instances with a more meaningful name and then group them into folders such as project name or type.
This can make it much easier to find the instances used by a particular project especially for new starters who won’t know the names of all the instances and projects in your company.
The CMS can be shared by all of your DBAs or developers so there is one central list of instances and what they are used for.

One additional feature is the ability to run queries against multiple instances. You simply right click on a particular folder and select “New Query”. A new query window will open with a pink footer showing connected (x/x), where x is the number of instances in the folder.
Any query run in this window will execute on every instance in that particular folder.
If you select the CMS server itself you can run a query on every instance you have registered

A great feature you may think but what if you accidently ran this in a multi server query window?

exec sp_msforeachdb 'drop database [?];'

Since writing my presentation I have come across a DBA team that aren’t allowed to use the CMS because of the danger of accidently running a dangerous multi server query on every instance in the company.

I’ve hunted high and low but can’t find anyway of disabling the multi query option in SSMS so if you are not willing to risk it then the usefulness of the CMS will not be available to your team.

What do other think?
Does your company use the CMS?
Are the benefits of the CMS worth the risk of someone accidently running a dangerous multi server query?
Have you found a way of disabling the multi server query option?

Let me know?

Thursday, 17 April 2014

SSMS Auto Recovery

By David Postlethwaite

At my presentation on SQL Server Management Studio at SQL Saturday in Exeter I promised to write some articles on Gethyn’s blog about the bits I didn’t manage to cover.

SQL Server Management Studio will autosave your unsaved query every few minutes so if if your pc crashes you won’t have lost you work.
This is a similar idea to what we see in Word and Excel

Any new query will be added to the solution or project that is currently open. If you don’t have a solution open it will get save into a backup folder
My Documents\SQL Server Management Studio\Backup Files\Solution1

You can alter the recovery time
Tools -> Options -> Environment –> AutoRecover

 

image

There is a bug in Management Studio 2012 where the file must be saved first before it will starts to autosave it.
If you haven’t saved it at least once then it won’t autosave.

Management Studio 2008 and 2005 will autosave unsaved files without problem

Wednesday, 16 April 2014

From where does SSMS get its database defaults?

We can create a database simply by typing in the command

create database DB1 



We don’t have to provide any other parameters for SSMS to be able to create the database

My Question is from where does SQL get the default values when we create a database without any options?
We have always assumed that is was from the model database.

Here’s an interesting exercise:

Create an empty database using the command above and make a note of its size and autogrowth values.
Compare these to those of the model database. Assuming you have never changed the model database you’ll find they are identical.

Change the initial size and the autogrowth values in the model database to something obviously different.
Now create a new database DB2 with the script

create database DB2 



Take a look at the size and autogrowth of the new database and compare them to the values you put in the model database. The data file size will probably match but the rest will have taken the original values you saw in the model database.

SSMS hasn’t used the values in the model database which is what we all assumed it would do

Now create a database using the GUI
You will see in the GUI that the new database has taken its values from the model database
It seems that SSMS only uses the model database as its template when you use the GUI, you cannot guarantee the values that will be used when using a simple script
Which is why its best practice is to include all the parameters when creating database.

In case you need to reset mode back to the SQL defaults then run this

USE [model]
GO
DBCC SHRINKFILE (N'modeldev' , 3)
GO
DBCC SHRINKFILE (N'modellog' , 1)
GO
USE [master]
GO
ALTER DATABASE [model] MODIFY FILE ( NAME = N'modeldev', FILEGROWTH = 1024KB )
GO
ALTER DATABASE [model] MODIFY FILE ( NAME = N'modellog', FILEGROWTH = 10%)

Monday, 14 April 2014

Missing Database Owner

By David Postlethwaite

At my presentation on SQL Server Management Studio at SQL Saturday in Exeter I promised to write some articles on Gethyn’s blog about the bits I didn’t manage to cover.

image

A scenario
A DBA creates a new database and by doing so he become the owner of that database.
He then leaves the company and his Windows account is deleted.
When you try to view the database properties in SQL Server Management Studio you will receive the error “Property Owner is not available”

When SSMS tries to open the database it check the permissions of the owner and because it can no longer find it in Active Directory it aborts the request

You will not be able to view the properties until a new owner is assigned.
This is quite simple just issue this command on the database

sp_changedbowner 'sa' 



You cannot just create a new Windows user with the same name. The underlying SIDs will not match.

If the owner of the database is a SQL user then it’s not possible to delete that user but SQL Server can’t manage Active Directory so is unaware when an account has been deleted.

Also note that if the owner of an SQL Agent job is deleted that Agent job will not run because, once again, SQL checks the permissions of the owner and can’t find it in AD so will not continue.

To avoid this scenario it is best practice to ensure that all databases and agent jobs are owned by “sa” unless there is a good reason not to.

Thursday, 10 April 2014

Alter Table Not Permitted

By David Postlethwaite

At my presentation on SQL Server Management Studio at SQL Saturday in Exeter I promised to write some articles on Gethyn’s blog about the bits I didn’t manage to cover.

image

You may have come across this error message when using SQL Server Management Studio

It’s new in SSMS 2008.
You cannot, by default, make changes to a table that will require it to be dropped and re-created.
These changes are
Changing data type on existing columns
Or changing allow nulls on existing columns
Or changing order of columns

But you can turn this behaviour off
Tools Menu -> Options -> Designers
Untick "prevent saving changes that require table re-creation"

But beware.
If it is a table with large amount of data it could take some time to copy it from the old to the new table