<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="https://www.w3.org/2005/Atom" xmlns:sy="https://purl.org/rss/1.0/modules/syndication/" xmlns:dc="https://purl.org/dc/elements/1.1/" xmlns:content="https://purl.org/rss/1.0/modules/content/" xmlns:wfw="https://wellformedweb.org/CommentAPI/" xmlns:slash="https://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>Stefano Altamura’s Blog</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/staltam/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/staltam/feed.xml</link>
    <description>Go with the data flow</description>
    <lastBuildDate>Tue, 16 Apr 2019 05:31:06 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>Hey DBAs! Powershell is your friend!</title>
      <link>https://docs.microsoft.com/archive/blogs/staltam/hey-dbas-powershell-is-your-friend</link>
      <pubDate>Fri, 11 May 2018 13:39:58 GMT</pubDate>
      <dc:creator><![CDATA[Stefano Altamura]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/staltam/?p=125</guid>
      <description><![CDATA[In the last few months I supported several clients to upgrade their “old” SQL Server instances to...]]></description>
      <content:encoded><![CDATA[In the last few months I supported several clients to upgrade their “old” SQL Server instances to the last version. I really like the side by side approach for several reasons, top two are the simple rollback procedure and the chance to clean some unsued objects like old logins, unused jobs. I know that DBAs usually keep something that is not used anymore just because “you’ll never know, just in case” 😊.

The last part of the upgrade process is usually a backup on the old instance and a restore on the new one for each database. This part is usually simple, but when you have lots of DBs, doing the same task lot of times could lead to errors, that’s why I like to have things scripted.

I noticed that lot of DBAs wrote down T-SQL Scripts, but again this could lead to a pains: you have to bounce from one query window to another or even worst from one RDP session to another.

What I suggest here is to use PowerShell, using <strong>sqlps module.</strong>

Here’s the links to the documentation.

<a href="https://docs.microsoft.com/en-us/powershell/module/sqlps/backup-sqldatabase?view=sqlserver-ps">https://docs.microsoft.com/en-us/powershell/module/sqlps/backup-sqldatabase?view=sqlserver-ps</a>

<a href="https://docs.microsoft.com/en-us/powershell/module/sqlps/restore-sqldatabase?view=sqlserver-ps">https://docs.microsoft.com/en-us/powershell/module/sqlps/restore-sqldatabase?view=sqlserver-ps</a>

It’s easy to set up a command like this:

&nbsp;

<script src="https://gist.github.com/Stephano288/1d234bea8273e0db243fb79c6611493d.js"></script>

&nbsp;

and restore the database using something like this:

&nbsp;

<script src="https://gist.github.com/Stephano288/440b8318711c26ee831c1c9e21b7b2e3.js"></script>

In this way I can backup and restore databases from one instance to the other in a single place: my PowerShell window.

From this point, if you know a little PowerShell, it’s really easy to let PowerShell generates the scripts for you:

&nbsp;

<script src="https://gist.github.com/Stephano288/6e7fa769995a230ed248d72d2f8d913f.js"></script>

&nbsp;

Really simple: a query to retrieve all databases, a loop to make substitutions in commands and place them in a powershell script.

The result will look like this:

<img width="1920" height="519" class="alignnone size-full wp-image-165" alt="" src="https://msdnshared.blob.core.windows.net/media/2018/05/scriptresult.png" />

&nbsp;

There are lots of Cmdlets in SQLPS, almost everything that you can do in SQL Server Management Studio can be done also in PowerShell.

Take a look at the documentation:

<a href="https://docs.microsoft.com/en-us/powershell/module/sqlps/?view=sqlserver-ps">https://docs.microsoft.com/en-us/powershell/module/sqlps/?view=sqlserver-ps</a>

Have Fun.

Stefano

&nbsp;

<strong>Disclaimer:</strong> I just want to show an alternative to T-SQL. The commands and the scripts listed above are just for learning purpose, every environment needs a specific assessment. Lots of things needs to be considered. In other words: don’t run them on production environments!

&nbsp;]]></content:encoded>
    </item>
    <item>
      <title>SQL Server Updates behavior</title>
      <link>https://docs.microsoft.com/archive/blogs/staltam/sql-server-updates-behavior</link>
      <pubDate>Thu, 27 Jul 2017 08:46:11 GMT</pubDate>
      <dc:creator><![CDATA[Stefano Altamura]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/staltam/?p=77</guid>
      <description><![CDATA[Recently a client asked me:

“We want to place all the fields of the table in the SET clause of an...]]></description>
      <content:encoded><![CDATA[Recently a client asked me:

“We want to place all the fields of the table in the SET clause of an UPDATE, no matter if the values are changed or not. How does it impact the I/O? Does SQL Server update every field, even if the old value matches the new one? What about indexes?” It’s a common scenario: the application loads lots of data in a form, then a user change from one value up to every value in the form, then saves the changes. They want to avoid checking which field was updated on client side and deal with complex T-SQL underneath. They want to update everything.

Makes sense. Let’s start digging!

I need to create a table with a bunch of rows and a few indexes
<script src="https://gist.github.com/anonymous/9411e4d6f3a20bc1d88b384b7f305d05.js"></script>
Then write a few updates queries:

<script src="https://gist.github.com/anonymous/c36eb48f3be635419196277907c60b40.js"></script>

&nbsp;

A good starting point is the queries execution plans.

Here's how a couple of execution plans look like from my update queries:

<img width="764" height="200" class="alignnone wp-image-85" alt="" src="https://msdnshared.blob.core.windows.net/media/2017/07/ExecutionPlan.png" /> <img width="767" height="207" class="alignnone wp-image-95" alt="" src="https://msdnshared.blob.core.windows.net/media/2017/07/ExecutionPlan2.png" />

The execution plan is computed before the actual query execution and does not take in account if the fields are to update or not. But in the execution plans there are some “<strong>compute scalar</strong>” operators. <span style="text-decoration: underline">Maybe</span> SQL Server use them to check if the field need to be really updated or not, at least I have a clue.

A closer look to the buffer pool will help me to find the answer, here the DMV <a href="https://docs.microsoft.com/en-us/sql/relational-databases/system-dynamic-management-views/sys-dm-os-buffer-descriptors-transact-sql">sys.dm_os_buffer_descriptors</a> comes handy.

This DMV returns a description about all the pages that are in SQL Server Buffer pool. The keystone here is colum “is_modified”, that marks dirty pages. My plan is to run a “real” update query and a “matching” update query. After each run check what’s in the DMV.

This is the query that I use in this case to check the status of the pages in the buffer pool.
<script src="https://gist.github.com/anonymous/c6df4c7a40f0e45a8871b785df7cbb5d.js"></script>

To be sure that pages are clean, I will issue a checkpoint before each test..

If i run a "not" update query, pages in the buffer pool stay clean.<img width="1000" height="421" class="alignnone size-full wp-image-105" alt="" src="https://msdnshared.blob.core.windows.net/media/2017/07/CleanBuffer1.png" />

If I run a "real" update query lots of pages in the buffer pool now are dirty:

<img width="1110" height="452" class="alignnone size-full wp-image-115" alt="" src="https://msdnshared.blob.core.windows.net/media/2017/07/DirtyBuffer.png" />

SQL Server is always efficient! Sadly this not means that your I/O is safe.
If you look closer to the execution plans, you will see that SQL Server Engine needs to check if an update is "real" or not, the only way to do it is to perform some reads.
If the plan uses a non-clustered index that does not cover the query a lookup in the cluster index is needed.

Always check your execution plan to avoid expensive read operations!

&nbsp;

Have Fun

Stefano

&nbsp;

PS: I want to thank my colleague, mentor and dear friend <a href="https://blogs.msdn.microsoft.com/frcogno/">Francesco </a>(check out his blog!) for the wonderful discussion that we had about this topic and the ways to demonstrate it. This is only a quick and simple method!]]></content:encoded>
    </item>
    <item>
      <title>How to Disable Always Encrypted Feature in SQL Server 2016.</title>
      <link>https://docs.microsoft.com/archive/blogs/staltam/how-to-disable-always-encrypted-feature</link>
      <pubDate>Wed, 05 Apr 2017 17:28:46 GMT</pubDate>
      <dc:creator><![CDATA[Stefano Altamura]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/staltam/?p=45</guid>
      <description><![CDATA[Hello, my name is Stefano Altamura and I’m an Italian PFE dedicated to SQL Server and Data...]]></description>
      <content:encoded><![CDATA[Hello, my name is Stefano Altamura and I’m an Italian PFE dedicated to SQL Server and Data Insights.
In my first post I want to talk about an approach that I often use to better understand how new features works.

A wonderful new feature in SQL Server 2016 is Always Encrypted. You can find lots of details here: <a href="https://msdn.microsoft.com/en-us/library/mt163865.aspx">https://msdn.microsoft.com/en-us/library/mt163865.aspx</a>.

I created a little demo to show Always Encrypted to my clients.
After that I want to “disable” the feature and bring back my database to the original state.
SQL Server Management Studio provides a way to decrypt the data, but i realize how to do it only after some time.

My first thought was: let’s create a new table with the same schema, load the plain text data in the new table, drop the crypted version and rename the plain text version. An inelegant solution. I don’t like it and I don't want to use it!

I need a different approach.

Always Encrypted wizard offers the chance to generate a Powershell Script to set up the encryption to run it in a different moment.

This is what SQL Server Management Studio generates for my database:

&nbsp;

<script src="https://gist.github.com/anonymous/5249fe001ab2bba6c7a855a573663e06.js"></script>

&nbsp;

The 2 more interesting lines are the last cmdlets at line 18 and 19:

The first command adds to the variable <em><strong>$encryptionChanges</strong> </em>the encryption setting for the field <em>CreditCardNumber</em> on <em>MyTargetTable</em>.

The second cmdlet apply the settings on my database.

That's the way I like it!

A couple of changes in the code and I can decypher the column and have it in plain text:

<script src="https://gist.github.com/anonymous/ebf32791f5fcd32641ab6aa4665a2c81.js"></script>

I was happy about the result, but I was struggling how to accomplish the task using SQL Server Management Studio.
The answer is very simple: run again the Encryption Wizard and set the "Encryption Type" to plaintext.

&nbsp;

<img width="760" height="781" class="alignnone wp-image-56" alt="always-encrypted" src="https://msdnshared.blob.core.windows.net/media/2017/04/Always-Encrypted1.png" />

&nbsp;

Long story short:
If you cannot find a way, always remember that the "Script" button can help you!

&nbsp;

&nbsp;

Have fun!

&nbsp;

Stefano]]></content:encoded>
    </item>
  </channel>
</rss>