Requires A Singleton?

Facebooktwitterredditpinterestlinkedinmail

Have you ever got this error message when you tried to parse an XML string in SQL Server?

XQuery [value()]: ‘value()’ requires a singleton (or empty sequence), found operand of type ‘xdt:untypedAtomic *’

DECLARE	@XMLToParse  XML
 
-- Load the XML data in to a variable to work with.
-- This would typically be passed as a parameter to a stored proc
SET		@XMLToParse =	'
								Baboon
						 '

SELECT	
@XMLToParse.value('data(/Animal/AnimalName)', 'VARCHAR(MAX)') AS AnimalName

 
How do we fix this?

This error is saying that the value() function needs an xpath that only exits once (non-repeating node). You and I know that there will only be one instance of , but SQL Server does not know that. To solve this, we just need to tell SQL Server that it should use the first instance of this AnimalName node that it finds. We do this by added the [1] tag to the xpath. We want to add it after the closing parentheses. This will tell SQL Server to grab whatever is the first node with that name.

DECLARE	@XMLToParse  XML
 
-- Load the XML data in to a variable to work with.
-- This would typically be passed as a parameter to a stored proc
SET		@XMLToParse =	'
								Baboon
						 '

SELECT	
@XMLToParse.value('data(/Animal/AnimalName)[1]', 'VARCHAR(MAX)') AS AnimalName
-- Notice the [1] on the line above!

 
Extra Fun

If there are multiple nodes with the same name in your XML, you can adjust the number inside the square braces to match whatever node you want. For example:


	Baboon
	Cat
	Dog 

[1] would return Baboon
[2] would return Cat
[3] would return Dog

SQL Server LIKE With Percent Literal

Facebooktwitterredditpinterestlinkedinmail

The LIKE operator in SQL Server is a really powerful tool. It allows you to specify a pattern to match records in your where clause. You can use the % (percent) as a wildcard value. For example: ‘monk%’ will match ‘monkey’ and ‘monkeys’. But what if you want to match the words ‘60% off sale’ and ‘60% off sales’… you can’t just put ‘60% off sale%’… you need to escape the first %. SQL Server gives us two different ways to escape a special character.

 
SQL Server LIKE – Exact Single Character

The first way that you can do this is by specifying a specific single character in your pattern. This is done by wrapping the single character in [ ]. The character that you put inside the brackets will tell the system that that character must be found exactly as appears.

SELECT	SaleDescription
FROM	SaleInfo
WHERE	SaleDescription LIKE '60[%] off sale%'

 
SQL Server LIKE – Escape Character (read at the bottom of this post to find out what an escape character is)

The second way that you can do this is by specifying an escape character. This is done by using the keyword ESCAPE after your pattern. The literal value of the wildcard character following the escape value will be used instead of the wildcard value. In the example below, we specify that ! is our ESCAPE character in our string. Then we put ! before %. That way the database will look for the literal value of % instead of using that in the wildcard pattern.

SELECT	SaleDescription
FROM	SaleInfo
WHERE	SaleDescription LIKE '60!% off sale%' ESCAPE '!'

 
What Is An Escape Character

An escape character is a character that is placed before a character (or string of characters) and it tells the system to read the following character as their literal value. Many systems have reserved characters that act as codes or wildcards in their system. Using an escape character, you tell the system to not read those values as special codes or wildcards.

 
Reference: https://msdn.microsoft.com/en-us/library/aa933232(v=sql.80).aspx

If Statement In An Update

Facebooktwitterredditpinterestlinkedinmail

If you are updating multiple rows in one statement, but only want to update a value in a column if a condition is satisfied, then you need to perform some sort of if logic inside your update. Here’s a straight forward way of doing this.

In the example below, I am just going to use 2 temp table variables. I’ll update the @SourceTable with the data from the @UpdateTable… but only where the UpdateFlag field is set to 1.

DECLARE @SourceTable TABLE
(AnimalID INT,
 AnimalName VARCHAR(255))
 
DECLARE @UpdateTable TABLE
(AnimalID INT,
 UpdateToValue VARCHAR(255),
 UpdateFlag  BIT)
 
INSERT
INTO   @SourceTable
VALUES (1, 'Dog'),
       (2, 'Cat'),
       (3, 'Snake')
 
INSERT
INTO    @UpdateTable
VALUES  (1, 'Pig', 1),
        (2, 'Cow', 0),
        (3, 'Turkey', 1)

UPDATE  S
SET     S.AnimalName =	CASE WHEN U.UpdateFlag = 1 THEN U.UpdateToValue 
							 ELSE S.AnimalName 
						END
FROM    @SourceTable S
INNER   JOIN @UpdateTable U ON U.AnimalID = S.AnimalID

SELECT *
FROM   @SourceTable S

 
In the example above, you can see that the AnimalName is only updated when the UpdateFlag column is set to 1. The 2nd record “Cat” is not updated with the value “Cow” because it has a 0 for the UpdateFlag. This is done using a case statement on the SET operation. The example above is very simple. This case logic in the SET operation becomes much more helpful in more complex queries.

SQL Server Management Studio Dark Theme

Facebooktwitterredditpinterestlinkedinmail

If you like the dark theme of Visual Studio and wish that you had the dark theme for SQL Server Management Studio (SSMS), look no further.  Management Studio actually comes with a dark theme, but it just isn’t enabled.  It’s super simple to enable this feature.  Here’s how.

Note: I’ve confirmed this works with SQL Server 2016.

Here is what my current themed Management Studio looks like:
SQL Server Management Studio Dark Theme Light

 

The place in SQL Server Management Studio that we set the color theme is in the Options.  To see what we currently have, you go to the options by clicking Tools –> Options…
SQL Server Management Studio Dark Theme Options

From there we navigate to Environment –> General.  On the right side we can see Color theme:.  This option shows the current color theme that you are SSMS is using.
SQL Server Management Studio Dark Theme Options

My only options in the drop-down are Blue and Light:
SQL Server Management Studio Dark Theme Options

To allow the Dark theme to be selected, all we need to do is make a small change to the ssms.pkgundef file.  This file is located at: “C:\program files (x86)\Microsoft SQL Server\130\tools\binn\managementstudio\ssms.pkgundef”.

 

Step 1) Close SQL Server Management Studio and then browse to the ssms.pkgundef file and open it with Notepad.  The easiest way I found is to right click on the file and choose Edit.

TIP: If you can’t save the file after editing it, you may need to launch Notepad as administrator.

SQL Server Management Studio Dark Theme Edit

Step 2) Find the // Remove Dark theme key.  The easiest way is to just do a search in Notepad.
SQL Server Management Studio Dark Theme Edit

Step 3) Add 2 slashes (//) in front of this key that starts with [$RootKey$\Themes\.  Save and close the file.
SQL Server Management Studio Dark Theme Edit

Step 4) Relaunch SQL Server Management Studio.  Go back in to the options and select the Dark theme.
SQL Server Management Studio Dark Theme Done

This is what your final product should look like!  Enjoy!
SQL Server Management Studio Dark Theme Dark