How to change the collation of a database in SQL Server?

Have you ever wondered how to change the collation for a database in SQL Server?

Well the other day I received a database from a third party supplier and the first step was to change the Collation of the database. My questioning about why they would supply the database in the incorrect collation setting was met with a wall of silence. Therefore I had to put in my documentation how to change the Collation of the database.

I did it the quick way by right click on database > Select properties > options > Select appropriate collation from dropdown menu next to Collation.

Database properties
Database properties

That was a good enough solution at the time but for my documentation I would always add the T-SQL so that anyone could follow the guide without making mistakes.

Below is an example of how to change the Collation via T-SQL

ALTER DATABASE AdventureWorks2012 COLLATE SQL_Latin1_General_CP850_BIN

Please let me know whether you found this guide useful and leave a message in the comments.

SQL Server Collations Find current collation and all supported collations

Have you ever needed to check your database or SQL instance to find out its collation? Have you ever wanted to check whether a collation is supported by Windows or SQL Server?

Well I am going to show you how to find out the information quickly and easily.

The below query will show the instance level collation.
SELECT SERVERPROPERTY(‘collation’)

1

The query below will show the collation setting at the database level

 

SELECT DATABASEPROPERTYEX(‘AdventureWorks2012’, ‘Collation’) SQLCollation

2

Sometimes its important to know the collations which the OS supports and the following query will help list all the collations which are supported by the OS.

 

SELECT * FROM sys.fn_helpcollations() WHERE name NOT LIKE ‘SQL%’;

3

When doing migrations it is very important to ensure that the Collation setting is maintained and to know whether it is supported by the SQL instance. The below query will help you easily and quickly ascertain that information.

 

SELECT * FROM sys.fn_helpcollations() WHERE name LIKE ‘SQL%’;

4

 

Hopefully that covers all your collation gathering requirements but if you do have another technique which I have missed then please leave them in the comments below.

 

Statistical Aggregate functions in SQL Server

Have you ever wondered how to do program or perform simple statistical function within SQL Server? Well the kind people at Microsoft have enabled us to all to save time by giving us a few inbuilt functions which save us from having to solve them programmatically.
I have used the unit Price column in the SalesOrderDetail table within the Adventureworks database to illustrate the in built functions

 

SELECT --UnitPrice,
COUNT(UnitPrice) AS 'Count of UnitPrice',
AVG(UnitPrice) AS 'Average UnitPrice',
MIN(UnitPrice) AS 'Minimum UnitPrice',
MAX(UnitPrice) AS 'Maximum UnitPrice',
SUM(UnitPrice) AS 'Total UnitPrice',
VAR(UnitPrice) AS 'Variance of UnitPrice',
STDEV(UnitPrice) AS 'Standard deviation of UnitPrice'
FROM [Sales].[SalesOrderDetail] 

1
The COUNT function counts the number of values within the column.
The AVG function returns the average unit price column.
The MIN function returns the smallest value within the column.
The MAX function returns the largest value within the column.
The SUM function returns the sum total of all the values within the column.
The VAR function returns the variance of all the values within the column.
The STDEV function returns the standard of all the values within the column.

 

I am sure that I have probably missed out a few functions so please let me know via the comments below.

SQL Cursor to Kill all connections to a database

Have you ever tried to restore over a database but found that all attempts are being blocked by an annoying SPID? Or had hundreds of orphaned SPIDs running crazy on your instance?

Well I have the solution for you with the below script which uses a cursor to kill all connections to a database.

DECLARE @spid varchar(10)</pre>
DECLARE kill_spid CURSOR fast_forward FOR
SELECT SPID FROM sys.sysprocesses WHERE DB_NAME(dbid) = 'AdventureWorks2012' AND spid > 50
OPEN kill_spid
FETCH NEXT FROM kill_spid INTO @spid
while @@FETCH_STATUS = 0
BEGIN EXEC ('Kill ' + @Spid)
FETCH NEXT
FROM kill_spid INTO @spid
END
CLOSE kill_spid
DEALLOCATE kill_spid

Make sure that you change AdventureWorks2012 for your database name and double check that you have the correct database name as I have seen it when people put the wrong database name in and its never a pretty sight.

 

SQL Cursors

Cursors are a way of manipulating data and interacting with them one at a time. They have a bad reputation within the SQL world as they go against the SET based logic and they can have a very high performance cost. Where possible you should ask yourself whether you could avoid using a Cursor. This is because of the performance advantages a SET based solution has and that Cursor problems only increase when the tasks are scaled up.

The five general steps of a cursor are:

  1. Declaration of the cursor
  2. Opening the cursor
  3. Fetching and manipulating the data
  4. Closing the cursor
  5. Deallocating the cursor

A simple example of a Cursor is below.

Declare @Databases varchar(50)</pre>
Declare DatabasesOnIntance CURSOR READ_ONLY FOR SELECT name FROM sys.databases order by name
Open DatabasesOnIntance
Fetch next from DatabasesOnIntance into @Databases
While @@FETCH_STATUS = 0
Begin
Print @Databases
Fetch next from DatabasesOnIntance into @Databases
End
Close DatabasesOnIntance
Deallocate DatabasesOnIntance
Cursor Results
Cursor Results

As you can see the Cursor I have created has printed each database on the instance

WHILE, BREAK, and CONTINUE Statements

Have you ever wondered how to create a loop in SQL? Or wondered how to break and escape a loop?

Well I am going to briefly introduce you to the WHILE, BREAK, and CONTINUE Statements which will satisfy your curiosity

The first command I will introduce you to is WHILE

DECLARE @i int = 1;
WHILE @i < = 5

BEGIN

PRINT @i;

SET @i = @i + 1;

END

Loop

Loop

As you can see the WHILE statement will force the loop to continue until we reach 5. This is very useful if you need to batch process tasks and limit the amount done within each batch.

The next command to learn is CONTINUE. This command forces you to go back to the beginning of the loop.

 

DECLARE @i int = 1;
WHILE @i < = 5

BEGIN

PRINT @i;

SET @i = @i + 1;

CONTINUE; -- This will cause the WHILE to loop back

PRINT 'You wont see this due to the CONTINUE commands cleverness.';

END
Loop
Loop

Once the WHILE command is satisfied the CONTINUE command will allow the loop to complete.

The final command related to loops is the BREAK command.

 

DECLARE @i int = 1;
WHILE @i < = 5

BEGIN

PRINT @i;

SET @i = @i + 1;

BREAK; -- Force the WHILE loop to terminate

PRINT 'You wont see this due to the BREAK commands cleverness..';

END

 

Loop
Loop

As you can see from the example when the BREAK is encountered the loop is broken and it only ever prints 1.

In the real world many developers try not to use the BREAK and CONTINUE commands as they can be easily avoided in code and many people find it makes code less readable and unnecessarily complex.

 

 

Learn about the WAITFOR TIME & WAITFOR DELAY

Have you ever wanted to pause a command for a short period or wanted to run a transaction at a specific time.

 

Well I am going to quickly show you how to do them both using the WAITFOR command.

SELECT GETDATE()
WAITFOR DELAY '00:00:10';
BEGIN
SELECT GETDATE()
END;
Date time
Date time

The above code shows you how to delay two print current date and time commands by ten seconds. It can be easily modified for any time such as 43 minutes, 43 hours….

SELECT GETDATE()
WAITFOR TIME '12:22:00';

BEGIN

SELECT GETDATE()

END;
Date time
Date time

The above command will cause the transaction to wait until that time before executing the command. The time can be modified at your pleasure for whatever time you require.

The two processes do carry a processor overhead as the transaction will be running until it’s completed. Also, these commands can usually be replaced by an appropriately timed SQL Agent job which reduces the process overhead and makes the administration of it far easier.

Declaring Variables and Retrieving Variables

A variable can be best described as being a place holder for information which you then fill in with relevant information which you will want to retrieve later.

The below query shows the result from the CURRENT_TIMESTAMP which returns the current date and time.

PRINT CURRENT_TIMESTAMP

Jan 15 2015 11:44AM

1

The CURRENT_TIMESTAMP result can be made into a variable which can be retrieved later.

DECLARE @ThisIsTheCurrentDateandTime Datetime = CURRENT_TIMESTAMP

PRINT @ThisIsTheCurrentDateandTime

2

The DECLARE command creates the variable, assigns the datatype and the information I would like to put into the variable.

After I have created the variable I then retrieve it using the PRINT command to show the value stored.

TSQL Challenge on BeyondRelational.COM

I was asked by one of my junior colleagues to help him with a puzzle he had seen on BeyondRelational.COM which I thought would be a nice challenge. I liked the premise of the scenario and I always liked a challenge decided to show my colleague how I would approach and resolve this problem.

I was a little annoyed that the example code to create the test data didn’t work so did a quick fix which I have posted below.

</pre>
CREATE TABLE Firstchallenge(
EmployeeID INT IDENTITY,
EmployeeName VARCHAR(15),
Department VARCHAR(15),
Salary NUMERIC(16,2)
)

INSERT INTO Firstchallenge(EmployeeName, Department, Salary)
VALUES('T Cook','Finance', 40000)
INSERT INTO Firstchallenge(EmployeeName, Department, Salary)
VALUES('D Michael','Finance', 25000)
INSERT INTO Firstchallenge(EmployeeName, Department, Salary)
VALUES('A Smith','Finance', 25000)
INSERT INTO Firstchallenge(EmployeeName, Department, Salary)
VALUES('D Adams','Finance', 15000)

INSERT INTO Firstchallenge(EmployeeName, Department, Salary)
VALUES('M Williams','IT', 80000)
INSERT INTO Firstchallenge(EmployeeName, Department, Salary)
VALUES('D Jones','IT', 40000)
INSERT INTO Firstchallenge(EmployeeName, Department, Salary)
VALUES('J Miller','IT', 50000)
INSERT INTO Firstchallenge(EmployeeName, Department, Salary)
VALUES('L Lewis','IT', 50000)

INSERT INTO Firstchallenge(EmployeeName, Department, Salary)
VALUES('A Anderson','Back-Office', 25000)
INSERT INTO Firstchallenge(EmployeeName, Department, Salary)
VALUES('S Martin','Back-Office', 15000)
INSERT INTO Firstchallenge(EmployeeName, Department, Salary)
VALUES('J Garcia','Back-Office', 15000)
INSERT INTO Firstchallenge(EmployeeName, Department, Salary)
VALUES('T Clerk','Back-Office', 10000)
<pre>

I told him that it would be quite simple to get the Ranking by using the RANK command and Partitioning the data by Department. However, I know you can’t filter by rankings from past experience but I knew if I put it into a subquery I could then filter the data which I have done below.


SELECT [EmployeeID]
 ,[EmployeeName]
 ,[Department]
 ,[Salary]
 FROM
(
SELECT [EmployeeID]
 ,[EmployeeName]
 ,[Department]
 ,[Salary]
 ,RANK() OVER (PARTITION BY [Department]ORDER BY [Salary] DESC) AS 'Salary Rank'
 FROM [TESTDB].[dbo].[Firstchallenge]
 ) A
WHERE [Salary Rank] = 2

I know that I could improve the query by using a CTE but I was happy that I was able to to get the result required and will probably put the CTE query in a future update of this article.

1

IF ELSE Control of flow statements

The IF ELSE statements are one of the most frequently used statement within SQL and control of

flow statements are one of the core statements within any programing language. Once you have

understood the concept you can easily make powerful scripts.

An IF statement is a check to see whether a condition is TRUE and is used in hundreds of different

scenarios such as checking whether an object exists, checking data for a value…… I always liken the

IF statement to checking your fridge for your favourite meal and if its there you eat otherwise you

check out all your other options.

The below is a simple script which tests to see whether I have the AdventureWorks2012 database.


IF EXISTS (SELECT * FROM sys.databases

WHERE name = 'AdventureWorks2012' )

PRINT 'AdventureWorks2012 is installed'

The result you will get is below.

AdventureWorks2012 is installed

1

The query checks whether the AdventureWorks2012 returns a value or is TRUE from my query and

then runs the PRINT command to confirm the value was returned as I expected.

You can make the IF statement more powerful by adding the ELSE statement. The ELSE Statement is

used as we often don’t just want to check if a statement is TRUE but also want to produce an action if its false.


DECLARE @TestValue int

SET @TestValue = 1

IF @TestValue >1

BEGIN

PRINT 'The Test value is greater than 1'

END

ELSE

PRINT 'The Test value is less than or equal to 1'

The result you will get is below.

The Test value is less than or equal to 1

2