<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>TSQL Archives - DBAduck</title>
	<atom:link href="https://www.dbaduck.com/category/tsql/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.dbaduck.com/category/tsql/</link>
	<description>DBA</description>
	<lastBuildDate>Wed, 18 May 2022 18:50:16 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://www.dbaduck.com/wp-content/uploads/2013/12/cropped-dbaducklogo-2-32x32.png</url>
	<title>TSQL Archives - DBAduck</title>
	<link>https://www.dbaduck.com/category/tsql/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How is the pandemic going for you? (T-SQL Tuesday #132)</title>
		<link>https://www.dbaduck.com/how-is-the-pandemic-going-for-you-t-sql-tuesday-132/</link>
					<comments>https://www.dbaduck.com/how-is-the-pandemic-going-for-you-t-sql-tuesday-132/#comments</comments>
		
		<dc:creator><![CDATA[dbaduck]]></dc:creator>
		<pubDate>Tue, 10 Nov 2020 18:26:26 +0000</pubDate>
				<category><![CDATA[TSQL]]></category>
		<category><![CDATA[TSQLTuesday]]></category>
		<category><![CDATA[sqlfamily tsqltuesday]]></category>
		<guid isPermaLink="false">https://dbaduck.com/?p=4614</guid>

					<description><![CDATA[<p>At the first of this year, things were going well. Just before the pandemic was in full swing, the company I worked for merged with another company. This added a lot of extra things and stress to the current situation and I found that it was a welcome blessing to work from home all the [&#8230;]</p>
<p>The post <a href="https://www.dbaduck.com/how-is-the-pandemic-going-for-you-t-sql-tuesday-132/">How is the pandemic going for you? (T-SQL Tuesday #132)</a> appeared first on <a href="https://www.dbaduck.com">DBAduck</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>At the first of this year, things were going well. Just before the pandemic was in full swing, the company I worked for merged with another company. This added a lot of extra things and stress to the current situation and I found that it was a welcome blessing to work from home all the time. This post is about what I learned and one of the things that I will be working on during this time to keep myself in a better place.</p>



<figure class="wp-block-image size-large"><a href="https://sqlworldwide.com/t-sql-tuesday-132-how-are-you-coping-with-pandemic/"><img fetchpriority="high" decoding="async" width="250" height="250" src="https://dbaduck.com/wp-content/uploads/2020/06/t-sql-tuesday-250x250-1.png" alt="" class="wp-image-4154" srcset="https://www.dbaduck.com/wp-content/uploads/2020/06/t-sql-tuesday-250x250-1.png 250w, https://www.dbaduck.com/wp-content/uploads/2020/06/t-sql-tuesday-250x250-1-150x150.png 150w" sizes="(max-width: 250px) 100vw, 250px" /></a></figure>



<p class="has-medium-font-size"><strong>What I realized at the onset of the pandemic</strong></p>



<p>I thought that it would be the best thing ever to be able to work from home and never go into the office. In the beginning it was great as I loved to work from home anyway because I got more done and drivebys were not prevalent. What I realized is that the interaction with the team was really nice. To keep up with things and talk with people physically was more important than I thought.  I also found out that I was perfectly OK without interaction while working and that I was more of an introvert than I thought.  But now I did not get to travel to SQL Saturday&#8217;s and speak, I did not go to the User Group physically and catch up with the members.  So while I loved being in my home-office and was perfectly content, I was not perfectly content. I had a side of me that wanted to interact and see the whites of peoples eyes and present in front of. In the end I wanted to be with the #SQLFamily more than I thought I would.</p>



<p class="has-medium-font-size"><strong>Health Ramifications</strong></p>



<p>I don&#8217;t think I take care of myself as much as when I was in the office. I eat worse and I sit all day and find myself having sat for hours on end without drinking water or eating anything. I don&#8217;t think that I am in better health than before the pandemic. I weigh more and I have not developed good habits as I should. So I have to start thinking about long term and not short term in my health. I am grateful to have been shown this by the pandemic, I know I cannot keep doing what I have been doing and be OK. I am beginning to put together a plan to get more healthy and develop good habits around exercise, good office practices (taking breaks, getting up from the chair, standing for a bit) so that I can be more healthy and happy, less stress and a good outlet to the daily grind.</p>



<p class="has-medium-font-size"><strong>My Advice to others</strong></p>



<p>Really it is about finding out who you are and what you want to be. To be of help to your colleagues you need to keep up with yourself and have something to offer. I have given all my time, effort and really everything to everyone else, and I love to do it because growing is very important in keeping your mental health.  If you do not learn and grow, you stagnate which means that you start to feel like you don&#8217;t have anything to offer and if that is a pattern every day, then it will be really detrimental a month from now, because the frog doesn&#8217;t get boiled overnight, it takes a slow rise of temperature and the frog stays in the water until the heat is so much that he cannot get out of the water because of no strength and then he boils.  Don&#8217;t let the temperature of your water slowly cook you to death, recognize right now that you are in that pot of water and you want to ensure that you get out on dry land to make a difference.  Give a little and then give a little more, but remember to give to yourself at the same time. Mostly so that you can thrive, grow and learn the things that make you, you. Take care of yourself through this pandemic and remember that you are a great part of our #SQLFamily and we need you to be complete. Stay safe and see you out there.</p>
<p>The post <a href="https://www.dbaduck.com/how-is-the-pandemic-going-for-you-t-sql-tuesday-132/">How is the pandemic going for you? (T-SQL Tuesday #132)</a> appeared first on <a href="https://www.dbaduck.com">DBAduck</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbaduck.com/how-is-the-pandemic-going-for-you-t-sql-tuesday-132/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>SQL Server TDE on Mirror and Log Shipping</title>
		<link>https://www.dbaduck.com/sql-server-tde-on-mirror-and-log-shipping/</link>
					<comments>https://www.dbaduck.com/sql-server-tde-on-mirror-and-log-shipping/#comments</comments>
		
		<dc:creator><![CDATA[dbaduck]]></dc:creator>
		<pubDate>Thu, 25 Jun 2015 03:09:45 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[TSQL]]></category>
		<category><![CDATA[LogShipping]]></category>
		<category><![CDATA[Mirroring]]></category>
		<category><![CDATA[TDE]]></category>
		<guid isPermaLink="false">http://dbaduck.wpengine.com/?p=206</guid>

					<description><![CDATA[<p>Today in our Virtual Chapter meeting for VCDBA, I presented on SQL Server Encryption including TDE.&#160; The question was posed as to whether or not the TDE would take affect if the mirror was established first and then you enabled TDE on the database.&#160; I was not sure at the time only because I had [&#8230;]</p>
<p>The post <a href="https://www.dbaduck.com/sql-server-tde-on-mirror-and-log-shipping/">SQL Server TDE on Mirror and Log Shipping</a> appeared first on <a href="https://www.dbaduck.com">DBAduck</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Today in our Virtual Chapter meeting for VCDBA, I presented on SQL Server Encryption including TDE.&nbsp; The question was posed as to whether or not the TDE would take affect if the mirror was established first and then you enabled TDE on the database.&nbsp; I was not sure at the time only because I had not done any tests, so I did not want to say yes or no unless I could substantiate it.</p>
<p>I set off to do some tests.&nbsp; First I created a Mirrored database using the demo code from the VC presentation.</p>
<div id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:c8ec039b-fcac-4b91-b8ee-6dc92c5637df" class="wlWriterEditableSmartContent" style="float: none; margin: 0px; display: inline; padding: 0px;">
[code lang=&#8221;sql&#8221;]<br />
use master<br />
GO<br />
CREATE DATABASE TESTTDE<br />
ON  PRIMARY<br />
(<br />
NAME = N&#8217;TESTTDE&#8217;,<br />
FILENAME = N&#8217;C:\sqldata\S1\TESTTDE.mdf&#8217; ,<br />
SIZE = 10240KB ,<br />
FILEGROWTH = 10240KB<br />
)<br />
LOG ON<br />
(<br />
NAME = N&#8217;TESTTDE_log&#8217;,<br />
FILENAME = N&#8217;C:\sqldata\S1\TESTTDE_log.ldf&#8217; ,<br />
SIZE = 10240KB ,<br />
FILEGROWTH = 10240KB<br />
)<br />
GO<br />
[/code]</div>
<p>Now once the database is created on instance 1 I backed up the database and a log backup. Then restored it to the second instance with it’s log with no recovery. Once it was primed and ready, I established a mirror and saw the database was synchronized.&nbsp; Now the trick is to use create the components to establish the <a href="https://msdn.microsoft.com/en-us/library/ms189586.aspx" target="_blank" rel="noopener noreferrer">Encryption Hierarchy</a>.</p>
<div id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:ce63dc46-9c7d-4edc-b09e-5b8bc943777e" class="wlWriterEditableSmartContent" style="float: none; margin: 0px; display: inline; padding: 0px;">
[code lang=&#8221;sql&#8221;]<br />
USE master;<br />
GO<br />
CREATE MASTER KEY<br />
ENCRYPTION BY PASSWORD = &#8216;INSTANCE1_Use1Strong2Password3Here!&#8217;;<br />
go</p>
<p>CREATE CERTIFICATE MyServerCert<br />
WITH SUBJECT = &#8216;My DEK Certificate&#8217;<br />
GO</p>
<p>[/code]</p>
</div>
<p>Now that we have the MASTER KEY and the Certificate in place, you need to backup the certificate to a set of files and restore it to the second instance.</p>
<div id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:a2009ef1-4b56-4f5c-8631-bdfbb861a34e" class="wlWriterEditableSmartContent" style="float: none; margin: 0px; display: inline; padding: 0px;">
[code lang=&#8221;sql&#8221;]<br />
BACKUP CERTIFICATE MyServerCert<br />
TO FILE=&#8217;C:\SQLBACKUP\MyserverCert.cer&#8217;<br />
WITH PRIVATE KEY (FILE=&#8217;C:\SQLBACKUP\MyServerCert.pvk&#8217;,<br />
ENCRYPTION BY PASSWORD=&#8217;My1Secure2Password!&#8217;)<br />
GO<br />
[/code]</div>
<p>NOW CHANGE TO THE SECOND INSTANCE</p>
<div id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:a1f34238-d061-48ac-8a5a-8b640128a001" class="wlWriterEditableSmartContent" style="float: none; margin: 0px; display: inline; padding: 0px;">
[code lang=&#8221;sql&#8221;]<br />
&#8212; Make sure that you change to the second instance</p>
<p>USE master;<br />
GO<br />
CREATE MASTER KEY<br />
ENCRYPTION BY PASSWORD = &#8216;INSTANCE2_Use1Strong2Password3Here!&#8217;;<br />
GO<br />
CREATE CERTIFICATE MyServerCert<br />
FROM FILE = &#8216;C:\SQLBACKUP\MyServerCert.cer&#8217;<br />
WITH PRIVATE KEY (FILE = &#8216;C:\SQLBACKUP\MyServerCert.pvk&#8217;,<br />
DECRYPTION BY PASSWORD = &#8216;My1Secure2Password!&#8217;);<br />
GO<br />
[/code]</p>
</div>
<p>Now that you have the Encryption Hierarchy established for both instances, let’s encrypt the database.</p>
<div id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:d96439b5-5286-402d-a373-773014e5564f" class="wlWriterEditableSmartContent" style="float: none; margin: 0px; display: inline; padding: 0px;">
[code lang=&#8221;sql&#8221;]<br />
&#8212; Make sure that you are in the Principal instance<br />
&#8212; or this will not work because you won&#8217;t be able<br />
&#8212; to USE TESTTDE on the Mirror</p>
<p>USE TESTTDE<br />
GO</p>
<p>CREATE DATABASE ENCRYPTION KEY<br />
WITH ALGORITHM = AES_128<br />
ENCRYPTION BY SERVER CERTIFICATE MyServerCert;<br />
GO</p>
<p>ALTER DATABASE TESTTDE<br />
SET ENCRYPTION ON;<br />
GO<br />
[/code]</p>
</div>
<p>What will happen in the Mirror is that it will encrypt the database, even though you won’t be able to see it.&nbsp; When it is fully encrypted, and you fail the database over in the Mirror, it will indicate that the database is encrypted. On the Principal you can see it with the query below.</p>
<div id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:dd14bd17-926c-4543-b818-1af33a8fc5c7" class="wlWriterEditableSmartContent" style="float: none; margin: 0px; display: inline; padding: 0px;">
[code lang=&#8221;sql&#8221;]<br />
SELECT<br />
db.name,<br />
db.is_encrypted,<br />
dm.encryption_state,<br />
dm.percent_complete,<br />
dm.key_algorithm,<br />
dm.key_length<br />
FROM<br />
sys.databases db<br />
LEFT OUTER JOIN sys.dm_database_encryption_keys dm<br />
ON db.database_id = dm.database_id;&lt;/pre&gt;<br />
[/code]</p>
<h2>Now for the Log Shipping copy</h2>
<p>To establish the test for the Log Shipping, do the same as you did for the mirror but restore the Full, then the Log with Standby so that you can see that it flags it as encrypted.&nbsp; First establish the Log Shipping without encryption set up, and once you are in Standby on the Shipped side, you can use the existing Certificate to test the idea of encryption afterwards.</p>
<p>Steps:</p>
<ol>
<li>CREATE DATABASE</li>
<li>SET Recovery Model to Full</li>
<li>Backup Database</li>
<li>Backup Log</li>
<li>Change to second instance</li>
<li>RESTORE DATABASE with NORECOVERY</li>
<li>RESTORE LOG WITH STANDBY</li>
<li>Change to first instance</li>
<li>USE TESTTDELOGSHIPPING</li>
<li>CREATE DATABASE ENCRYPTION KEY</li>
<li>ALTER DATABASE TESTTDELOGSHIPPING SET ENCRYPTION ON</li>
<li>BACKUP LOG</li>
<li>Change to second instance</li>
<li>RESTORE LOG TESTTDELOGSHIPPING WITH STANDBY</li>
<li>Run the above query to see if the database is encrypted. You will see is_encrypted = 1</li>
</ol>
<p>This shows that the Mirroring and Log Shipping processes are affected by the Database Encryption Process.</p>
<p>Happy Encrypting….!</p>
</div>
<p>The post <a href="https://www.dbaduck.com/sql-server-tde-on-mirror-and-log-shipping/">SQL Server TDE on Mirror and Log Shipping</a> appeared first on <a href="https://www.dbaduck.com">DBAduck</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbaduck.com/sql-server-tde-on-mirror-and-log-shipping/feed/</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
			</item>
		<item>
		<title>sys.dm_db_index_physical_stats Easter Egg</title>
		<link>https://www.dbaduck.com/sys-dm_db_index_physical_stats-easter-egg/</link>
					<comments>https://www.dbaduck.com/sys-dm_db_index_physical_stats-easter-egg/#respond</comments>
		
		<dc:creator><![CDATA[dbaduck]]></dc:creator>
		<pubDate>Fri, 08 May 2015 04:42:56 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[TSQL]]></category>
		<category><![CDATA[Fragmentation]]></category>
		<category><![CDATA[Index]]></category>
		<guid isPermaLink="false">http://dbaduck.wpengine.com/?p=193</guid>

					<description><![CDATA[<p>I was reading through documentation on the DMV sys.dm_db_index_physical_stats and found that there are a couple of things you need to be aware of when using this DMV.  First, we know that there are parameters for the DMV. Parameters Database Id Object ID Index Id Partition Id Level of interrogation (DEFAULT, LIMITED, DETAILED, SAMPLED, NULL [&#8230;]</p>
<p>The post <a href="https://www.dbaduck.com/sys-dm_db_index_physical_stats-easter-egg/">sys.dm_db_index_physical_stats Easter Egg</a> appeared first on <a href="https://www.dbaduck.com">DBAduck</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>I was reading through documentation on the DMV <a href="https://msdn.microsoft.com/en-us/library/ms188917.aspx" target="_blank" rel="noopener noreferrer">sys.dm_db_index_physical_stats</a> and found that there are a couple of things you need to be aware of when using this DMV.  First, we know that there are parameters for the DMV.</p>
<h2><strong>Parameters</strong></h2>
<ul>
<li>Database Id</li>
<li>Object ID</li>
<li>Index Id</li>
<li>Partition Id</li>
<li>Level of interrogation (DEFAULT, LIMITED, DETAILED, SAMPLED, NULL = LIMITED).</li>
</ul>
<p>The interesting thing is that when any of the first 4 parameters are NULL, the default is to evaluate ALL of the items in that parameter.  For example, if you specify NULL in the Database ID then it will evaluate ALL databases in the server. If Object ID is NULL then it will evaluate all objects.  So I knew all that, but here is what I did not realize, quoting from the documentation:</p>
<p>“<em>passing values that are not valid to these functions may cause unintended results. For example, if the database or object name cannot be found because they do not exist or are spelled incorrectly, both functions will return NULL. The sys.dm_db_index_physical_stats function interprets NULL as a wildcard value specifying all databases or all objects</em>.”</p>
<p>“<em>Additionally, the <strong>OBJECT_ID function is processed before the sys.dm_db_index_physical_stats function is called</strong> and is therefore evaluated in the context of the current database, not the database specified in database_id. This behavior may cause the OBJECT_ID function to return a NULL value; or, if the object name exists in both the current database context and the specified database, an error message may be returned. The following examples demonstrate these unintended results</em>.”</p>
<p>Now that is nice, but here is what it means. If you are specifying a database id for use in this function and the database id is not the “<em>current</em>” database, then if the OBJECT_ID() function is used, it will evaluate it in the “<em>current”</em> database and NOT the database that is represented by the database id in the statement. If the object does not exist in the database, then it will result in NULL and will then proceed to do an evaluation on all objects in the database represented by the database id.</p>
<p>The other caveat is that if in the current database there is an Object by that name that results in an object id being returned, when the function begins, it will not be able to find the object id returned and will actually error. Take a look at the examples below and notice that you will want to be in the database that you intend to use if you are specifying anything beyond the database id. Don’t get caught with NULL in the statement without you knowing about it.</p>
<p>&nbsp;</p>
<p>[code lang=&#8221;SQL&#8221;]use master<br />
GO<br />
SELECT *<br />
FROM sys.dm_db_index_physical_stats(DB_ID(&#8216;AdventureWorks2012&#8217;), OBJECT_ID(&#8216;Person.Person&#8217;), NULL, NULL, &#8216;LIMITED&#8217;)</p>
<p>&#8212; Results were for 190 rows<br />
/*<br />
** What happens here is that it evaluates OBJECT_ID(&#8216;Person.Person&#8217;) to NULL<br />
** because it does not exist in master as shown below.<br />
*/</p>
<p>use master<br />
GO<br />
SELECT OBJECT_ID(&#8216;Person.Person&#8217;)<br />
&#8212; Result is NULL<br />
GO<br />
use AdventureWorks2012<br />
GO<br />
SELECT OBJECT_ID(&#8216;Person.Person&#8217;)<br />
&#8212; Result is 1765581328</p>
<p>/*<br />
** Let&#8217;s create a table in master of the same name<br />
** as in the AdventureWorks2012 database.<br />
*/</p>
<p>use master<br />
GO<br />
CREATE SCHEMA Person AUTHORIZATION dbo<br />
GO<br />
CREATE TABLE Person.Person (id int)<br />
GO<br />
SELECT *<br />
FROM sys.dm_db_index_physical_stats(DB_ID(&#8216;AdventureWorks2012&#8217;), OBJECT_ID(&#8216;Person.Person&#8217;), NULL, NULL, &#8216;LIMITED&#8217;)<br />
GO<br />
&#8212; You get the error below<br />
/*<br />
Msg 2501, Level 16, State 40, Line 3<br />
Cannot find a table or object with the name &#8216;.StateProvinceCountryRegion&#8217;. Check the system catalog.<br />
*/<br />
[/code]</p>
<p>The post <a href="https://www.dbaduck.com/sys-dm_db_index_physical_stats-easter-egg/">sys.dm_db_index_physical_stats Easter Egg</a> appeared first on <a href="https://www.dbaduck.com">DBAduck</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbaduck.com/sys-dm_db_index_physical_stats-easter-egg/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
