Category Archives: Transact-Sql

Avoid Running T-Sql Scripts In the Wrong Azure Database

There are problems with "USE" in Sql Azure. If you prefix an SSMS script with “USE {DatabaseName}” to make sure it runs in the right place the statement will either be ignored (you’re already in the database referred to) or raise an error. That will kill the current batch, but if there is a GO statement in the script any statements after it will still run, and of course that will be in the wrong database. What to do? Continue reading Avoid Running T-Sql Scripts In the Wrong Azure Database

Sql Server Identity and Membership Functions

On my current research project I’m looking at security. The number of different functions available (some of which are obsolescent) has always been an irritation, so I decided to create a Transact-Sql query that demonstrates the functions and provides links to the documentation. You can use EXECUTE AS LOGIN or EXECUTE AS USER to explore what’s returned for other security principals, always assuming you have the necessary permissions of course. Hope you find it useful. Continue reading Sql Server Identity and Membership Functions

Agile Continuous Delivery of Databases (3)

A phase of my Agile Blue/Green deployment project is now complete, as I have a Proof of Concept which has been tested successfully. As you may recall from prior posts, the Blue/Green technique features two databases, the “green” live one that users connect to (I’ve made an arbitrary assignment of colours), and the offline “blue” one which has a different version of the schema (a previous or future release). Continue reading Agile Continuous Delivery of Databases (3)

Notes on the OVER Clause in Sql Server Ranking Functions

This article only deals with a couple of points so I’ll keep it short.

FIRST_VALUE, LAST_VALUE
FIRST_VALUE is a very useful ranking function and works predictably. LAST_VALUE is also useful but may give you surprises. You can ignore LAST_VALUE if you want, and just re-use FIRST_VALUE: Continue reading Notes on the OVER Clause in Sql Server Ranking Functions

CROSS APPLY with Ranking and Analytical Functions

As I posted here, I’m very interested in using CROSS APPLY in a new way to keep down clutter in sql statements with complex column expressions. I recently found a limitation. I think I’ve figured out why, and since understanding reduces frustration I’m posting it here. Continue reading CROSS APPLY with Ranking and Analytical Functions

OPENROWSET and BULK INSERT (4)

{Previous Article in Series}
Here are some final notes on this topic. I’m going to describe an import I did recently, and hope this may help you decide where OPENROWSET and BULK INSERT could fit into your development activity and what tools you might use.

I was doing an ad hoc load of some data from a small csv file. There was no corresponding table in the database to store this data, and I don’t like doing work for the sake of it, so my first stop was the “Sql Server 2014 Import and Export Data” wizard. I was hoping it would be a simple matter for the wizard to build a table and read the data into it. It crashed with an error which wasn’t very specific. Continue reading OPENROWSET and BULK INSERT (4)

The INSERT statement and Column Defaults in Sql Server

When you INSERT into a table which has a column with a default value defined, and you omit that column from your statement, what will happen? The default will be written. What if you write an explicit Null (and the column is nullable)? In nearly all cases the Null is written.

You may not want this – it’s one of the cases where it might be nice if there were more than one type of Null, as some people advocated in the early days. Then we could distinguish a “known unknown” (© D. Rumsfeld) Continue reading The INSERT statement and Column Defaults in Sql Server

Complex T-Sql Column Expressions? You must try this!

There’s an outstanding video by Kendra Little called “5 T-Sql Features You’re Missing Out On” which you can reach from my Links (Topical) page under “Database > Transact-Sql”. It describes a special way of using the CROSS APPLY statement in T-SQL. This is a technique that can change the way you write Sql, or at least it can in cases where you have an expression that is used in several columns of a SELECT statement. Continue reading Complex T-Sql Column Expressions? You must try this!

“Test-First” Database Development (2)

I’ve made additions and changes to the GitHub repository for “Test First” development of the database schema. All procedure names will now start with “Test”, so that they’re easier to find in Object Explorer and Intellisense. I’ve added a “TestHeader” stored procedure. You can put this at the top of your TDD script and it’ll add the date/time, database user, Sql Server version etc., to your results.

The spObjectExists procedure has been replaced by TestObjectExists, with the following signature:
CREATE PROCEDURE [dbo].[TestObjectExists]
@i_ObjectType sysname
,@i_ObjectName sysname = Null
,@i_ParentSchemaName sysname = Null --not needed if testing schema's existence
,@i_DbName sysname = Null
AS
Continue reading “Test-First” Database Development (2)