Showing posts with label SQL Server. Show all posts
Showing posts with label SQL Server. Show all posts

Friday, 25 July 2014

Database Unit Testing and Learning TSQL

Did you know that you can write database unit tests for SQL Server?

There is a framework called tSQLt that helps you to do it.

There is also a tool from Red Gate Software called SQL Test that provide a nice UI on top of it, integrated in SQL Server Management Studio.


What about using tests for learning TSQL?

I think that it is not unreasonable to design a TSQL course where each lesson is made of a set of tests that you need to make pass. This idea of learning by making tests pass is used in the nand2tetris course that I am doing with the friends of the Cambridge Programmer's Study Group.

In this post, I try to create an example using tSQLt.

The database I use is from the Training Kit: Querying Microsoft SQL Server 2012.

Installing tSQLt

First run the following SQL statements.


Execute the tSQLt.class.sql script provided by tSQLt.

Creating the test

First, you need to create a test class that we call Tests.


Then, we create a test.
The framework tSQLt allows you to create fake tables but in this case the test use the actual table HR.Employees and the actual data contained in it.


Running the test

You can run the test individually using the tSQLt.Run or alternatively you can run all the tests in the database using tSQLt.RunAll
You can also create a keyboard shortcut to run all the tests easily.

You can see that the test fail!


Make the test pass and learn

You can make the test pass simply adding the DISTINCT keyword in the select statement.  

Run the tests and they all pass.


I really like the idea of learning by making tests pass.

What do you think?


Monday, 2 June 2014

Stored Procedure to Calculate Expressions

I was reading the section Character Functions in the Chapter 2 of the book Querying Microsoft SQL Server 2012 and pretty soon I get bored of trying individually the various functions on strings available in T-SQL.

Then, I challenged myself to write a stored procedure (this is my first one) to evaluate an arithmetic expression with parenthesis and show the individual steps of the calculation. I wanted something generic but I added some restrictions to it after seeing how crazy is to implement a split function in T-SQL.


This is the stored procedure. I know, it is crazy and I am sure there are much better way of doing it. Remember that my goal was to just get confidence with the T-SQL functions on strings. Anyway, I am happy to receive feedback on it and see different way of implementing this behaviour.


This is the result of the execution.


It was fun :)



Wednesday, 28 May 2014

Preparing for Querying Microsoft SQL Server 2012

Few months ago I joined Red Gate Software and I am having a lot of fun so far. We have a great team in SQL Compare and we are working hard to add support for SQL Server 2014.

I think that learning more about SQL Server is very important because a lot of Red Gate products are designed to make DBA and developers working with it in a very productive way.

This is why I decided to start the path toward becoming a MCSA on SQL Server. To be honest, as a developer, my goal is just learning more about SQL Server and not necessarily get certified. However I will use the certification materials as a clear path to increase my knowledge.

The first exam on the list is
[70-461] Querying Microsoft SQL Server 2012

I know SQL and I have studied database development and design at University. However I don't know much about SQL Server specific features and the incredible power that the platform can offer. 

In order to keep me motivated I will post on my blog all the things I found interesting during my learning.

The following is a list of all the resources I will use to prepare for this exam:

It's time to write SQL :)