How to Rename a Database

If you ever need to rename a database, I recommend using the following script. It ensures no other users are connected. This is important, otherwise your connection won’t be able to gain a lock. I hope that helps!

Use RowCount to determine query success

If you have a Stored Procedure that populates a temporary table, and you want to ensure the temporary table has been populated with something, you could use @@Rowcount. It’s an in-build property, and can be used as an alternative to selecting COUNT(1) from the temporary…

Setting NOCOUNT in your Stored Procedures

NOCOUNT is a SQL Server setting that (when activated) will stop SQL Server from returning the “x row(s) affected” message as part of its execution. Although this message is helpful with T-SQL queries in SQL Server Management Studio, it is of little use with Stored…

Set variable using a case statement

Case statements are obviously a tidy alternative to using simple if/else statements, and I recently found myself in a position where I had a good opportunity to use one. First off, let’s look at the syntax. In the above syntax example, we have the variable…

Getting first and last days of a given month

The following statements will allow you to manipulate date values from a date input (e.g. GETDATE()) to provide you with the last day of the given month. I have found this very helpful. You can also specify a hard-coded date, and wrap in CAST functions…