What we know already:
SQL Server has some really stupid, generic error messages.
Case in point…
String or binary data would be truncated.
Yes, we know what it means but what column would be truncated? What value would be the offender here?
I am okay with not having the exact answer but it would be nice to have more!
What I learned:
PowerShell actually has some pretty generic error messages as well.
Since I am using PowerShell mainly for interacting with multiple SQL instances, my PowerShell errors mainly revolve around SQL Server.
So this error message is not helpful.
(I’m slightly colour-blind so I can barely read red on blue, I find this green (yellow?) easier)
Can we get more?
Sure we can but let’s set up an example so you can play-along at home too.
First of all, what PowerShell version are we using?
Great! So let us add in our assemblies that will allow us to connect to SQL Server using SMO.
# Load the assembly since we probably do not have it loaded
Now I like the results showing up but if you don’t want them, just throw a
$null = before the
# SILENTLY load the assembly since we probably do not have it loaded
$null = [System.Reflection.Assembly]::LoadWithPartialName(‘Microsoft.SqlServer.Smo’)
Now let us connect to mine (or your) database to run some scripts against it.
# Connect to the instance and database
$SQLInstance = New-Object Microsoft.SqlServer.Management.Smo.Server ‘localhost’
$Database = New-Object Microsoft.SqlServer.Management.Smo.Database
$Database = $SQLInstance.Databases.Item(‘Pantheon’)
Everything is getting thrown into a variable/object here so there is going to be no output. Just change the ‘localhost’ bit to your server and ‘Pantheon’ to your test database.
Now, let’s get our T-SQL on!
# Create our T-SQL statement.
$sql = ‘SELECT SERVERPROPERTY(‘ProductLevel’) AS What?, SERVERPROPERTY(‘ProductVersion’) AS Huh?;’
You can see the first problem we run into here. The single quotation marks are breaking up our statement.
There are 2 fixes for this; we can double quotation mark the start and end of the string e.g.
"SELECT ..." or we can do what we normally do in SQL Server and double up the single quotation marks e.g.
I’ve gone with the latter but hey, you choose, go crazy, whatever you want!
So now we have this:
Now let us run this against our database and see what happens.
# Execute with results…kinda like it says…
The whole reason for this blog post i.e. stupid, generic error message.
Now ignoring the fact that you already know what is wrong, this tells me that there is either something wrong with the
$Database variable, the
$sql variable or the syntax statement. Maybe even something else though!
This is not helpful and I’m going to have a bad time.
help about_automatic_variables -showwindow
but the main point is that
Contains an array of error objects that represent the most
recent errors. The most recent error is the first error object in the
So we want more information about our error message so we go…
And we get…
This…this is not what I wanted.
Thankfully, the defintion states that it is an error object and we know that objects can have more properties than what is shown be default.
So we try again, making sure that we return everything
# More than Generic
$Error | Select-Object *
Bingo, that is a lot more helpful! Especially when we scan the results and we see this guy (highlighted)
We may be working with PowerShell but we still have to obey SQL Server’s rules. So if we want to have a column with a question mark, we’re going to need to wrap it in square brackets.
So let’s fix up our
$sql variable and try again.
# fix me!
$sql = ‘SELECT SERVERPROPERTY(”ProductLevel”) AS [What?], SERVERPROPERTY(”ProductVersion”) AS [Huh?];’
We re-run out execute…
#Execute with results…kinda like it says…
Like a sheepdog, let’s round it up:
I’m liking PowerShell more and more as I use it.
That is mainly outside of work but I’ve already turned my gathering of daily checks data from a half hour long process to a 2 minute one.
So it’s nice to know that, while it may have stupid, generic error messages, it also has the tools to help you with them.
Now if we could only get the tools to deal with “String or binary data would be truncated”…