<?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>SQL Script Library Archives - The SERO Group</title>
	<atom:link href="https://theserogroup.com/tag/sql-script-library/feed/" rel="self" type="application/rss+xml" />
	<link>https://theserogroup.com/tag/sql-script-library/</link>
	<description>SQL Servers Healthy, Secure, And Reliable</description>
	<lastBuildDate>Mon, 10 Nov 2025 21:51:19 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>

<image>
	<url>https://theserogroup.com/wp-content/uploads/2024/07/cropped-Canister-only-1-32x32.png</url>
	<title>SQL Script Library Archives - The SERO Group</title>
	<link>https://theserogroup.com/tag/sql-script-library/</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">121220030</site>	<item>
		<title>How to Enable Query Store in SQL Server: A Step-by-Step Guide</title>
		<link>https://theserogroup.com/sql-server/how-to-enable-query-store-in-sql-server-a-step-by-step-guide/</link>
		
		<dc:creator><![CDATA[Lee Markum]]></dc:creator>
		<pubDate>Wed, 12 Nov 2025 13:00:05 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Database Administration]]></category>
		<category><![CDATA[Database Development]]></category>
		<category><![CDATA[IT Manager]]></category>
		<category><![CDATA[Query Store]]></category>
		<category><![CDATA[Script Library]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Script Library]]></category>
		<category><![CDATA[SQL Server Management]]></category>
		<guid isPermaLink="false">https://theserogroup.com/?p=7614</guid>

					<description><![CDATA[<p>In my previous post about Query Store, I wrote about the four key benefits to enabling Query Store. Now that I&#8217;ve convinced you to turn it on, how do you do that? One thing to point out is that in SQL Server 2022 and above, when creating a new database from the SSMS GUI or&#8230; <br /> <a class="read-more" href="https://theserogroup.com/sql-server/how-to-enable-query-store-in-sql-server-a-step-by-step-guide/">Read more</a></p>
<p>The post <a href="https://theserogroup.com/sql-server/how-to-enable-query-store-in-sql-server-a-step-by-step-guide/">How to Enable Query Store in SQL Server: A Step-by-Step Guide</a> appeared first on <a href="https://theserogroup.com">The SERO Group</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>In <a href="https://theserogroup.com/dba/4-key-performance-benefits-of-enabling-query-store/" target="_blank" rel="noreferrer noopener">my previous post about Quer</a><a href="https://theserogroup.com/dba/4-key-performance-benefits-of-enabling-query-store/">y Store</a>, I wrote about the four key benefits to enabling Query Store. Now that I&#8217;ve convinced you to turn it on, how do you do that?</p>



<p>One thing to point out is that in SQL Server 2022 and above, when creating a new database from the SSMS GUI or by simply using the CREATE DATABASE MyNewDB syntax, the Query Store option will be on by default. For databases restored to SQL Server 2016 or later, the Query Store&#8217;s status from the original system will remain unchanged when the database is restored on the new instance.</p>



<p>Let&#8217;s go through the three ways to enable Query Store.</p>



<ol class="wp-block-list">
<li>Manually in SQL Server Management Studio</li>



<li>Using T-SQL</li>



<li>Using PowerShell</li>
</ol>



<h3 class="wp-block-heading" id="h-1-enabling-query-store-using-sql-server-management-studio">1. Enabling Query Store using SQL Server Management Studio:</h3>



<p>Since you’re likely already comfortable using SQL Server Management Studio for queries and database maintenance, SMSS does offer a convenient, familiar method for getting started with Query Store.</p>



<h4 class="wp-block-heading" id="h-steps-to-enable-query-store-using-ssms">Steps to Enable Query Store using SSMS</h4>



<ol class="wp-block-list">
<li>Connect to a SQL Server instance running SQL Server 2016 or higher.</li>



<li>Click the &#8216;+&#8217; sign next to the Databases folder to expand and see the list of databases.</li>



<li>Right-click on the database name and select &#8220;Properties.&#8221;</li>



<li>Left-click the &#8220;Query Store&#8221; option on the left-hand side of the GUI.</li>



<li>Change the Operation Mode(Requested) option from &#8220;Off&#8221; to &#8220;Read write.&#8221;</li>



<li>Click OK to apply the change and enable Query Store.</li>
</ol>



<h4 class="wp-block-heading" id="h-further-details">Further Details</h4>



<p>Here is what you will see after step 3. The Query Store option mentioned in step 4 is at the bottom of the list of options, like the below.</p>



<figure class="wp-block-image size-full"><a href="https://theserogroup.com/wp-content/uploads/2025/11/SelectingQueryStoreOptionInSelectAPage.png"><img decoding="async" width="172" height="216" src="https://theserogroup.com/wp-content/uploads/2025/11/SelectingQueryStoreOptionInSelectAPage.png" alt="" class="wp-image-7617"/></a></figure>



<p>Left-clicking that Query Store option will cause the below to show up on the right of the SSMS GUI.</p>



<p>What you see when you do that are the existing defaults on 2019 and above. If you are enabling Query Store on versions 2016 or 2017, you will want to adjust additional defaults. Prior to 2019, the default for &#8220;Query Store Capture Mode&#8221; was &#8220;All.&#8221; Change this option to &#8220;Auto&#8221; instead.</p>



<p>Furthermore, the default for Max_Storage_Size_MB was far too low in 2016 and 2017 and could be better in 2019 as well. This value represents the maximum amount of space that Query Store data will occupy in the database in which it was enabled. A good default value to start with is 2048 MB. It may be necessary to adjust that to 4096 MB at the high end in order to capture queries for the entire length of the &#8220;Stale Query Threshold (Days)&#8221; value.</p>



<p>The &#8220;Stale Query Threshold (Days)&#8221; option controls how many days of Query Store data will be kept. If the max storage size is set too low for a retention value of 30 days, then Query Store will start deleting collected data in the system tables to keep Query Store below the max storage size. This could result in having less data available than you intend for troubleshooting.</p>



<p>The rest of the defaults are acceptable and so could be left alone without concern.</p>



<figure class="wp-block-image size-full"><a href="https://theserogroup.com/wp-content/uploads/2025/11/QueryStore2019DefaultsInSSMS-1.png"><img fetchpriority="high" decoding="async" width="699" height="488" src="https://theserogroup.com/wp-content/uploads/2025/11/QueryStore2019DefaultsInSSMS-1.png" alt="" class="wp-image-7621" srcset="https://theserogroup.com/wp-content/uploads/2025/11/QueryStore2019DefaultsInSSMS-1.png 699w, https://theserogroup.com/wp-content/uploads/2025/11/QueryStore2019DefaultsInSSMS-1-300x209.png 300w" sizes="(max-width: 699px) 100vw, 699px" /></a></figure>



<h3 class="wp-block-heading" id="h-2-enabling-query-store-using-t-sql">2. Enabling Query Store using T-SQL</h3>



<p>The T-SQL language is, of course, the language of SQL Server. It is often more flexible than the SSMS GUI. Notice in the screenshot up above that there is a “Script” button. If you click that instead of clicking &#8220;ok&#8221; in the UI, then SQL Server will script out the options in the GUI into a query window. This will allow you to see what the GUI does. Using T-SQL, it is easier to enable Query Store on multiple databases. You can use a construct like sp_msforeachdb to enable Query Store for multiple databases at once.</p>



<pre class="wp-block-code"><code>USE &#91;master]
GO
ALTER DATABASE &#91;MyDB] SET QUERY_STORE = ON
GO
ALTER DATABASE &#91;MyDB] SET QUERY_STORE (OPERATION_MODE = READ_WRITE, MAX_STORAGE_SIZE_MB = 2048)
GO</code></pre>



<h3 class="wp-block-heading" id="h-3-enabling-query-store-using-powershell">3. Enabling Query Store using PowerShell</h3>



<p>Many accidental DBAs, those folks who were “voluntold” to start managing SQL Server, are network engineers, sysadmins, or cloud admins. Automation is often music to their ears, and in the Windows universe, PowerShell is a go-to method for automating tasks. Consequently, using PowerShell to automate the enabling of Query Store may feel natural to accidental DBAs. For the below command, the DBATools module will be needed in your environment.</p>



<p>Below is how Query Store could be enabled on all user databases on an instance of SQL Server. If you only want to enable Query Store on a few select databases on an instance, then add the -Database parameter with a comma-separated list of databases.</p>



<pre class="wp-block-code"><code>Set-DbaDbQueryStoreOption -SqlInstance ServerA -State ReadWrite ​

-FlushInterval 900 -CollectionInterval 60 -MaxSize 4096 ​

-CaptureMode Auto -CleanupMode Auto -StaleQueryThreshold 30, -WaitStatsCaptureMode ON</code></pre>



<p>Also, if your SQL Server environment has the Registered Server feature set up, then PowerShell can be used to read the servers registered there, loop over them, and enable Query Store on all user databases across your environment. This would be done using the Get-DbaRegServer command in the DBATools module.</p>



<h3 class="wp-block-heading" id="h-trace-flags-for-query-store">Trace Flags for Query Store</h3>



<p>If you aren’t familiar with Trace Flags, these are numbers that Microsoft uses to enable certain kinds of behavior in the database engine. They are occasionally meant to be short-term fixes, and later the functionality in a trace flag is built into how the SQL Server database engine works. This is the case for trace flags and Query Store. There are two trace flags to know about and enable. Notice that flag 7752 isn’t needed on SQL Server 2019 and above.</p>



<p>Trace Flag 7745—This prevents Query Store data from writing to disk prior to shutdown or failover process so it doesn’t delay a shutdown or failover.​</p>



<p>Trace Flag 7752 – Loads Query Store data to memory asynchronously from query execution. This default is built into the engine in SQL Server 2019.​</p>



<h3 class="wp-block-heading" id="h-want-to-work-with-the-sero-group">Want to Work With The SERO Group?</h3>



<p>Want to learn more about how The SERO Group helps organizations take the guesswork out of managing their SQL Servers? <a href="https://theserogroup.com/contact-us/" target="_blank" rel="noreferrer noopener">Schedule a no-obligation discovery call</a>&nbsp;with us to get started.</p>
<p>The post <a href="https://theserogroup.com/sql-server/how-to-enable-query-store-in-sql-server-a-step-by-step-guide/">How to Enable Query Store in SQL Server: A Step-by-Step Guide</a> appeared first on <a href="https://theserogroup.com">The SERO Group</a>.</p>
]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">7614</post-id>	</item>
		<item>
		<title>Error Msg 15138 The Database Principal Owns a Schema in the Database, and Cannot be Dropped</title>
		<link>https://theserogroup.com/dba/error-msg-15138-the-database-principal-owns-a-schema-in-the-database-and-cannot-be-dropped/</link>
					<comments>https://theserogroup.com/dba/error-msg-15138-the-database-principal-owns-a-schema-in-the-database-and-cannot-be-dropped/#comments</comments>
		
		<dc:creator><![CDATA[Joe Webb]]></dc:creator>
		<pubDate>Tue, 27 Jul 2021 19:43:29 +0000</pubDate>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Database Administration]]></category>
		<category><![CDATA[Database Development]]></category>
		<category><![CDATA[IT Manager]]></category>
		<category><![CDATA[Script Library]]></category>
		<category><![CDATA[Sero]]></category>
		<category><![CDATA[Sero Group]]></category>
		<category><![CDATA[Serogroup]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Assessment]]></category>
		<category><![CDATA[SQL Audit]]></category>
		<category><![CDATA[SQL Consultant]]></category>
		<category><![CDATA[SQL Script Library]]></category>
		<category><![CDATA[SQL Security]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server Consultant]]></category>
		<category><![CDATA[SQL Server Management]]></category>
		<category><![CDATA[The Sero Group]]></category>
		<guid isPermaLink="false">https://theserogroup.com/?p=3886</guid>

					<description><![CDATA[<p>You&#8217;re cleaning up some old usernames in a database. The users are no longer needed so you want to drop them and maybe even the server login. You issue the standard DROP USER username; command in the query editor and it immediately comes back with Msg 15138, Level 16, State 1, Line 1 The database&#8230; <br /> <a class="read-more" href="https://theserogroup.com/dba/error-msg-15138-the-database-principal-owns-a-schema-in-the-database-and-cannot-be-dropped/">Read more</a></p>
<p>The post <a href="https://theserogroup.com/dba/error-msg-15138-the-database-principal-owns-a-schema-in-the-database-and-cannot-be-dropped/">Error Msg 15138 The Database Principal Owns a Schema in the Database, and Cannot be Dropped</a> appeared first on <a href="https://theserogroup.com">The SERO Group</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>You&#8217;re cleaning up some old usernames in a database. The users are no longer needed so you want to drop them and maybe even the server login. You issue the standard DROP USER <em>username</em>; command in the query editor and it immediately comes back with <span class="has-inline-color has-vivid-red-color">Msg 15138, Level 16, State 1, Line 1 The database principal owns a schema in the database, and cannot be dropped.</span></p>



<figure class="wp-block-image size-large"><a href="https://theserogroup.com/wp-content/uploads/2021/07/dropusererror.png"><img decoding="async" width="1024" height="333" src="https://theserogroup.com/wp-content/uploads/2021/07/dropusererror-1024x333.png" alt="DROP USER SQL Server Error" class="wp-image-3864" srcset="https://theserogroup.com/wp-content/uploads/2021/07/dropusererror-1024x333.png 1024w, https://theserogroup.com/wp-content/uploads/2021/07/dropusererror-300x98.png 300w, https://theserogroup.com/wp-content/uploads/2021/07/dropusererror-768x250.png 768w, https://theserogroup.com/wp-content/uploads/2021/07/dropusererror.png 1366w" sizes="(max-width: 1024px) 100vw, 1024px" /></a></figure>



<p>The error message is telling us that the user owns a schema in this database. And as long as that&#8217;s the case, the user cannot be dropped. So, we need to determine which schema(s) are owned by the user.</p>



<h2 class="wp-block-heading">Who owns a schema? Finding the reason for Msg 15138</h2>



<p>There are a couple of easy ways to determine which schemas a user owns. You can view properties window for the user, or you can write a quick query. Let&#8217;s look at each method. </p>



<h3 class="wp-block-heading">Viewing Owned Schemas in the Properties window</h3>



<p>To see which schemas a user owns, drill down in the server, database, Security, Users in the connections pane of SQL Server Management Studio or Azure Data Studio. Right-click the user and select Properties. You&#8217;ll see the following Database User properties window open. Click on the Owned Schemas page. </p>



<p>In this example, Kim owns the db_ddladmin schema. That&#8217;s preventing you from dropping the user.</p>



<figure class="wp-block-image size-large"><a href="https://theserogroup.com/wp-content/uploads/2021/07/userpropertieswindow.png"><img loading="lazy" decoding="async" width="1024" height="858" src="https://theserogroup.com/wp-content/uploads/2021/07/userpropertieswindow-1024x858.png" alt="Viewing the database user properties window" class="wp-image-3865" srcset="https://theserogroup.com/wp-content/uploads/2021/07/userpropertieswindow-1024x858.png 1024w, https://theserogroup.com/wp-content/uploads/2021/07/userpropertieswindow-300x251.png 300w, https://theserogroup.com/wp-content/uploads/2021/07/userpropertieswindow-768x644.png 768w, https://theserogroup.com/wp-content/uploads/2021/07/userpropertieswindow-1288x1080.png 1288w, https://theserogroup.com/wp-content/uploads/2021/07/userpropertieswindow.png 1360w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></figure>



<h3 class="wp-block-heading">Using T-SQL to see the schemas owned by a user</h3>



<p>Not really a point-and-click kind of person? The following query will show you a list of the schemas owned by a specific user. Of course, you can omit the WHERE clause to see a list of all schemas and who owns them. Replace Kim with the user you&#8217;re interested in. </p>



<pre class="wp-block-code"><code>--what schemas does this user own?
SELECT SCHEMA_NAME, 
    SCHEMA_OWNER
FROM INFORMATION_SCHEMA.schemata
WHERE SCHEMA_OWNER = 'Kim';
</code></pre>



<p>The results are below.</p>



<figure class="wp-block-image size-large"><a href="https://theserogroup.com/wp-content/uploads/2021/07/SchemasOwnedByAUser-1.png"><img loading="lazy" decoding="async" width="558" height="194" src="https://theserogroup.com/wp-content/uploads/2021/07/SchemasOwnedByAUser-1.png" alt="Schemas Owned By A User" class="wp-image-3868" srcset="https://theserogroup.com/wp-content/uploads/2021/07/SchemasOwnedByAUser-1.png 558w, https://theserogroup.com/wp-content/uploads/2021/07/SchemasOwnedByAUser-1-300x104.png 300w" sizes="auto, (max-width: 558px) 100vw, 558px" /></a></figure>



<h2 class="wp-block-heading">Fixing the Msg 15138 The Database Principal Owns a Schema error</h2>



<p>Ok, we know the schema that&#8217;s preventing us from dropping the database. Now we need to fix the problem. To do that, we need to transfer the ownership of the schema to another user. As before there are a couple of ways to do this. </p>



<h3 class="wp-block-heading">Changing the schema owner using the Properties window</h3>



<p>To change the owner of a schema using SQL Server Management Studio or Azure Data Studio, use the connection list to drill down into the server, database, Security, Schemas. Right-click on the schema in question and select the Properties menu item.</p>



<p>On the General page, you&#8217;ll see the Schema Owner. Click Search to open a window that allows you to find another user. Enter the username you&#8217;d like to be the new owner, and click Check Names. Once, verified, click Ok, and then Ok again to close the Properties window. </p>



<figure class="wp-block-image size-large"><a href="https://theserogroup.com/wp-content/uploads/2021/07/changing_the_schema_owner_ssms.png"><img loading="lazy" decoding="async" width="1024" height="773" src="https://theserogroup.com/wp-content/uploads/2021/07/changing_the_schema_owner_ssms-1024x773.png" alt="changing the schema owner using ssms" class="wp-image-3888" srcset="https://theserogroup.com/wp-content/uploads/2021/07/changing_the_schema_owner_ssms-1024x773.png 1024w, https://theserogroup.com/wp-content/uploads/2021/07/changing_the_schema_owner_ssms-300x227.png 300w, https://theserogroup.com/wp-content/uploads/2021/07/changing_the_schema_owner_ssms-768x580.png 768w, https://theserogroup.com/wp-content/uploads/2021/07/changing_the_schema_owner_ssms-1536x1160.png 1536w, https://theserogroup.com/wp-content/uploads/2021/07/changing_the_schema_owner_ssms-1430x1080.png 1430w, https://theserogroup.com/wp-content/uploads/2021/07/changing_the_schema_owner_ssms.png 1544w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></figure>



<h3 class="wp-block-heading">Changing who owns a schema using T-SQL </h3>



<p>Once again, we can use a quick T-SQL statement instead of the point-and-click method. Run the following statement to change the owner of the schema. Of course, change the schema and username to fit your needs. </p>



<pre class="wp-block-code"><code>--transfer schema ownership to dbo
ALTER AUTHORIZATION ON SCHEMA::db_ddladmin TO dbo;
</code></pre>



<figure class="wp-block-image size-large"><a href="https://theserogroup.com/wp-content/uploads/2021/07/tsql-results.png"><img loading="lazy" decoding="async" width="982" height="448" src="https://theserogroup.com/wp-content/uploads/2021/07/tsql-results.png" alt="changing the schema owner using tsql" class="wp-image-3896" srcset="https://theserogroup.com/wp-content/uploads/2021/07/tsql-results.png 982w, https://theserogroup.com/wp-content/uploads/2021/07/tsql-results-300x137.png 300w, https://theserogroup.com/wp-content/uploads/2021/07/tsql-results-768x350.png 768w" sizes="auto, (max-width: 982px) 100vw, 982px" /></a></figure>



<h2 class="wp-block-heading">Who should own a schema?</h2>



<p>This begs a question: who should own a schema?</p>



<p>Generally, unless you have a compelling reason to do otherwise, having the schema owner be dbo is often preferred. This keeps things simple and straightforward. A good case for this would be when schemas are used to create a logical workspace or namespace for the database objects. </p>



<p>But a compelling reason may exist. For example, schemas are sometimes used as part of a broader security implementation. You can assign permissions to a schemas and all database objects in the schema will inherit those permissions. That can be convenient. If that&#8217;s the case in your environment, it&#8217;s worth discussing whether a specific user should own the schema. </p>



<p>So, as is frequently the case, the answer is: It depends.</p>



<h2 class="wp-block-heading">Want to work with The SERO Group?</h2>



<p>Here are some other posts posts that may be helpful, And be sure to check out our <a href="https://theserogroup.com/tag/script-library/">Script Library</a>.</p>



<ul class="wp-block-list"><li><a href="https://theserogroup.com/sql-server/whos-the-sql-server-database-owner-and-how-can-you-change-it/">Who’s the SQL Server Database Owner and How Can You Change It?</a></li><li><a href="https://theserogroup.com/data-security/what-takes-precedent-db_datareader-grant-or-db_denydatareader-deny/">What Takes Precedent db_datareader (GRANT) or db_denydatareader (DENY)?</a></li><li><a href="https://theserogroup.com/data-security/who-has-sysadmin-access-to-your-sql-servers/">Who Has sysadmin Access to your SQL Servers?</a></li></ul>



<p>Want to learn more about how SERO Group helps organizations take the guesswork out of managing their SQL Servers? It’s easy and there is no obligation.&nbsp;</p>



<p><a href="https://calendly.com/joe_webb" target="_blank" rel="noreferrer noopener">Schedule a call</a>&nbsp;with us to get started.</p>
<p>The post <a href="https://theserogroup.com/dba/error-msg-15138-the-database-principal-owns-a-schema-in-the-database-and-cannot-be-dropped/">Error Msg 15138 The Database Principal Owns a Schema in the Database, and Cannot be Dropped</a> appeared first on <a href="https://theserogroup.com">The SERO Group</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://theserogroup.com/dba/error-msg-15138-the-database-principal-owns-a-schema-in-the-database-and-cannot-be-dropped/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3886</post-id>	</item>
		<item>
		<title>Who Has sysadmin Access to your SQL Servers?</title>
		<link>https://theserogroup.com/data-security/who-has-sysadmin-access-to-your-sql-servers/</link>
					<comments>https://theserogroup.com/data-security/who-has-sysadmin-access-to-your-sql-servers/#comments</comments>
		
		<dc:creator><![CDATA[Joe Webb]]></dc:creator>
		<pubDate>Thu, 22 Jul 2021 15:28:47 +0000</pubDate>
				<category><![CDATA[Data Security]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Database Administration]]></category>
		<category><![CDATA[Database Development]]></category>
		<category><![CDATA[IT Manager]]></category>
		<category><![CDATA[Microsoft Azure]]></category>
		<category><![CDATA[Script Library]]></category>
		<category><![CDATA[Sero]]></category>
		<category><![CDATA[Sero Group]]></category>
		<category><![CDATA[Serogroup]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Assessment]]></category>
		<category><![CDATA[SQL Audit]]></category>
		<category><![CDATA[SQL Consultant]]></category>
		<category><![CDATA[SQL Events]]></category>
		<category><![CDATA[SQL Script Library]]></category>
		<category><![CDATA[SQL Security]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server Consultant]]></category>
		<category><![CDATA[SQL Server Management]]></category>
		<category><![CDATA[The Sero Group]]></category>
		<guid isPermaLink="false">https://theserogroup.com/?p=3831</guid>

					<description><![CDATA[<p>Phishing attacks account for more than 80% of all security incidents according to this CSO article Top cybersecurity facts, figures and statistics. And the resulting data breaches cost an average of $3.92 million. With security incidents and data breaches making the news daily, it&#8217;s important to secure your networks, including your SQL Servers. That&#8217;s not&#8230; <br /> <a class="read-more" href="https://theserogroup.com/data-security/who-has-sysadmin-access-to-your-sql-servers/">Read more</a></p>
<p>The post <a href="https://theserogroup.com/data-security/who-has-sysadmin-access-to-your-sql-servers/">Who Has sysadmin Access to your SQL Servers?</a> appeared first on <a href="https://theserogroup.com">The SERO Group</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Phishing attacks account for more than 80% of all security incidents according to this CSO article <a href="https://www.csoonline.com/article/3153707/top-cybersecurity-facts-figures-and-statistics.html" target="_blank" rel="noreferrer noopener">Top cybersecurity facts, figures and statistics</a>. And the resulting data breaches cost an average of $3.92 million. With security incidents and data breaches making the news daily, it&#8217;s important to secure your networks, including your SQL Servers. That&#8217;s not a trivial task. But a good place to start is knowing who has sysadmin access to your SQL Servers. </p>



<h2 class="wp-block-heading" id="h-what-can-a-sysadmin-do">What can a sysadmin do?</h2>



<p>We often talk about a sysadmin as if it refers to an individual, but sysadmin is actually a role. Or more accurately, sysadmin is a Fixed Server Role. A role is a group or collection of individual logins. We assign permissions to the role and all members of the role inherit the permissions of the role. You know, role-based security. It&#8217;s much easier to manage than assigning permissions to the individual logins. </p>



<p>Members of the sysadmin role can do anything in SQL Server. Anything is a pretty bold statement. But in this case, it&#8217;s true. They have the proverbial keys to the kingdom.  </p>



<p>Someone with sysadmin privileges can create and drop databases. They can grant or revoke permissions for other logins. They can change the configuration of the SQL Server; for example, the can enable xp_cmdshell or CLR integration. They can query or change data. They can do it all. </p>



<p>So, if someone in your organization who&#8217;s a member of sysadmin falls for a phishing attack, it&#8217;s bad. The attacker very likely has sysadmin privileges on your SQL Server. Yikes! And if they know what they are doing, they very well may be able to go beyond the SQL Server.</p>



<h2 class="wp-block-heading" id="h-who-are-your-sysadmins">Who are your sysadmins? </h2>



<p>Knowing who has sysadmin privileges to your SQL Servers is an important first step in securing the servers. Here&#8217;s a query I use to get a list of syadmins in SQL Server. </p>



<pre class="wp-block-code"><code>--who are the sysadmins for this SQL Server? 
SELECT p.name AS &#91;loginname],
    p.type,
    p.type_desc,
    CONVERT(VARCHAR(10), p.create_date, 101) AS &#91;created],
    CONVERT(VARCHAR(10), p.modify_date, 101) AS &#91;updated],
    p.is_disabled,
    CASE COALESCE(sl.is_expiration_checked, -1)
        WHEN 0 THEN 'No'
        WHEN 1 THEN 'Yes'
        ELSE '--'
        END AS &#91;is_expiration_checked],
    CASE COALESCE(sl.is_policy_checked, -1)
        WHEN 0 THEN 'No'
        WHEN 1 THEN 'Yes'
        ELSE '--'
        END  AS &#91;is_policy_checked],
    LOGINPROPERTY(sl.name, 'PasswordLastSetTime') AS PasswordLastSetTime
FROM sys.server_principals AS p
    JOIN sys.syslogins AS s ON p.sid = s.sid
    LEFT JOIN sys.sql_logins AS sl ON sl.sid = p.sid
WHERE p.type_desc IN('SQL_LOGIN', 'WINDOWS_LOGIN', 'WINDOWS_GROUP')
    -- Logins that are not process logins
    AND p.name NOT LIKE '##%'
    AND p.name NOT LIKE 'NT SERVICE%'
    -- Logins that are sysadmins
    AND s.sysadmin = 1 ;
</code></pre>



<p>Notice that the script looks for SQL logins as well as Windows logins. For the SQL logins, the script looks to see if the login must adhere to password expiration and complexity requirements. It also shows the last time that the SQL login&#8217;s password was changed. </p>



<p>The results from a test lab are shown below.</p>



<figure class="wp-block-image size-large"><a href="https://theserogroup.com/wp-content/uploads/2021/07/sysadmin_query_results.png"><img loading="lazy" decoding="async" width="1024" height="262" src="https://theserogroup.com/wp-content/uploads/2021/07/sysadmin_query_results-1024x262.png" alt="who" class="wp-image-3838" srcset="https://theserogroup.com/wp-content/uploads/2021/07/sysadmin_query_results-1024x262.png 1024w, https://theserogroup.com/wp-content/uploads/2021/07/sysadmin_query_results-300x77.png 300w, https://theserogroup.com/wp-content/uploads/2021/07/sysadmin_query_results-768x196.png 768w, https://theserogroup.com/wp-content/uploads/2021/07/sysadmin_query_results.png 1088w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></figure>



<h2 class="wp-block-heading" id="h-how-many-sysadmins-should-you-have">How many sysadmins should you have? </h2>



<p>So, you&#8217;ve run the query and gotten the list of sysadmins. You&#8217;ve likely found some surprises. Maybe there&#8217;s a login for someone that left the company years ago. Or maybe a vender was temporarily granted sysadmin privileges during an installation but it was never removed. Some power users or developers may have been added to the role for a reason no one can remember? And your CFO needs sysadmin access? Really?</p>



<p>In any case, there are more members of sysadmin than there should be. Identify those logins and remove them. Grant them lower level permissions that will meet their needs while still protecting your SQL Server. </p>



<p>But this begs the question &#8211; how many sysadmins should you have? </p>



<p>That&#8217;s going to depend on your environment. The size of your company and the number of SQL Servers you have will certainly influence the number of sysadmins you need. I cannot answer that question for you, at least not with a specific number. </p>



<p>What I can say is: you should have as few as possible, but no less than that. </p>



<p>Here are a few other posts that you may find interesting.</p>



<ul class="wp-block-list">
<li><a href="https://theserogroup.com/data-security/what-takes-precedent-db_datareader-grant-or-db_denydatareader-deny/">What Takes Precedent db_datareader (GRANT) or db_denydatareader (DENY)?</a></li>



<li><a href="https://theserogroup.com/professional-development/scary-and-dangerous-things-in-sql-server/">Scary and Dangerous Things in SQL Server</a></li>



<li><a href="https://theserogroup.com/sql-server/whos-the-sql-server-database-owner-and-how-can-you-change-it/">Who’s the SQL Server Database Owner and How Can You Change It?</a></li>



<li><a href="https://theserogroup.com/sql-server/securing-your-sql-servers-what-should-you-audit/">Securing Your SQL Servers, What Should You Audit?</a></li>



<li><a href="https://theserogroup.com/sql-server/protect-your-sql-server-from-mrbminer-and-other-malware-attacks/">Protect Your SQL Server from MrbMiner and Other Malware Attacks</a></li>
</ul>



<p>Also, check out our <a href="https://theserogroup.com/tag/script-library/">Script Library</a>. </p>



<h2 class="wp-block-heading" id="h-want-to-work-with-the-sero-group">Want to work with The SERO Group?</h2>



<p>Worried about your SQL Server&#8217;s security? Want a second set of eyes to review it? We can help. </p>



<p><a href="https://theserogroup.com/#contact" target="_blank" rel="noreferrer noopener">Schedule a call</a> with us to get started. It’s easy and there is no obligation. </p>
<p>The post <a href="https://theserogroup.com/data-security/who-has-sysadmin-access-to-your-sql-servers/">Who Has sysadmin Access to your SQL Servers?</a> appeared first on <a href="https://theserogroup.com">The SERO Group</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://theserogroup.com/data-security/who-has-sysadmin-access-to-your-sql-servers/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3831</post-id>	</item>
		<item>
		<title>What SQL Server Version Am I Running?</title>
		<link>https://theserogroup.com/sql-server/what-sql-server-version-am-i-running/</link>
					<comments>https://theserogroup.com/sql-server/what-sql-server-version-am-i-running/#comments</comments>
		
		<dc:creator><![CDATA[Joe Webb]]></dc:creator>
		<pubDate>Tue, 13 Jul 2021 13:21:48 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Database Administration]]></category>
		<category><![CDATA[Database Development]]></category>
		<category><![CDATA[IT Manager]]></category>
		<category><![CDATA[Script Library]]></category>
		<category><![CDATA[Sero]]></category>
		<category><![CDATA[Sero Group]]></category>
		<category><![CDATA[Serogroup]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Assessment]]></category>
		<category><![CDATA[SQL Audit]]></category>
		<category><![CDATA[SQL Consultant]]></category>
		<category><![CDATA[SQL Script Library]]></category>
		<category><![CDATA[SQL Security]]></category>
		<category><![CDATA[SQL Server Consultant]]></category>
		<category><![CDATA[SQL Server Management]]></category>
		<category><![CDATA[The Sero Group]]></category>
		<guid isPermaLink="false">https://theserogroup.com/?p=3809</guid>

					<description><![CDATA[<p>One of the first things we look at when doing a SQL Server Assessment (Health Check) is the SQL Server version and patch level. We don&#8217;t stop there, of course. We check a ton of other things that can affect the SQL Server&#8217;s performance, its security, and its reliability. But we start with the version&#8230; <br /> <a class="read-more" href="https://theserogroup.com/sql-server/what-sql-server-version-am-i-running/">Read more</a></p>
<p>The post <a href="https://theserogroup.com/sql-server/what-sql-server-version-am-i-running/">What SQL Server Version Am I Running?</a> appeared first on <a href="https://theserogroup.com">The SERO Group</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>One of the first things we look at when doing a SQL Server Assessment (Health Check) is the SQL Server version and patch level. We don&#8217;t stop there, of course. We check a ton of other things that can affect the SQL Server&#8217;s performance, its security, and its reliability. But we start with the version of the database engine. Why? Because the version and patch level affects everything else. </p>



<p>If you want to skip the &#8220;why&#8221; and jump straight to the &#8220;how&#8221;, <a href="#howto">click here</a>.</p>



<h2 class="wp-block-heading">Does your SQL Server version really matter? </h2>



<p>Microsoft releases a new version of SQL Server every 18 to 24 months. Each new version builds on the prior version, adding new features and improving existing capabilities. It&#8217;s a bit like Apple releasing a new iPhone. It&#8217;s new and shiny and can do things the prior versions couldn&#8217;t. </p>



<p>However, there&#8217;s a big difference. Upgrading your iPhone is really a luxury. If you don&#8217;t upgrade, you&#8217;ll miss out on a better camera and a maybe few O/S improvements. But otherwise, you&#8217;ll function just as well. </p>



<p>But not upgrading your database servers is another story. Not upgrading can create big problems. See <a href="https://theserogroup.com/sql-server/why-upgrade-my-sql-server-2008-servers/">Why Not Upgrading Could Be Risky for Your Data</a>. Additionally, over time SQL Server will reach its end of mainstream support. That usually happens after five years. Extended support typically ends 10 years after the initial version release. What does the loss of mainstream and extended support mean? Check out <a href="https://theserogroup.com/sql-server/end-of-mainstream-support-for-sql-server-2016/">End of Mainstream Support for SQL Server 2016</a> for a summary.</p>



<p>Your SQL Server version matters. You want to be as close to current as your application venders will support. </p>



<h3 class="wp-block-heading">What&#8217;s with all these SQL Server updates?</h3>



<p>Before releasing a new SQL Server version into the wild, the SQL Server team spends a lot of time testing. However, SQL Server is complex and over time bugs are uncovered. Bugs that need to be fixed. So, the SQL Server team continues to improve the product even after the initial release date. </p>



<p>Microsoft pushes out updates using a couple of vehicles. Hotfixes are issued for high priority items. Items that affect security and system integrity are often released in hotfixes first.</p>



<h4 class="wp-block-heading">Cumulative Updates</h4>



<p>Periodically Microsoft will bundle the hotfixes along with other fixes into a Cumulative Update, or CU for short. Prior to SQL Server 2017, Microsoft had even larger updates called Service Packs, or SPs. Since then, they&#8217;ve opted for a more straightforward CU approach.</p>



<p>Applying CUs is important. Let&#8217;s look at SQL Server 2019 CU8 released in October of 2020 as an example. The CU contained the following fixes. </p>



<ul class="wp-block-list"><li>Fixed an issue that reduced throughput and caused higher CPU when you&nbsp;run workloads that frequently allocate and release memory, such as XML related functions.</li><li>Query Store scalability improvement for adhoc workloads. Query Store now imposes internal limits to the amount of memory it can use and automatically changes the operation mode to READ-ONLY until enough memory has been returned to the Database Engine, preventing performance issues.</li><li>FIX: Restoring database fails due to backup command timeout in SQL Server 2019.</li><li>FIX: SQL Server 2019 service fails to start in Linux operating system</li></ul>



<p>This is just a sampling. There were at lot of other fixes included in CU8. For a full list, see the <a href="https://support.microsoft.com/en-us/topic/cumulative-update-8-for-sql-server-2019-ed7f79d9-a3f0-a5c2-0bef-d0b7961d2d72" target="_blank" rel="noreferrer noopener">SQL Server CU8 Release Notes</a>. </p>



<p>So, keeping your SQL Server patched with CUs can improve its reliability, its performance, and its security. That&#8217;s why checking the SQL Server version and patch level is the first thing we check in our SQL Assessments. And you can easily check this, too. </p>



<h2 class="wp-block-heading" id="howto">How can I tell what version of SQL Server I&#8217;m running.</h2>



<p>Figuring out which version of SQL Server you&#8217;re running is straightforward. Let&#8217;s look at a graphical way, a couple of T-SQL scripts, and a PowerShell/dbatools script. </p>



<h3 class="wp-block-heading">Using SQL Server Management Studio or Azure Data Studio</h3>



<p>If you only have a server or two to check, using the point-and-click method in SQL Server Management Studio or Azure Data Studio is convenient. Right-click on the instance name in the list of connections and select Properties. In the window that opens, look for the version information on the General page. </p>



<figure class="wp-block-image size-large"><a href="https://theserogroup.com/wp-content/uploads/2021/07/SQLServerProperties.png"><img loading="lazy" decoding="async" width="1024" height="816" src="https://theserogroup.com/wp-content/uploads/2021/07/SQLServerProperties-1024x816.png" alt="SQL Server Properties" class="wp-image-3813" srcset="https://theserogroup.com/wp-content/uploads/2021/07/SQLServerProperties-1024x816.png 1024w, https://theserogroup.com/wp-content/uploads/2021/07/SQLServerProperties-300x239.png 300w, https://theserogroup.com/wp-content/uploads/2021/07/SQLServerProperties-768x612.png 768w, https://theserogroup.com/wp-content/uploads/2021/07/SQLServerProperties-1355x1080.png 1355w, https://theserogroup.com/wp-content/uploads/2021/07/SQLServerProperties.png 1358w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></figure>



<p>You can see that this one is SQL Server Developer Edition running on Ubuntu Linux. It&#8217;s version is 15.0.4073.23. But what do those numbers mean? Is  it up to date or not? </p>



<p>Go to <a href="https://sqlserverbuilds.blogspot.com/" target="_blank" rel="noreferrer noopener">https://sqlserverbuilds.blogspot.com/</a> and you&#8217;ll see that 15.0.4073.23 represents Cumulative update 8 (CU8) for SQL Server 2019. We can also see that this instance is behind on updates. See also <a href="https://theserogroup.com/sql-server/is-there-an-update-for-my-sql-server/">Is There an Update for My SQL Server?</a></p>



<h3 class="wp-block-heading">Using a T-SQL query</h3>



<p>Often it&#8217;s faster and easier to run a quick script to gather the version information. This is especially true if you need to collect the information for dozens or even hundreds of SQL Servers. </p>



<p>You can use a couple of different queries to return the version and update information. </p>



<h4 class="wp-block-heading">SERVERPROPERTY()</h4>



<p>Let&#8217;s look at the <a href="https://docs.microsoft.com/en-us/sql/t-sql/functions/serverproperty-transact-sql?view=sql-server-ver15" target="_blank" rel="noreferrer noopener">SERVERPROPERTY()</a> function first. Use the following query to return server version and patch level information. </p>



<pre class="wp-block-code"><code>SELECT 
     SERVERPROPERTY('ServerName') AS &#91;Server Name] ,
     SERVERPROPERTY('Edition') AS &#91;Edition], 
     SERVERPROPERTY('ProductLevel') AS &#91;Product Level] ,
     SERVERPROPERTY('ProductUpdateLevel') AS &#91;Update Level] ,
     SERVERPROPERTY('ProductVersion') AS &#91;Version Number] ;
</code></pre>



<figure class="wp-block-image size-large"><a href="https://theserogroup.com/wp-content/uploads/2021/07/SQLServerPropertiesQuery1.png"><img loading="lazy" decoding="async" width="1024" height="705" src="https://theserogroup.com/wp-content/uploads/2021/07/SQLServerPropertiesQuery1-1024x705.png" alt="SQL Server properties query" class="wp-image-3814" srcset="https://theserogroup.com/wp-content/uploads/2021/07/SQLServerPropertiesQuery1-1024x705.png 1024w, https://theserogroup.com/wp-content/uploads/2021/07/SQLServerPropertiesQuery1-300x206.png 300w, https://theserogroup.com/wp-content/uploads/2021/07/SQLServerPropertiesQuery1-768x528.png 768w, https://theserogroup.com/wp-content/uploads/2021/07/SQLServerPropertiesQuery1.png 1212w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></figure>



<h4 class="wp-block-heading">@@VERSION</h4>



<p>You can also query the<a href="https://docs.microsoft.com/en-us/sql/t-sql/functions/version-transact-sql-configuration-functions?view=sql-server-ver15" target="_blank" rel="noreferrer noopener"> @@Version</a> variable as shown below.</p>



<pre class="wp-block-code"><code>SELECT @@VERSION ; </code></pre>



<p>The results are shown below.</p>



<pre class="wp-block-code"><code>Microsoft SQL Server 2019 (RTM-CU8) (KB4577194) - 15.0.4073.23 (X64) 	Sep 23 2020 16:03:08 	Copyright (C) 2019 Microsoft Corporation	Developer Edition (64-bit) on Linux (Ubuntu 18.04.5 LTS) &lt;X64&gt;</code></pre>



<p>Both queries work and are quick to run. You can even run the queries in Central Management Server to collect information from multiple instances at once. </p>



<h3 class="wp-block-heading">Using PowerShell and dbatools</h3>



<p>If you&#8217;ve looked through our <a href="https://theserogroup.com/tag/script-library/">Script Library</a>, you&#8217;ve probably noticed we&#8217;re fans of using PowerShell and <a href="https://dbatools.io/" target="_blank" rel="noreferrer noopener">dbatools.io</a> to perform common administrative tasks in SQL Server. And as you&#8217;d expect, you can get the version information using these tools. </p>



<p>In the PowerShell script below, I&#8217;m using the<a href="https://docs.dbatools.io/#Get-DbaInstanceProperty" target="_blank" rel="noreferrer noopener"> Get-DbaInstanceProperty</a> commandlet on the localhost to gather the information. </p>



<pre class="wp-block-code"><code>Get-DbaInstanceProperty -SqlInstance localhost -SqlCredential sa -InstanceProperty NetName, Edition, VersionString, ProductLevel, HostPlatform | Format-Table </code></pre>



<p>There&#8217;s much more information available from the commandlet. Just omit the InstanceProperty parameter to get a complete list. </p>



<pre class="wp-block-code"><code>Get-DbaInstanceProperty -SqlInstance localhost -SqlCredential sa |Format-Table</code></pre>



<h2 class="wp-block-heading">Want to work with The SERO Group?</h2>



<p>Want to learn more about how SERO Group helps organizations take the guesswork out of managing their SQL Servers? Or learn more about our <a href="https://theserogroup.com/sql-configuration-assessment">SQL Server Assessments</a>? It’s easy and there is no obligation.&nbsp;<a href="https://calendly.com/joe_webb" target="_blank" rel="noreferrer noopener">Schedule a call</a> and let&#8217;s talk. </p>
<p>The post <a href="https://theserogroup.com/sql-server/what-sql-server-version-am-i-running/">What SQL Server Version Am I Running?</a> appeared first on <a href="https://theserogroup.com">The SERO Group</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://theserogroup.com/sql-server/what-sql-server-version-am-i-running/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3809</post-id>	</item>
		<item>
		<title>How Often Should I Test My SQL Server Backups?</title>
		<link>https://theserogroup.com/the-sero-group/how-often-should-i-test-my-sql-server-backups/</link>
					<comments>https://theserogroup.com/the-sero-group/how-often-should-i-test-my-sql-server-backups/#comments</comments>
		
		<dc:creator><![CDATA[Joe Webb]]></dc:creator>
		<pubDate>Fri, 12 Feb 2021 15:50:20 +0000</pubDate>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[The Sero Group]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Database Administration]]></category>
		<category><![CDATA[Database Development]]></category>
		<category><![CDATA[IT Manager]]></category>
		<category><![CDATA[Script Library]]></category>
		<category><![CDATA[Sero]]></category>
		<category><![CDATA[Sero Group]]></category>
		<category><![CDATA[SQL Assessment]]></category>
		<category><![CDATA[SQL Audit]]></category>
		<category><![CDATA[SQL Consultant]]></category>
		<category><![CDATA[SQL Script Library]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server Management]]></category>
		<guid isPermaLink="false">https://theserogroup.com/?p=3481</guid>

					<description><![CDATA[<p>“People don’t want to buy a quarter-inch drill. They want a quarter-inch hole!” In the world of databases, stakeholders don’t care about SQL backups. They care about the ability to restore a SQL database.</p>
<p>The post <a href="https://theserogroup.com/the-sero-group/how-often-should-i-test-my-sql-server-backups/">How Often Should I Test My SQL Server Backups?</a> appeared first on <a href="https://theserogroup.com">The SERO Group</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>“Backing up your SQL Server database is not really important,” I used to tell students. I’d pause, letting that sink in for a moment while the look of disbelief slowly overtook their faces. Then, I’d add “It’s the ability to restore a database that’s important. You’ve got to test your SQL Server backups. Unless you do that, you cannot be confident in your ability to restore if it becomes necessary.”</p>



<p>It’s like what Harvard Business School Professor Theodore Levitt said about marketing: “People don’t want to buy a quarter-inch drill. They want a quarter-inch hole!” In the world of databases, stakeholders don’t care about SQL backups. They care about the ability to restore a SQL database.</p>



<h2 class="wp-block-heading" id="h-testing-your-sql-server-backups">Testing your SQL Server backups</h2>



<p>How do you know if your SQL Server backups are good? We recommend three levels of testing.</p>



<ol class="wp-block-list">
<li>Checking your SQL backup jobs</li>



<li>Verifying the backup file</li>



<li>Proving your backups with a test restore.</li>
</ol>



<h3 class="wp-block-heading" id="h-checking-your-database-backup-job">Checking your database backup job</h3>



<p>The ability to restore a database starts with having a good backup. We prefer native backups and in our minds, there’s no better way to manage your native backups than using <a rel="noreferrer noopener" href="https://ola.hallengren.com" target="_blank">Ola Hallegren</a>’s Award winning scripts. It’s our tool of choice when it comes to SQL backups. (Note: when/if you run a SQL Server backup manually, you can <a href="https://theserogroup.com/sql-server/script-how-long-until-my-sql-server-backup-restore-completes/">use this script to see how much longer the backup will take</a>.)</p>



<p>Schedule full, differential, and transaction log backups to occur regularly. Then make sure that they actually run and complete successfully. </p>



<p>Job failure notifications are good, and you should set those up, but it&#8217;s reassuring to actually see for yourself that the jobs complete successfully. The absence of a failure notification doesn’t automatically mean success. It’s better to check. For our customers, we look at the SQL Server Agent job histories daily.</p>



<p>You can use SQL Server Management Studio or Azure Data Studio to look at job histories. Of course, scripting it out is faster and more detailed. You can use the following script as starting point for your own query to check job history. This query looks for all failed jobs within the past week.</p>



<pre class="wp-block-code"><code>USE msdb;
GO

SELECT j.name AS Job_Name,
    h.step_name AS Step_Name,
    CONVERT(CHAR(10), CAST(STR(h.run_date, 8, 0) AS DATETIME), 111) AS RunDate,
    STUFF(STUFF(RIGHT('000000'+CAST(h.run_time AS VARCHAR(6)), 6), 5, 0, ':'), 3, 0, ':') AS Run_Time,
    h.run_duration AS Step_Duration_In_Seconds,
    CASE h.run_status
		 WHEN 0 THEN 'Failed'
		 WHEN 1 THEN 'Succeeded'
		 WHEN 2 THEN 'Retrying'
		 WHEN 3 THEN 'Canceled'
		 WHEN 4 THEN 'In-progress'
	  END AS Execution_Status,
    h.message AS &#91;Message]
FROM sysjobhistory AS h
    JOIN sysjobs AS j ON j.job_id = h.job_id
WHERE CAST(STR(h.run_date, 8, 0) AS DATETIME) &gt; DATEADD(ww, -1, GETDATE())
    AND h.step_name = '(Job outcome)'
    AND h.run_status != 1
ORDER BY j.name ASC,
	    h.run_date DESC,
	    h.run_time DESC;
</code></pre>



<p>In this example, the database backup job failed recently. </p>



<figure class="wp-block-image size-large"><a href="https://theserogroup.com/wp-content/uploads/2021/02/failedbackupjob.png"><img loading="lazy" decoding="async" width="1006" height="102" src="https://theserogroup.com/wp-content/uploads/2021/02/failedbackupjob.png" alt="sql server failed backup job output" class="wp-image-3493" srcset="https://theserogroup.com/wp-content/uploads/2021/02/failedbackupjob.png 1006w, https://theserogroup.com/wp-content/uploads/2021/02/failedbackupjob-300x30.png 300w, https://theserogroup.com/wp-content/uploads/2021/02/failedbackupjob-768x78.png 768w" sizes="auto, (max-width: 1006px) 100vw, 1006px" /></a></figure>



<p>Another script we frequently use to look for backup job failures is:</p>



<pre class="wp-block-code"><code>
USE MSDB;

SELECT DISTINCT
    s.&#91;Database_Name],
    f.logical_device_name AS LogicalDeviceName,
    f.physical_device_name AS PhysicalDeviceName,
    s.expiration_date AS ExpirationDate,
    s.name AS Name,
    s.&#91;description] AS &#91;Description],
    s.user_name AS UserName,
    s.backup_start_date AS StartDate,
    s.backup_finish_date AS EndDate,
    DATEDIFF(mi, s.backup_start_date, s.backup_finish_date) AS DurationInMinutes,
    CAST(CASE s.type 
        WHEN 'D' THEN 'Database' 
        WHEN 'L' THEN 'Log' 
        WHEN 'I' THEN 'Differential' 
        WHEN 'F' THEN 'File' 
        WHEN 'G' THEN 'Diff File' 
        WHEN 'P' THEN 'Partial' 
        WHEN 'Q' THEN 'Diff Partial' 
        END AS NVARCHAR(128)) AS BackupType,
    ISNULL(s.compressed_backup_size, s.backup_size) / 1048576 as SIZE,
    GetDate() AS DateChecked
FROM msdb.dbo.backupmediafamily AS f
    JOIN msdb.dbo.backupset AS s ON f.media_set_id = s.media_set_id
WHERE (CONVERT(datetime, s.backup_start_date, 102) &gt;= GETDATE() - 1)
    AND s.server_name = @@servername --Filters out databases that were restored from other instances.
    --AND s.&#91;Database_Name] = 'DBA'
    AND s.type = 'D'
ORDER BY StartDate DESC </code></pre>



<h3 class="wp-block-heading" id="h-verifying-your-database-backup-file">Verifying your database backup file</h3>



<p>Your weekly, daily, and sub-daily backups are scheduled and running like clockwork. You’re checking to make sure that they are actually completing successfully. But, what if the SQL backup file is bad or incomplete? What if a file was corrupted while being written to disk? The job completed successfully, but you don&#8217;t have a valid backup file to restore from. </p>



<p>To help with this, you should verify your SQL backup files regularly. Use the <a href="https://docs.microsoft.com/en-us/sql/t-sql/statements/restore-statements-verifyonly-transact-sql?view=sql-server-ver15" target="_blank" rel="noreferrer noopener">RESTORE VERIFYONLY TSQL command</a> to check the completeness of the backup file, as well as to make sure the entire file is readable by SQL Server. </p>



<p>In the following example, we’re verifying that the BaseballData.bak file is valid.</p>



<pre class="wp-block-code"><code>RESTORE VERIFYONLY FROM DISK='/var/opt/mssql/backup/BaseballData.bak'</code></pre>



<p>If it verifies successfully, you’ll see a message similar to the following.</p>



<figure class="wp-block-image size-large"><a href="https://theserogroup.com/wp-content/uploads/2021/02/verifysqlbackup.png"><img loading="lazy" decoding="async" width="406" height="116" src="https://theserogroup.com/wp-content/uploads/2021/02/verifysqlbackup.png" alt="verifying a sql backup file" class="wp-image-3495" srcset="https://theserogroup.com/wp-content/uploads/2021/02/verifysqlbackup.png 406w, https://theserogroup.com/wp-content/uploads/2021/02/verifysqlbackup-300x86.png 300w" sizes="auto, (max-width: 406px) 100vw, 406px" /></a></figure>



<p>RESTORE VERIFYONLY helps provide confidence that the backup files are usable without having to actually restore the database. We recommend running a RESTORE VERIFYONLY regularly, at least monthly on select SQL backup files.</p>



<h3 class="wp-block-heading" id="h-testing-your-database-backup">Testing your database backup</h3>



<p>Once you&#8217;ve have confidence that SQL backups are occurring, and that the backup files are well-formed and complete. There is still one more check that should be done periodically. To have complete confidence you can restore when needed, it&#8217;s good to actually go through the restore process. </p>



<p>It&#8217;s like a grade school fire drill. You can talk to students about what to do during a fire, showing them exit plans and rallying points, but until you walk through the process, it&#8217;s all theoretical. And, in an emergency, you don&#8217;t want to work from theory; you want to have practiced. </p>



<p>Take a SQL backup file and restore it another SQL Server instance. Apply differential backup files and transaction log files. Document the steps if you haven&#8217;t already. Creating scripts that automate it the process is a good idea, as well. In the following script, I&#8217;m restoring a copy of the BaseballData database to another instance. </p>



<pre class="wp-block-code"><code>RESTORE DATABASE BaseballData_restored  
   FROM DISK = '/var/opt/mssql/backup/BaseballData.bak' 
   WITH RECOVERY,  
   MOVE 'Baseball' TO '/var/opt/mssql/data/Baseball_restored.mdf',   
   MOVE 'Baseball_log' TO '/var/opt/mssql/data/Baseball_restored.ldf';  </code></pre>



<p>Once you&#8217;ve restored the SQL backup, test the integrity of the database by running <a href="https://docs.microsoft.com/en-us/sql/t-sql/database-console-commands/dbcc-checkdb-transact-sql?view=sql-server-ver15" target="_blank" rel="noreferrer noopener">DBCC CHECKDB</a>, checking the output for errors. Also see: <a href="https://theserogroup.com/sql-server/when-was-the-last-known-good-dbcc-checkdb-integrity-check/">When Was the Last Known Good DBCC CHECKDB Integrity Check?</a> </p>



<pre class="wp-block-code"><code>DBCC CHECKDB (BaseballData_restored);</code></pre>



<p>For our DBA as a Service customers, we recommend going through this exercise quarterly.</p>



<h2 class="wp-block-heading" id="h-how-often-should-you-test-sql-server-backup-files">How often should you test SQL Server backup files?</h2>



<p>Each environment is different and has it&#8217;s own setup of unique requirements. So there isn&#8217;t a one-size-fits-all answer here. We generally recommend the following as a good starting point and then adjusting as needed. </p>



<ol class="wp-block-list">
<li><strong>Checking your SQL backup jobs.</strong> Do this daily. Without a backup file, you cannot restore. Don&#8217;t rely exclusively on job failure notifications. Make sure the backup jobs completed successfully. </li>



<li><strong>Verifying the backup file.</strong> Spot check a few key backup files at least monthly to ensure that the files are well-formed and complete.</li>



<li><strong>Proving your backups with a test restore.</strong> Depending on your systems and the storage requirements, performing a test restore and running an integrity check against the restored database every three or four months is worthwhile exercise. </li>
</ol>



<h2 class="wp-block-heading" id="h-want-to-work-with-the-sero-group">Want to work with The Sero Group?</h2>



<p>Want to learn more about how SERO Group helps organizations take the guesswork out of managing their SQL Servers? It’s easy and there is no obligation.&nbsp;</p>



<p><a href="https://theserogroup.com/#contact" target="_blank" rel="noreferrer noopener">Schedule a call</a> with us to get started.</p>
<p>The post <a href="https://theserogroup.com/the-sero-group/how-often-should-i-test-my-sql-server-backups/">How Often Should I Test My SQL Server Backups?</a> appeared first on <a href="https://theserogroup.com">The SERO Group</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://theserogroup.com/the-sero-group/how-often-should-i-test-my-sql-server-backups/feed/</wfw:commentRss>
			<slash:comments>6</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3481</post-id>	</item>
		<item>
		<title>Am I affected by MrbMiner malware?</title>
		<link>https://theserogroup.com/sql-server/am-i-affected-by-mrbminer-malware/</link>
					<comments>https://theserogroup.com/sql-server/am-i-affected-by-mrbminer-malware/#comments</comments>
		
		<dc:creator><![CDATA[Luke Campbell]]></dc:creator>
		<pubDate>Wed, 16 Sep 2020 14:19:17 +0000</pubDate>
				<category><![CDATA[Consulting]]></category>
		<category><![CDATA[SQL Assess]]></category>
		<category><![CDATA[SQL Audit]]></category>
		<category><![CDATA[SQL Security]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[audit]]></category>
		<category><![CDATA[exploit]]></category>
		<category><![CDATA[Script Library]]></category>
		<category><![CDATA[SQL Script Library]]></category>
		<guid isPermaLink="false">http://theserogroup.com/?p=3176</guid>

					<description><![CDATA[<p>SQL Server hardware can be powerful. Lots of CPU cores and memory. Just what a crypto miner may need in their quest to generate cryptocurrency. In the case of the MrbMiner exploit, numerous SQL Servers have been exploited with brute-force attacks. These attacks are scanning for servers exposed directly to the internet and are using&#8230; <br /> <a class="read-more" href="https://theserogroup.com/sql-server/am-i-affected-by-mrbminer-malware/">Read more</a></p>
<p>The post <a href="https://theserogroup.com/sql-server/am-i-affected-by-mrbminer-malware/">Am I affected by MrbMiner malware?</a> appeared first on <a href="https://theserogroup.com">The SERO Group</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>SQL Server hardware can be powerful.  Lots of CPU cores and memory.  Just what a crypto miner may need in their quest to generate cryptocurrency.  In the case of the MrbMiner exploit, numerous SQL Servers have been exploited with brute-force attacks.  These attacks are scanning for servers exposed directly to the internet and are using weak passwords.  Once exploited, a new account, using the username &#8220;Default&#8221; is created.  An app is downloaded which begins mining Monero cryptocurrency.  More details on the exploit can be found <a rel="noreferrer noopener" href="https://zd.net/3hCOciU" target="_blank">here</a>.</p>



<h3 class="wp-block-heading" id="h-checking-for-the-default-username">Checking for the &#8220;Default&#8221; username</h3>



<p>You can check for the presence of this login by running the query below.  If found, a full network audit is recommended.  </p>



<pre class="wp-block-code"><code>--Verify the default account doesn't exist.  No results is a good thing.
SELECT 
	&#91;name],
	&#91;type_desc],
	is_disabled,
	create_date,
	modify_date
FROM sys.server_principals
WHERE &#91;name] = 'Default'</code></pre>



<p>What if you already had a login named &#8220;Default&#8221;?  Determine if the password was recently changed to &#8220;<em>@fg125kjnhn987</em>&#8221; and if there have been any recent login failures.  Review for any recently created logins as well.  Are all logins accounted for?  Were any created that you were not aware of?  If so, review each further to determine what permissions they have and identify what they&#8217;re being used for. </p>



<pre class="wp-block-code"><code>--Review recently created logins
SELECT 
	&#91;name],
	&#91;type_desc],
	is_disabled,
	create_date,
	modify_date
FROM sys.server_principals
ORDER BY create_date desc</code></pre>



<p>Review recently modified logins.</p>



<pre class="wp-block-code"><code>--Review recently modified logins
SELECT 
	&#91;name],
	&#91;type_desc],
	is_disabled,
	create_date,
	modify_date
FROM sys.server_principals
ORDER BY modify_date desc</code></pre>



<h3 class="wp-block-heading" id="h-avoid-making-your-sql-servers-easy-targets">Avoid making your SQL Servers easy targets</h3>



<p>By utilizing security best practices, most brute-force attacks can be stopped.  Or, at the very least, set off alarm bells and whistles to alert you of suspicious activity.  Below is a list of do&#8217;s and don&#8217;ts we typically recommend.  This is not an exhaustive list.</p>



<h4 class="wp-block-heading" id="h-don-t">Don&#8217;t</h4>



<ol class="wp-block-list">
<li>Don&#8217;t expose your SQL Servers to the internet (if at all possible).  Use a VPN to access externally.</li>



<li>Don&#8217;t use weak passwords (for any account).  </li>



<li>Don&#8217;t add your SQL Server service accounts to the local admin group.</li>



<li>Don&#8217;t grant your SQL Server service accounts more permissions than required.</li>



<li>Don&#8217;t grant logins more permissions than required.</li>



<li>Don&#8217;t install additional services which are not required.  SQL Server licenses include not only the database engine, but integration services, analysis services, and reporting services as well (at the time of this post).  It&#8217;s easy enough to go ahead and install these additional services but also increases the attack surface area.  Only install what is required.</li>



<li>Don&#8217;t enable additional options, within SQL Server, if unneeded.  For example, xp_cmdshell,  Ole Automation Procedures, and ad hoc distributed queries.</li>
</ol>



<h4 class="wp-block-heading" id="h-do">Do</h4>



<ol class="wp-block-list" id="block-970d4d59-f9f9-4999-a879-814cbfbf5cab">
<li>Patch often.  Review latest cumulative update, service pack releases, and hotfixes.  Start <a rel="noreferrer noopener" href="https://bit.ly/3krF3Mb" data-type="URL" data-id="https://bit.ly/3krF3Mb" target="_blank">here</a>.</li>



<li>Implement a policy in which an account will become locked out after X number of attempts.</li>



<li>Change passwords often.</li>



<li>Audit the creation of new logins (and review the audits often 😉).</li>



<li>Disable the SA account. This account is well known and has unfettered access.</li>



<li>If not required, don&#8217;t use SQL Server authentication.</li>



<li>Review failed login attempts.  Especially those occurring numerous times within a short span of time.  This could be an indication of a brute-force attempt.</li>



<li>Implement a process to audit using guidelines such as the US government Security Technical Implementation Guides (STIGs) or Center for Internet Security (more on these below).</li>



<li>Review the health and performance metrics of your SQL Servers regularly.</li>



<li>Proactively monitor your SQL Servers to look for unexpected deviations of resource consumption.  </li>
</ol>



<h3 class="wp-block-heading" id="h-audit-your-environment">Audit your environment</h3>



<p>These types of exploits can typically be avoided.  Implementing good security practices can be a painful process (not only from a technical perspective but also from the staff and end user perspective).  The following guides provide a large set of information and scripts to get you started with securing your environment.  </p>



<h4 class="wp-block-heading" id="h-cis-center-for-internet-security">CIS &#8211; Center for Internet Security</h4>



<p>CIS Benchmarks are consensus-developed secure configuration guidelines for hardening.  There are benchmarks for operating systems, server software, cloud providers, network devices etc.  Take a look <a rel="noreferrer noopener" href="https://www.cisecurity.org/cis-benchmarks/" data-type="URL" data-id="https://www.cisecurity.org/cis-benchmarks/" target="_blank">here</a> for a list of what they have to offer.  SQL Server specific benchmarks can be found at <a rel="noreferrer noopener" href="https://www.cisecurity.org/benchmark/microsoft_sql_server/" target="_blank">https://www.cisecurity.org/benchmark/microsoft_sql_server/</a>.  There are some aspects of the site which requires membership but includes additional tools.  Well worth the consideration.</p>



<h4 class="wp-block-heading" id="h-national-vulnerability-database">National Vulnerability Database</h4>



<p>The <a rel="noreferrer noopener" href="https://nvd.nist.gov/ncp/repository" data-type="URL" data-id="https://nvd.nist.gov/ncp/repository" target="_blank">NCP</a> is the U.S. government repository of publicly available security checklists which provide guidance on setting the security configuration of operating systems and applications.  The checklists (STIG) can be downloaded as a zip.  To view, download and install the STIG Viewer from <a rel="noreferrer noopener" href="https://public.cyber.mil/stigs/srg-stig-tools/" target="_blank">https://public.cyber.mil/stigs/srg-stig-tools/</a> and follow the instructions.</p>



<h3 class="wp-block-heading" id="h-how-we-can-help">How we can help</h3>



<p>Security is constantly evolving.  Setting up good policies around platform hardening, password complexity and rotation, and using accounts with the least privilege required is a daunting task for any organization.  Especially those without dedicated security or database administrators.  I&#8217;m hopeful a few of the resources above can get you started on the right path.  We&#8217;re here to help as well.  If you&#8217;d like assistance in assessing your SQL Servers, schedule a call with us <a href="https://theserogroup.com/#contact" target="_blank" rel="noreferrer noopener">here</a>. </p>



<p>Thanks for reading!</p>
<p>The post <a href="https://theserogroup.com/sql-server/am-i-affected-by-mrbminer-malware/">Am I affected by MrbMiner malware?</a> appeared first on <a href="https://theserogroup.com">The SERO Group</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://theserogroup.com/sql-server/am-i-affected-by-mrbminer-malware/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3176</post-id>	</item>
		<item>
		<title>Vollgar: 6 Scripts to Help Review Your SQL Servers</title>
		<link>https://theserogroup.com/sql-server/vollgar-6-scripts-to-help-review-your-sql-servers/</link>
					<comments>https://theserogroup.com/sql-server/vollgar-6-scripts-to-help-review-your-sql-servers/#comments</comments>
		
		<dc:creator><![CDATA[Joe Webb]]></dc:creator>
		<pubDate>Tue, 07 Apr 2020 19:49:02 +0000</pubDate>
				<category><![CDATA[SQL Assess]]></category>
		<category><![CDATA[SQL Audit]]></category>
		<category><![CDATA[SQL Script Library]]></category>
		<category><![CDATA[SQL Security]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Script Library]]></category>
		<guid isPermaLink="false">http://theserogroup.com/?p=2747</guid>

					<description><![CDATA[<p>Last week, Guardicore released information about a newly discovered attack that uses SQL Servers to compromise servers and networks. Here&#8217;s a link; I&#8217;d really encourage you to read it. The attack known as Vollgar uses a simple brute force attack to gain access to SQL Servers exposed to the internet. It then uses the elevated&#8230; <br /> <a class="read-more" href="https://theserogroup.com/sql-server/vollgar-6-scripts-to-help-review-your-sql-servers/">Read more</a></p>
<p>The post <a href="https://theserogroup.com/sql-server/vollgar-6-scripts-to-help-review-your-sql-servers/">Vollgar: 6 Scripts to Help Review Your SQL Servers</a> appeared first on <a href="https://theserogroup.com">The SERO Group</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Last week, Guardicore released information about a newly discovered attack that uses SQL Servers to compromise servers and networks. Here&#8217;s a <a aria-label="link (opens in a new tab)" rel="noreferrer noopener" href="https://www.guardicore.com/2020/04/vollgar-ms-sql-servers-under-attack/" target="_blank">link</a>; I&#8217;d really encourage you to read it. The attack known as Vollgar uses a simple brute force attack to gain access to SQL Servers exposed to the internet. It then uses the elevated permissions of the compromised logins (sysadmin or serveradmin) to modify the capabilities of SQL Server and extended its access. </p>



<p><a rel="noreferrer noopener" aria-label="Guardicore has released a PowerShell script (opens in a new tab)" href="https://github.com/guardicore/labs_campaigns/tree/master/Vollgar" target="_blank">Guardicore has released a PowerShell script</a> that examines servers to determine if they’ve been infected.&nbsp;We’ve safely run the scripts on SQL Servers in our lab environment and for many of our clients. </p>



<p>If you routinely apply regular updates to your servers, practice the Principle of Least Privilege, regularly change critical passwords, have stringent password complexity requirements, and don&#8217;t expose your SQL Servers directly to the internet, the likelihood of a brute force attack succeeding is greatly reduced. </p>



<p>Here are six scripts that can help determine your level of potential exposure.</p>



<h3 class="wp-block-heading">Who Has sysadmin or serveradmin Privileges?</h3>



<p>The Vollgar attack is a brute force attack that attempts to guess the password for SQL Logins with elevated privileges. To be successful it needs logins that can execute <a rel="noreferrer noopener" aria-label="sp_configure (opens in a new tab)" href="https://docs.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-configure-transact-sql?view=sql-server-ver15" target="_blank">sp_configure</a> to change server-level settings. This are implicitly held by the sysadmin and serveradmin fixed server roles. </p>



<p>So, the first step in determining your exposure to Vollgar is to discover the members of the sysadmin and serveradmin roles. The following script will show you the members of each role. </p>



<pre class="wp-block-code"><code>USE master; 
GO

EXEC sp_helpsrvrolemember
	'sysadmin';

EXEC sp_helpsrvrolemember
	'serveradmin';</code></pre>



<p>In my sample database, the following is returned. </p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="941" height="518" src="http://theserogroup.com/wp-content/uploads/2020/04/server_role_members-1.png" alt="" class="wp-image-2771" srcset="https://theserogroup.com/wp-content/uploads/2020/04/server_role_members-1.png 941w, https://theserogroup.com/wp-content/uploads/2020/04/server_role_members-1-300x165.png 300w, https://theserogroup.com/wp-content/uploads/2020/04/server_role_members-1-768x423.png 768w" sizes="auto, (max-width: 941px) 100vw, 941px" /></figure>



<p>Another approach to retrieving the same information in one consolidated result set is to use the following script. </p>



<pre class="wp-block-code"><code>--list of logins that are members of the sysadmin or serveradmin roles
SELECT SP1.&#91;name] AS 'Login',
	SP2.&#91;name] AS 'ServerRole'
FROM sys.server_principals AS SP1
	JOIN sys.server_role_members AS SRM 
		ON SP1.principal_id = SRM.member_principal_id
	JOIN sys.server_principals AS SP2 
		ON SRM.role_principal_id = SP2.principal_id
WHERE SP2.&#91;name] IN ('sysadmin', 'serveradmin')
ORDER BY SP2.&#91;name],
	 SP1.&#91;name];</code></pre>



<p>As expected, this script produces the same results. </p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="594" height="325" src="http://theserogroup.com/wp-content/uploads/2020/04/server_role_members_v2-1.png" alt="" class="wp-image-2772" srcset="https://theserogroup.com/wp-content/uploads/2020/04/server_role_members_v2-1.png 594w, https://theserogroup.com/wp-content/uploads/2020/04/server_role_members_v2-1-300x164.png 300w" sizes="auto, (max-width: 594px) 100vw, 594px" /></figure>



<p>Of course, it&#8217;s best practice to only grant the minimum rights required by each login, a practice known as least privilege. If these queries return more logins than absolutely necessary, it&#8217;s time to review your security practices. </p>



<h3 class="wp-block-heading">Who has Passwords that Do Not Expire and without Password Complexity Requirements?</h3>



<p>Having a complex password and changing it regularly is part of the basic blocking and tackling of security. Passwords like &#8220;Password123&#8221;, &#8220;Qwerty&#8221;, and &#8220;Puddles!&#8221; can be cracked in very short order using tools freely available on the web. And if these passwords never expire, users have no reason to change them regularly, making them even more of a liability.</p>



<p>For Windows Integrated Authentication, password complexity and expiration is handled at the network domain level. For SQL logins, these are enforced inside of SQL Server. </p>



<p>To find active SQL logins (e.g. not disabled) that do not require a basic level of complexity and are set to not expired, run the following script.</p>



<pre class="wp-block-code"><code>--Active SQL Logins where passwords do not expire
--and do not have complexity requirements 
SELECT name, 
	type_desc, 
	create_date, 
	modify_date, 
	default_database_name
FROM sys.sql_logins
WHERE is_expiration_checked = 0
	 AND is_disabled = 0 
	 AND is_policy_checked = 0 ; </code></pre>



<p>In my sample system, the script produces the following list. </p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="956" height="330" src="http://theserogroup.com/wp-content/uploads/2020/04/password_dont_expire-1.png" alt="" class="wp-image-2773" srcset="https://theserogroup.com/wp-content/uploads/2020/04/password_dont_expire-1.png 956w, https://theserogroup.com/wp-content/uploads/2020/04/password_dont_expire-1-300x104.png 300w, https://theserogroup.com/wp-content/uploads/2020/04/password_dont_expire-1-768x265.png 768w" sizes="auto, (max-width: 956px) 100vw, 956px" /></figure>



<p>Regularly changing passwords creates a moving target for potential attackers. If you have SQL logins that do not expire and do not have minimum complexity requirements, consider turning these features on for all your logins. </p>



<p>Putting some of the above queries together will give us a list of all active SQL logins that are members of the sysadmin or serveradmin fixed server roles along with whether their logins adhere to password complexity and expiration policies. </p>



<pre class="wp-block-code"><code>--list of SQL logins that are members of the sysadmin or serveradmin roles
SELECT SP1.&#91;name] AS 'Login',
	SP2.&#91;name] AS 'ServerRole',
	CASE l.is_disabled WHEN 1 THEN 'No' ELSE 'Yes' END AS Is_Enabled,
	CASE l.is_expiration_checked WHEN 1 THEN 'Yes' ELSE 'No' End AS Pwd_Expires,
	CASE l.is_policy_checked WHEN 1 THEN 'Yes' ELSE 'No' END AS Pwd_Complexity_Reqs
FROM sys.server_principals AS SP1
	JOIN sys.server_role_members AS SRM
	ON SP1.principal_id = SRM.member_principal_id
	JOIN sys.server_principals AS SP2
	ON SRM.role_principal_id = SP2.principal_id
	JOIN sys.sql_logins AS l
	ON l.principal_id = SRM.member_principal_id
WHERE SP2.&#91;name] IN ('sysadmin', 'serveradmin')
ORDER BY SP2.&#91;name],
	 SP1.&#91;name];</code></pre>



<p>The following results are returned on my test system.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="802" height="274" src="http://theserogroup.com/wp-content/uploads/2020/04/elevated_privileges.png" alt="" class="wp-image-2783" srcset="https://theserogroup.com/wp-content/uploads/2020/04/elevated_privileges.png 802w, https://theserogroup.com/wp-content/uploads/2020/04/elevated_privileges-300x102.png 300w, https://theserogroup.com/wp-content/uploads/2020/04/elevated_privileges-768x262.png 768w" sizes="auto, (max-width: 802px) 100vw, 802px" /></figure>



<h3 class="wp-block-heading">When was a SQL Login Password Changed?</h3>



<p>From the prior two queries, we can see that Alice and Donnie are both active members of the sysadmin fixed server role. Donnie&#8217;s password doesn&#8217;t expire and doesn&#8217;t have to meet any password complexity requirements. Of course, this is a big red flag for security. Alice&#8217;s login, on the other hand, is set to adhere to complexity and expiration requirements. That&#8217;s good. </p>



<p>But how long has it been since Alice actually changed her password? We can use the <a rel="noreferrer noopener" aria-label="LOGINPROPERTY() (opens in a new tab)" href="https://docs.microsoft.com/en-us/sql/t-sql/functions/loginproperty-transact-sql?view=sql-server-ver15" target="_blank">LOGINPROPERTY()</a> function to help us. Note: that for the function to return meaningful information, both  CHECK_POLICY and CHECK_EXPIRATION must be enabled for the login.</p>



<pre class="wp-block-code"><code>--when was a login's password last changed?
SELECT 'Alice' AS username,
	LOGINPROPERTY('Alice', 'PasswordLastSetTime') AS PasswordLastSetTime;</code></pre>



<p>In this case, we can see that Alice last set her password on March 26, 2020. </p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="435" height="110" src="http://theserogroup.com/wp-content/uploads/2020/04/password_last_changed-1.png" alt="" class="wp-image-2774" srcset="https://theserogroup.com/wp-content/uploads/2020/04/password_last_changed-1.png 435w, https://theserogroup.com/wp-content/uploads/2020/04/password_last_changed-1-300x76.png 300w" sizes="auto, (max-width: 435px) 100vw, 435px" /></figure>



<p>We can use other properties in the LOGINPROPERTY() function, such as BadPasswordCount and BadPasswordTime.  I wouldn&#8217;t rely too heavily on the results, though. The BadPasswordCount is reset to 0 as soon as Alice successfully logs in. And, just as importantly, it&#8217;s only relevant for those SQL Logins who have  CHECK_POLICY and CHECK_EXPIRATION enabled. </p>



<pre class="wp-block-code"><code>--bad password attempts
SELECT name, 
	LOGINPROPERTY(name, 'BadPasswordCount') AS BadPasswordCount,
	LOGINPROPERTY(name, 'BadPasswordTime') AS BadPasswordTime
FROM sys.sql_logins 
WHERE is_expiration_checked = 1
	AND is_disabled = 0 
	AND is_policy_checked = 1; </code></pre>



<p>The results from my test system are shown below. </p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="693" height="490" src="http://theserogroup.com/wp-content/uploads/2020/04/bad_attempts-1.png" alt="" class="wp-image-2768" srcset="https://theserogroup.com/wp-content/uploads/2020/04/bad_attempts-1.png 693w, https://theserogroup.com/wp-content/uploads/2020/04/bad_attempts-1-300x212.png 300w" sizes="auto, (max-width: 693px) 100vw, 693px" /></figure>



<h3 class="wp-block-heading">How to See Failed Login Attempts</h3>



<p>Assuming your SQL Server is configured to log failed login attempts, and of course it should be, you can query the error log files using the sp_readerrorlog procedure to see the failed attempts. </p>



<pre class="wp-block-code"><code>EXEC sp_readerrorlog 0, 1, 'Login failed' ;</code></pre>



<p>The following is returned on my test system.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="862" height="273" src="http://theserogroup.com/wp-content/uploads/2020/04/error_log_files-1.png" alt="" class="wp-image-2769" srcset="https://theserogroup.com/wp-content/uploads/2020/04/error_log_files-1.png 862w, https://theserogroup.com/wp-content/uploads/2020/04/error_log_files-1-300x95.png 300w, https://theserogroup.com/wp-content/uploads/2020/04/error_log_files-1-768x243.png 768w" sizes="auto, (max-width: 862px) 100vw, 862px" /></figure>



<p>Better yet, use a monitoring tool to proactively monitor failed login attempts and alert when a minimum threshold is exceeded. For our <a rel="noreferrer noopener" aria-label="DBA as a Service (opens in a new tab)" href="http://theserogroup.com/#how-we-help" target="_blank">DBA as a Service</a> clients, we provide <a rel="noreferrer noopener" aria-label="SentryOne (opens in a new tab)" href="https://www.sentryone.com/" target="_blank">SentryOne</a>&#8216;s <a rel="noreferrer noopener" aria-label="SQLSentry  (opens in a new tab)" href="https://www.sentryone.com/products/sentryone-platform/sql-sentry/sql-server-performance-monitoring" target="_blank">SQLSentry </a>monitoring tool to help with this and other events that should be monitored.</p>



<h3 class="wp-block-heading">Parting Thoughts</h3>



<p>Many years ago, I set up a test system for a writing project I was involved with. As part of the test, I set the sa password to something like &#8220;Cat123Dog!&#8221; The password met most requirements of the day &#8211; upper and lower case, at least one number and one letter, and a special symbol. &#8220;Not bad,&#8221; I thought to myself.</p>



<p>Then I downloaded <a rel="noreferrer noopener" aria-label="Ophcrack (opens in a new tab)" href="https://ophcrack.sourceforge.io/" target="_blank">Ophcrack</a>, a free Windows password cracker, and released it on my unsuspecting SQL Server. Expecting the utility to run for hours, if not days, I returned to work. </p>



<p>A few minutes later, I decided to check on it, wanting to make sure it wasn&#8217;t hung for some reason. I was stunned. Ophcrack had already found the password! That was at least 10 years ago. I&#8217;m sure the tools of the hacker trade have gotten much better since then. </p>



<p>Recently, I&#8217;ve read where most breaches are a result of social engineering &#8211; someone receives an e-Card from a secret admirer, finds a thumb drive in the parking lot, or clicks an email link. &#8220;The days of brute force attacks are over,&#8221; they say. </p>



<p>Vollgar has proven them wrong. Basic security measures are still best practice. You owe it to yourself to make sure you&#8217;re doing it well. Here are a few links that may help.</p>



<ul class="wp-block-list"><li><a rel="noreferrer noopener" aria-label="Introduction to SQL Server Security (opens in a new tab)" href="https://www.red-gate.com/simple-talk/sysadmin/data-protection-and-privacy/introduction-to-sql-server-security-part-1/" target="_blank">Introduction to SQL Server Security</a></li><li><a rel="noreferrer noopener" aria-label="Securing SQL Server (opens in a new tab)" href="https://docs.microsoft.com/en-us/sql/relational-databases/security/securing-sql-server?view=sql-server-ver15" target="_blank">Securing SQL Server</a></li><li><a href="https://www.mssqltips.com/sqlservertip/3159/sql-server-security-checklist/" target="_blank" rel="noreferrer noopener" aria-label="SQL Server Security Checklist (opens in a new tab)">SQL Server Security Checklist</a></li></ul>
<p>The post <a href="https://theserogroup.com/sql-server/vollgar-6-scripts-to-help-review-your-sql-servers/">Vollgar: 6 Scripts to Help Review Your SQL Servers</a> appeared first on <a href="https://theserogroup.com">The SERO Group</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://theserogroup.com/sql-server/vollgar-6-scripts-to-help-review-your-sql-servers/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2747</post-id>	</item>
		<item>
		<title>Is There an Update for My SQL Server?</title>
		<link>https://theserogroup.com/sql-server/is-there-an-update-for-my-sql-server/</link>
					<comments>https://theserogroup.com/sql-server/is-there-an-update-for-my-sql-server/#comments</comments>
		
		<dc:creator><![CDATA[Joe Webb]]></dc:creator>
		<pubDate>Tue, 31 Mar 2020 17:58:45 +0000</pubDate>
				<category><![CDATA[SQL Audit]]></category>
		<category><![CDATA[SQL Script Library]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Script Library]]></category>
		<category><![CDATA[SQL Upgrades]]></category>
		<guid isPermaLink="false">http://theserogroup.com/?p=2718</guid>

					<description><![CDATA[<p>We&#8217;ve all asked that question at some point. Maybe we inherited a new-to-us SQL Server that hasn&#8217;t been maintained regularly. Or, perhaps we&#8217;re prepping for a maintenance window and we want to get the latest update into Test as soon as possible. Regardless, we want to know two things. First, what product version is my&#8230; <br /> <a class="read-more" href="https://theserogroup.com/sql-server/is-there-an-update-for-my-sql-server/">Read more</a></p>
<p>The post <a href="https://theserogroup.com/sql-server/is-there-an-update-for-my-sql-server/">Is There an Update for My SQL Server?</a> appeared first on <a href="https://theserogroup.com">The SERO Group</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>We&#8217;ve all asked that question at some point. Maybe we inherited a new-to-us SQL Server that hasn&#8217;t been maintained regularly. Or, perhaps we&#8217;re prepping for a maintenance window and we want to get the latest update into Test as soon as possible. Regardless, we want to know two things. First, what product version is my SQL Server currently running? And second, what is the most recent update? </p>



<h3 class="wp-block-heading">What version is my SQL Server currently running?</h3>



<p>There are several ways to check the version of SQL Server you&#8217;re currently running &#8211; all of them documented in countless places on the the web. Checking the instance property page in Management Studio and executing SELECT @@VERSION are two of the more common. </p>



<p>My preference is to run the following script using <a rel="noreferrer noopener" aria-label="SERVERPROPERTY (opens in a new tab)" href="https://docs.microsoft.com/en-us/sql/t-sql/functions/serverproperty-transact-sql?view=sql-server-ver15" target="_blank">SERVERPROPERTY</a>. When used in combination with <a rel="noreferrer noopener" aria-label="Central Management Server (opens in a new tab)" href="https://docs.microsoft.com/en-us/sql/ssms/register-servers/create-a-central-management-server-and-server-group?view=sql-server-ver15" target="_blank">Central Management Server</a>, you can check the current level for all the SQL Server instances in your environment with one click of the Execute button. </p>



<pre class="wp-block-code"><code>SELECT
	SERVERPROPERTY('servername') AS Server_Name,
	SERVERPROPERTY('edition') AS Edition,
	SERVERPROPERTY('productlevel') AS Product_Level, 
	SERVERPROPERTY('productversion') AS Product_Version,
	SERVERPROPERTY('productupdatelevel') AS Product_Update_Level,
	SERVERPROPERTY('productupdatereference') AS Product_Update_Reference,
	SERVERPROPERTY('resourceversion') AS Resource_Version, 
	@@VERSION AS Version_Information </code></pre>



<p>Running the query on my Docker container instance returns the following.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="916" height="76" src="http://theserogroup.com/wp-content/uploads/2020/03/ssms_results-1.png" alt="" class="wp-image-2731" srcset="https://theserogroup.com/wp-content/uploads/2020/03/ssms_results-1.png 916w, https://theserogroup.com/wp-content/uploads/2020/03/ssms_results-1-300x25.png 300w, https://theserogroup.com/wp-content/uploads/2020/03/ssms_results-1-768x64.png 768w" sizes="auto, (max-width: 916px) 100vw, 916px" /></figure>



<p>Keep in mind that SERVERPROPERTY can behave slightly differently for older versions of SQL Server. ProductUpdateLevel and ProductUpdateReference, for instance, were introduced as part of SQL Server 2012. When run against an older version, such as SQL Server 2005, these will return NULL. </p>



<p>Running against another older SQL Server produces the following. Still, it gives you the information you need.</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="978" height="84" src="http://theserogroup.com/wp-content/uploads/2020/03/ssms_results2-1.png" alt="" class="wp-image-2736" srcset="https://theserogroup.com/wp-content/uploads/2020/03/ssms_results2-1.png 978w, https://theserogroup.com/wp-content/uploads/2020/03/ssms_results2-1-300x26.png 300w, https://theserogroup.com/wp-content/uploads/2020/03/ssms_results2-1-768x66.png 768w" sizes="auto, (max-width: 978px) 100vw, 978px" /></figure>



<p>So, is that up to date or not for this version of SQL Server? </p>



<h3 class="wp-block-heading">What is the latest update level for my version of SQL Server? </h3>



<p>To answer that question, let&#8217;s turn to the definitive source &#8211; Microsoft. Microsoft has gotten into a fairly predictable cadence of regularly releasing updates for SQL Server. To help us keep track of the releases they&#8217;ve provided a <a rel="noreferrer noopener" aria-label="SQL Docs page that lists all versions of SQL Server (opens in a new tab)" href="https://docs.microsoft.com/en-us/sql/database-engine/install-windows/latest-updates-for-microsoft-sql-server?view=sql-server-ver15&amp;_lrsc=17f3dde9-1a6c-467a-ad87-c08ff7c03f42" target="_blank">SQL Docs page that lists all versions of SQL Server</a> for the past 20 years. Yes, all the way back to SQL Server 2000. </p>



<p>The list shows the Product Version, the Latest Service Pack (if applicable), the Latest GDR (General Distribution Release), the Latest cumulative update (CU), the CU Release Date, and a link to some General Guidance for the each version. Here&#8217;s what the site looks like today, March 31, 2020. </p>



<figure class="wp-block-image size-large"><a href="https://docs.microsoft.com/en-us/sql/database-engine/install-windows/latest-updates-for-microsoft-sql-server?view=sql-server-ver15&amp;_lrsc=17f3dde9-1a6c-467a-ad87-c08ff7c03f42" target="_blank" rel="noreferrer noopener"><img loading="lazy" decoding="async" width="1024" height="650" src="http://theserogroup.com/wp-content/uploads/2020/03/Latest_Updates-1024x650.png" alt="" class="wp-image-2724" srcset="https://theserogroup.com/wp-content/uploads/2020/03/Latest_Updates-1024x650.png 1024w, https://theserogroup.com/wp-content/uploads/2020/03/Latest_Updates-300x190.png 300w, https://theserogroup.com/wp-content/uploads/2020/03/Latest_Updates-768x487.png 768w, https://theserogroup.com/wp-content/uploads/2020/03/Latest_Updates.png 1473w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></figure>



<p>Not sure of the difference in a SP, GDR, and CU? Here&#8217;s a <a rel="noreferrer noopener" aria-label="blog post I wrote 12 years ago when I blogged at SQLTeam (opens in a new tab)" href="https://weblogs.sqlteam.com/joew/2008/05/07/60591/" target="_blank">blog post I wrote 12 years ago when I blogged at SQLTeam</a> that explains the vernacular. </p>



<h3 class="wp-block-heading">Additional Information</h3>



<p>Here are a few other links that may help. </p>



<ul class="wp-block-list"><li><a rel="noreferrer noopener" aria-label="SQL Server Release Blog (opens in a new tab)" href="https://aka.ms/sqlreleases" target="_blank">SQL Server Release Blog</a> &#8211; A Micosoft Tech Community blog dedicated to SQL Server Releases.</li><li><a rel="noreferrer noopener" aria-label="SQL Server Builds Blog (opens in a new tab)" href="https://sqlserverbuilds.blogspot.com/" target="_blank">SQL Server Builds Blog</a> &#8211; I particularly like this blog since it shows each version along with relevant build numbers. </li><li><a rel="noreferrer noopener" aria-label="SQL Server Updates  (opens in a new tab)" href="https://sqlserverupdates.com/" target="_blank">SQL Server Updates </a> &#8211; Brent Ozar also maintains a great site for keeping track of version update information. </li></ul>
<p>The post <a href="https://theserogroup.com/sql-server/is-there-an-update-for-my-sql-server/">Is There an Update for My SQL Server?</a> appeared first on <a href="https://theserogroup.com">The SERO Group</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://theserogroup.com/sql-server/is-there-an-update-for-my-sql-server/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2718</post-id>	</item>
		<item>
		<title>How Many tempdb Data Files Should My SQL Server Have?</title>
		<link>https://theserogroup.com/sql-server/how-many-tempdb-data-files-should-my-sql-server-have/</link>
					<comments>https://theserogroup.com/sql-server/how-many-tempdb-data-files-should-my-sql-server-have/#comments</comments>
		
		<dc:creator><![CDATA[Joe Webb]]></dc:creator>
		<pubDate>Tue, 02 Jul 2019 13:30:58 +0000</pubDate>
				<category><![CDATA[Azure]]></category>
		<category><![CDATA[SQL Assess]]></category>
		<category><![CDATA[SQL Script Library]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Database Administration]]></category>
		<category><![CDATA[Database Development]]></category>
		<category><![CDATA[DBA]]></category>
		<category><![CDATA[Script Library]]></category>
		<category><![CDATA[Sero]]></category>
		<category><![CDATA[Sero Group]]></category>
		<category><![CDATA[SQL Audit]]></category>
		<category><![CDATA[SQL Consultant]]></category>
		<category><![CDATA[SQL Server Consultant]]></category>
		<category><![CDATA[SQL Server Management]]></category>
		<category><![CDATA[TempDB]]></category>
		<category><![CDATA[The Sero Group]]></category>
		<guid isPermaLink="false">http://theserogroup.com/?p=2433</guid>

					<description><![CDATA[<p>We&#8217;re frequently asked how many tempdb files a SQL Server should have, especially from those who have downloaded our free&#160;5 Common SQL Server Configuration Issues PDF. That&#8217;s&#160;because there&#8217;s a lot of well-intended but incorrect information posted on the internet about tempdb.&#160; What Is tempdb? Let&#8217;s start with a very brief description of tempdb. When SQL&#8230; <br /> <a class="read-more" href="https://theserogroup.com/sql-server/how-many-tempdb-data-files-should-my-sql-server-have/">Read more</a></p>
<p>The post <a href="https://theserogroup.com/sql-server/how-many-tempdb-data-files-should-my-sql-server-have/">How Many tempdb Data Files Should My SQL Server Have?</a> appeared first on <a href="https://theserogroup.com">The SERO Group</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>We&#8217;re frequently asked how many tempdb files a SQL Server should have, especially from those who have downloaded our free&nbsp;<a rel="noreferrer noopener" href="http://theserogroup.com/#common-issues" target="_blank">5 Common SQL Server Configuration Issues PDF</a>. That&#8217;s&nbsp;because there&#8217;s a lot of well-intended but incorrect information posted on the internet about tempdb.&nbsp;</p>



<h2 class="wp-block-heading">What Is tempdb? </h2>



<p>Let&#8217;s start with a very brief description of tempdb. When SQL Server needs some additional workspace to resolve a query, it uses a built-in system database called tempdb. A query&nbsp;may use tempdb for sorting operations, cursors, temporary tables, or even aggregation operations among other things. Since there is only one tempdb database for each SQL Server instance, it can be quite heavily used.&nbsp;</p>



<p>By default, when you install SQL Server, one&nbsp;data file is created for the tempdb database. Having only one data file, however, can hinder SQL Server&#8217;s performance. The solitary file can become a bottleneck for queries that require tempdb. This is a pretty common issue, in fact, it made our&nbsp;<a rel="noreferrer noopener" href="http://www.theserogroup.com/#common-issues" target="_blank">Top 5 List</a>.&nbsp;</p>



<h2 class="wp-block-heading">How Many tempdb Files Do You Need?</h2>



<p>So, if the default value is likely not right for you, how many tempdb&nbsp;data files should you have? The answer is: it depends.&nbsp;According to&nbsp;<a rel="noreferrer noopener" href="https://support.microsoft.com/en-us/help/2154845/recommendations-to-reduce-allocation-contention-in-sql-server-tempdb-d" target="_blank">Microsoft Support</a>, the best approach is to create one tempdb data file per logical processor up to 8 data files. </p>



<p>If your system has more than 8 logical processors, start with 8 data files and monitor your server&#8217;s workload to determine if more data files would be beneficial.&nbsp;If you do find that an increase is warranted, add 4 data files at a time, but do not add more than the number of logical processors.</p>



<h2 class="wp-block-heading">How Many tempdb Files Do You Have?</h2>



<p>How many tempdb data files does your SQL Server have? A fairly straightforward query can answer the question.&nbsp;Open Management Studio and run the following query.</p>



<pre class="wp-block-code"><code>--tell me about my tempdb
SELECT
  f.name AS &#91;file_name],
  CAST((f.size / 128.0) AS DECIMAL(15, 2)) AS &#91;size_in_MB],
  CAST(f.size / 128.0 - CAST(FILEPROPERTY(f.name, 'SpaceUsed') AS INT) / 128.0 AS DECIMAL(15, 2)) AS &#91;space_available_in_MB],
  &#91;file_id] AS &#91;file_id],
  ISNULL(fg.name, 'LOG') AS &#91;filegroup_name],
  f.physical_name AS &#91;physical_name]
FROM sys.master_files AS f
LEFT OUTER JOIN sys.data_spaces AS fg
  ON f.data_space_id = fg.data_space_id
WHERE f.database_id = 2;</code></pre>



<p>You&#8217;ll notice that the results from the&nbsp;query above&nbsp;include the folder location for each tempdb file. That&#8217;s because file placement can also have a dramatic impact on performance and even reliability. But that&#8217;s another story.</p>



<p>For more information about tempdb, check out&nbsp;<a rel="noreferrer noopener" href="https://jwebb.me/ms_doc_tempdb" target="_blank">Microsoft&#8217;s SQL Docs</a>.&nbsp;</p>



<p>Want to know more about other configuration options that could affect performance and reliability? Check out <a href="https://theserogroup.com/2019/08/06/is-my-sql-server-configured-properly/" target="_blank" rel="noreferrer noopener">Is My SQL Server Configured Properly?</a></p>



<p>[EDIT Oct 5, 2021] &#8211; Also see <a href="https://theserogroup.com/dba/how-to-configure-sql-server-tempdb/">How to Configure SQL Server tempdb?</a> for other tempdb configuration settings that can affect performance. </p>



<h2 class="wp-block-heading">Want to work with The Sero Group?</h2>



<p>Want to learn more about how SERO Group helps organizations take the guesswork out of managing their SQL Servers? It’s easy and there is no obligation. </p>



<p><a href="https://calendly.com/joe_webb">Schedule a call</a> with us to get started.</p>
<p>The post <a href="https://theserogroup.com/sql-server/how-many-tempdb-data-files-should-my-sql-server-have/">How Many tempdb Data Files Should My SQL Server Have?</a> appeared first on <a href="https://theserogroup.com">The SERO Group</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://theserogroup.com/sql-server/how-many-tempdb-data-files-should-my-sql-server-have/feed/</wfw:commentRss>
			<slash:comments>5</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2433</post-id>	</item>
		<item>
		<title>Script: How Long Until My SQL Server Backup/Restore Completes?</title>
		<link>https://theserogroup.com/sql-server/script-how-long-until-my-sql-server-backup-restore-completes/</link>
					<comments>https://theserogroup.com/sql-server/script-how-long-until-my-sql-server-backup-restore-completes/#comments</comments>
		
		<dc:creator><![CDATA[Joe Webb]]></dc:creator>
		<pubDate>Tue, 13 Nov 2018 16:20:17 +0000</pubDate>
				<category><![CDATA[SQL Script Library]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[DBA]]></category>
		<category><![CDATA[Script Library]]></category>
		<guid isPermaLink="false">http://theserogroup.com/?p=2269</guid>

					<description><![CDATA[<p>Ding!&#160; Your desktop IM client chimes. &#8220;Any idea how long my SQL Server backup/restore will take to complete? I think it may be stuck.&#8221;&#160; Someone has started a database restore (or backup) using a T-SQL command. Now he wants to know how long the process will take to complete. He&#8217;s gone to the bathroom and&#8230; <br /> <a class="read-more" href="https://theserogroup.com/sql-server/script-how-long-until-my-sql-server-backup-restore-completes/">Read more</a></p>
<p>The post <a href="https://theserogroup.com/sql-server/script-how-long-until-my-sql-server-backup-restore-completes/">Script: How Long Until My SQL Server Backup/Restore Completes?</a> appeared first on <a href="https://theserogroup.com">The SERO Group</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Ding!&nbsp; Your desktop IM client chimes. &#8220;Any idea how long my SQL Server backup/restore will take to complete? I think it may be stuck.&#8221;&nbsp;</p>



<p>Someone has started a database restore (or backup) using a T-SQL command. Now he wants to know how long the process will take to complete. He&#8217;s gone to the bathroom and even gotten a coffee, yet it&#8217;s still running.&nbsp;</p>



<p>If he had used the WITH STATS option for either the <a href="https://docs.microsoft.com/en-us/sql/t-sql/statements/backup-transact-sql?view=sql-server-2017" target="_blank" rel="noopener">BACKUP DATABASE</a> or <a href="https://docs.microsoft.com/en-us/sql/t-sql/statements/restore-statements-transact-sql?view=sql-server-2017" target="_blank" rel="noopener">RESTORE DATABASE</a> command, the percentage processed would have been piped to the Messages tab of Management Studio a little at a time. 10 percent processed. 20 percent processed. And so on as shown below.</p>



<pre class="wp-block-code"><code>BACKUP DATABASE wideworldimporters TO DISK = 'c:\temp\wwi.bak' WITH STATS;</code></pre>



<figure class="wp-block-image"><img loading="lazy" decoding="async" width="640" height="239" src="http://theserogroup.com/wp-content/uploads/2018/11/Messages.png" alt="" class="wp-image-2272" srcset="https://theserogroup.com/wp-content/uploads/2018/11/Messages.png 640w, https://theserogroup.com/wp-content/uploads/2018/11/Messages-300x112.png 300w" sizes="auto, (max-width: 640px) 100vw, 640px" /></figure>



<h2 class="wp-block-heading" id="h-sql-server-backup-restore-information-using-dmvs">SQL Server backup/restore information using DMVs</h2>



<p>Still, that doesn&#8217;t provide an estimate of how long the process is likely <g class="gr_ gr_12 gr-alert sel gr_gramm gr_replaced gr_inline_cards gr_disable_anim_appear Grammar only-ins replaceWithoutSep" id="12" data-gr-id="12">to </g>run. Fortunately, the Dynamic Management Views in SQL Server allow us to gather and report backup and restore estimates.</p>



<p>Here&#8217;s a script that I&#8217;ve found useful. It&#8217;s an amalgamation of <a href="https://jwebb.me/2Ppz0MR" target="_blank" rel="noopener">a script I found on StackOverflow</a> and my own handiwork. As always, this is for your reference only and no guarantees are implied.</p>



<pre class="wp-block-code"><code>SELECT SERVERPROPERTY('ServerName') AS &#91;Instance],<br>&nbsp;&nbsp; reqs.session_id,<br>&nbsp;&nbsp; sess.login_name,<br>&nbsp;&nbsp; reqs.command,<br>&nbsp;&nbsp; CAST(reqs.percent_complete AS NUMERIC(10, 2)) AS &#91;Percent Complete],<br>&nbsp;&nbsp; CONVERT(VARCHAR(20), DATEADD(ms, reqs.estimated_completion_time, GETDATE()), 20) AS &#91;Estimated Completion Time],<br>&nbsp;&nbsp; CAST(reqs.total_elapsed_time / 60000.0 AS NUMERIC(10, 2)) AS &#91;Elapsed Minutes],<br>&nbsp;&nbsp; CAST(reqs.estimated_completion_time / 60000.0 AS NUMERIC(10, 2)) AS &#91;Estimated Remaining Time in Minutes],<br>&nbsp;&nbsp; CAST(reqs.estimated_completion_time / 3600000.0 AS NUMERIC(10, 2)) AS &#91;Estimated Remaining Time in Hours],<br>&nbsp;&nbsp; CAST((<br>&nbsp;&nbsp;&nbsp;&nbsp; SELECT SUBSTRING(text, reqs.statement_start_offset/2,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CASE<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WHEN reqs.statement_end_offset = -1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; THEN 1000<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ELSE(reqs.statement_end_offset-reqs.statement_start_offset)/2<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; END)<br>&nbsp;&nbsp;&nbsp;&nbsp; FROM sys.dm_exec_sql_text(sql_handle)) AS VARCHAR(1000)) AS &#91;SQL]<br>FROM sys.dm_exec_requests AS reqs<br>&nbsp;JOIN sys.dm_exec_sessions AS sess ON sess.session_id = reqs.session_id<br>WHERE command IN('RESTORE DATABASE', 'BACKUP DATABASE');</code></pre>



<p>The script returns the following information:</p>



<ul class="wp-block-list">
<li>Instance Name</li>



<li>Session Id&nbsp;</li>



<li>Login Name</li>



<li>Command&nbsp;</li>



<li>Percent Complete</li>



<li>Estimated Completion Time</li>



<li>Elapsed Time in Minutes</li>



<li>Estimated Remaining Time in Minutes</li>



<li>Estimated Remaining Time in Hours</li>



<li>The Complete Command Used to Start Backup/Restore<br></li>
</ul>



<p>Since this script uses Dynamic Management Views, it won&#8217;t work on ancient versions of SQL Server. I&#8217;ve tested it on SQL Server 2008 R2 and forward on the Windows version. I haven&#8217;t tried it on SQL Server 2017 running on Linux.&nbsp;</p>



<p>Hopefully, you&#8217;ll find this script handy. Here are a few more that you may find helpful.</p>



<ul class="wp-block-list">
<li><a href="https://theserogroup.com/sql-server/hadr-options-for-sql-server/">High Availability and Disaster Recovery in SQL Server</a></li>



<li><a href="https://theserogroup.com/sql-server/how-to-create-sql-server-2019-failover-clustered-instances-in-azure/">How to Create SQL Server 2019 Failover Clustered Instances in Azure</a></li>



<li><a href="https://theserogroup.com/sql-server/vollgar-6-scripts-to-help-review-your-sql-servers/">Vollgar: 6 Scripts to Help Review Your SQL Servers</a></li>
</ul>



<h2 class="wp-block-heading" id="h-want-to-work-with-the-sero-group">Want to work with The Sero Group?</h2>



<p>Want to learn more about how SERO Group helps organizations take the guesswork out of managing their SQL Servers? It’s easy and there is no obligation.&nbsp;</p>



<p><a href="https://theserogroup.com/#contact" target="_blank" rel="noreferrer noopener">Schedule a call</a> with us to get started.</p>
<p>The post <a href="https://theserogroup.com/sql-server/script-how-long-until-my-sql-server-backup-restore-completes/">Script: How Long Until My SQL Server Backup/Restore Completes?</a> appeared first on <a href="https://theserogroup.com">The SERO Group</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://theserogroup.com/sql-server/script-how-long-until-my-sql-server-backup-restore-completes/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2269</post-id>	</item>
	</channel>
</rss>
