<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>SQL Server Scripts</title><link>https://www.sqlserver70.com/</link><description>Recent content on SQL Server Scripts</description><generator>Hugo -- gohugo.io</generator><language>en</language><copyright>SQLServer70.com</copyright><atom:link href="https://www.sqlserver70.com/index.xml" rel="self" type="application/rss+xml"/><item><title>Decode PLE and Buffer Cache Hit Ratio via cntr_type Codes</title><link>https://www.sqlserver70.com/post/page-life-expectancy-dm-os-performance-counters/</link><pubDate>Sun, 06 Sep 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/page-life-expectancy-dm-os-performance-counters/</guid><description>
&lt;p&gt;How many seconds should a data page sit in the buffer pool before &lt;a href="https://learn.microsoft.com/en-us/sql/sql-server/"&gt;SQL Server&lt;/a&gt; decides it isn't needed anymore, and where does that number actually come from? Page life expectancy is one row among hundreds returned by &lt;a href="https://learn.microsoft.com/en-us/sql/relational-databases/system-dynamic-management-objects/sys-dm-os-performance-counters-transact-sql?view=sql-server-ver17"&gt;sys.dm_os_performance_counters&lt;/a&gt;, and reading it correctly — along with its neighbor, buffer cache hit ratio — depends entirely on a column most ad hoc queries against this view skip past: &lt;code&gt;cntr_type&lt;/code&gt;. Get that column wrong and a perfectly healthy instance can look like it's in crisis, or the reverse.&lt;/p&gt;</description></item><item><title>Audit Server Configuration Drift with sys.configurations</title><link>https://www.sqlserver70.com/post/server-configuration-drift-sys-configurations/</link><pubDate>Mon, 24 Aug 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/server-configuration-drift-sys-configurations/</guid><description>
&lt;p&gt;Running &lt;code&gt;sp_configure&lt;/code&gt; and getting back a success message is not the same thing as a setting actually taking effect. Between the moment a value is changed and the moment &lt;a href="https://learn.microsoft.com/en-us/sql/sql-server/"&gt;SQL Server&lt;/a&gt; is actually running it sits a gap — one a busy DBA can close with a single &lt;code&gt;RECONFIGURE&lt;/code&gt;, or forget entirely until a restart months later flips a setting no one meant to touch yet. &lt;a href="https://learn.microsoft.com/en-us/sql/relational-databases/system-catalog-views/sys-configurations-transact-sql"&gt;sys.configurations&lt;/a&gt; is where that gap is visible: its &lt;code&gt;value&lt;/code&gt; and &lt;code&gt;value_in_use&lt;/code&gt; columns don't have to match, and when they don't, this script says exactly why not and what to do about it.&lt;/p&gt;</description></item><item><title>Find Ad Hoc Plan Cache Bloat with sys.dm_exec_cached_plans</title><link>https://www.sqlserver70.com/post/adhoc-plan-cache-bloat-dm-exec-cached-plans/</link><pubDate>Thu, 13 Aug 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/adhoc-plan-cache-bloat-dm-exec-cached-plans/</guid><description>
&lt;p&gt;Not every plan in SQL Server's cache earns its place. A query compiled once — because its literal values were hard-coded into the text instead of passed as parameters — gets a full compiled plan cached right alongside plans an application reuses thousands of times a day. &lt;a href="https://learn.microsoft.com/en-us/sql/relational-databases/system-dynamic-management-objects/sys-dm-exec-cached-plans-transact-sql"&gt;sys.dm_exec_cached_plans&lt;/a&gt; is where that difference becomes measurable: &lt;code&gt;usecounts&lt;/code&gt; shows whether a plan was ever looked up again, and &lt;code&gt;size_in_bytes&lt;/code&gt; shows exactly how many bytes it's still holding in memory for the privilege of never running twice.&lt;/p&gt;</description></item><item><title>Age Open Transactions with sys.dm_tran_active_transactions</title><link>https://www.sqlserver70.com/post/long-open-transactions-dm-tran-active-transactions/</link><pubDate>Sun, 02 Aug 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/long-open-transactions-dm-tran-active-transactions/</guid><description>
&lt;p&gt;A transaction that never commits keeps every log record written after it pinned in place, no matter how much work piles up behind it. &lt;code&gt;sys.dm_tran_active_transactions&lt;/code&gt; lists every transaction currently open on the instance, but on its own it only shows a transaction ID and a start time — pairing it with &lt;code&gt;sys.dm_tran_session_transactions&lt;/code&gt; and &lt;code&gt;sys.dm_exec_sessions&lt;/code&gt; turns that anonymous row into a specific session, login, and elapsed duration worth investigating.&lt;/p&gt;
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;h2 id="purpose-and-overview"&gt;Purpose and Overview&lt;/h2&gt;
&lt;p&gt;The transaction log can only truncate up to the oldest point still needed for recovery, and an open transaction — no matter how small the actual data change behind it — holds that point fixed wherever it began. A transaction log backup running on schedule and a &lt;code&gt;CHECKPOINT&lt;/code&gt; completing normally free nothing if a session opened a transaction ten minutes ago and hasn't committed or rolled back since. The log keeps growing, autogrow events start firing, and the eventual full-log error traces back to a single session that most routine monitoring never surfaces directly.&lt;/p&gt;</description></item><item><title>Rank Expensive Queries with sys.query_store_runtime_stats</title><link>https://www.sqlserver70.com/post/query-store-top-resource-queries-runtime-stats/</link><pubDate>Sun, 26 Jul 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/query-store-top-resource-queries-runtime-stats/</guid><description>
&lt;p&gt;Query Store buckets runtime statistics into fixed time intervals, which means a query that ran steadily all week doesn't produce one row of history — it produces one row per interval, split further by execution outcome. Averaging those rows unweighted is how a &amp;quot;most expensive queries&amp;quot; report quietly gets the ranking wrong on a busy instance. &lt;code&gt;sys.query_store_runtime_stats&lt;/code&gt; is the catalog view holding that interval-level detail, and joined correctly to plan, query, and query text — with a weighted rollup across execution counts — it produces the resource-consumption ranking a DBA actually wants.&lt;/p&gt;</description></item><item><title>Rank Top Memory Consumers with sys.dm_os_memory_clerks</title><link>https://www.sqlserver70.com/post/top-memory-consumers-dm-os-memory-clerks/</link><pubDate>Mon, 20 Jul 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/top-memory-consumers-dm-os-memory-clerks/</guid><description>
&lt;p&gt;Buffer pool counters explain data cache pressure but go silent the moment memory trouble comes from somewhere else. &lt;code&gt;sys.dm_os_memory_clerks&lt;/code&gt; is the DMV that covers everything the buffer pool doesn't — plan cache, In-Memory OLTP, Query Store, query memory grants, Extended Events sessions — by reporting &lt;code&gt;pages_kb&lt;/code&gt; per clerk. Grouping those rows by &lt;code&gt;type&lt;/code&gt; produces a ranked list of exactly which internal SQL Server subsystem is holding the most memory right now.&lt;/p&gt;</description></item><item><title>Break Down Buffer Pool Memory by Database</title><link>https://www.sqlserver70.com/post/buffer-pool-memory-by-database-dm-os-buffer-descriptors/</link><pubDate>Sun, 12 Jul 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/buffer-pool-memory-by-database-dm-os-buffer-descriptors/</guid><description>
&lt;p&gt;&lt;code&gt;sys.dm_os_buffer_descriptors&lt;/code&gt; is the DMV that maps SQL Server's data cache down to the individual page level — one row per 8 KB page currently held in the buffer pool, tagged with the &lt;code&gt;database_id&lt;/code&gt; and &lt;code&gt;file_id&lt;/code&gt; it belongs to. Rolling those rows up with a simple &lt;code&gt;GROUP BY database_id&lt;/code&gt; turns millions of individual page entries into a short, sortable list showing exactly how much of the instance's memory each database is holding onto right now.&lt;/p&gt;</description></item><item><title>Measure Disk I/O Performance per Database File</title><link>https://www.sqlserver70.com/post/sql-server-disk-io-performance-by-database-file/</link><pubDate>Fri, 03 Jul 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-disk-io-performance-by-database-file/</guid><description>
&lt;p&gt;I/O latency is where disk bottlenecks hide. SQL Server accumulates every millisecond of file-level read and write wait in &lt;code&gt;sys.dm_io_virtual_file_stats&lt;/code&gt; — one row per database file, reset at each service restart. Joining that DMV to &lt;code&gt;sys.master_files&lt;/code&gt; replaces raw file IDs with physical paths and file types, and dividing accumulated stall by operation count converts totals into the average milliseconds per read and per write that a storage or DBA team can act on.&lt;/p&gt;</description></item><item><title>Detect Suspect Database Pages with msdb.dbo.suspect_pages</title><link>https://www.sqlserver70.com/post/sql-server-suspect-database-pages-detection-script/</link><pubDate>Thu, 02 Jul 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-suspect-database-pages-detection-script/</guid><description>
&lt;p&gt;Storage errors do not always surface as immediate failures. When SQL Server encounters an 823, 824, or 825 error reading or writing a database page, it records the offending page in &lt;code&gt;msdb.dbo.suspect_pages&lt;/code&gt; — a system table that accumulates page-level I/O failures across service restarts. Querying this table is the fastest first-pass for any corruption investigation, answering whether bad pages exist and which databases are affected before committing to a full &lt;code&gt;DBCC CHECKDB&lt;/code&gt; run.&lt;/p&gt;</description></item><item><title>Configure Database Mail and Send a Test Message</title><link>https://www.sqlserver70.com/post/sql-server-database-mail-configuration-and-test-script/</link><pubDate>Wed, 01 Jul 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-database-mail-configuration-and-test-script/</guid><description>
&lt;p&gt;Database Mail ships disabled; enabling it and wiring up an SMTP account, a profile, and a public principal grant are the steps that stand between a fresh SQL Server instance and &lt;code&gt;sp_send_dbmail&lt;/code&gt; working reliably. The configuration script below executes all four setup calls inside &lt;code&gt;msdb&lt;/code&gt;, fires a test message, and queries &lt;code&gt;msdb.dbo.sysmail_event_log&lt;/code&gt; to confirm the message reached the SMTP relay — all in one repeatable script.&lt;/p&gt;
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;h2 id="purpose-and-overview"&gt;Purpose and Overview&lt;/h2&gt;
&lt;p&gt;Sending email from SQL Server — for Agent job failures, custom monitoring alerts, or ad hoc DBA notifications — runs through a single pathway: an SMTP account registered in &lt;code&gt;msdb&lt;/code&gt;, wrapped in a named profile, and exposed to the Database Mail service via a principal-profile grant. When any of those three links is missing or misconfigured, &lt;code&gt;sp_send_dbmail&lt;/code&gt; accepts the call and silently queues the message, but the mail never leaves the server. The only diagnostic path is &lt;code&gt;msdb.dbo.sysmail_event_log&lt;/code&gt;.&lt;/p&gt;</description></item><item><title>Apply Row and Page Compression to Tables and Indexes</title><link>https://www.sqlserver70.com/post/sql-server-compress-tables-and-indexes-script/</link><pubDate>Tue, 30 Jun 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-compress-tables-and-indexes-script/</guid><description>
&lt;p&gt;Storage budgets shrink faster than databases grow. Uncompressed tables and indexes often carry two to four times the physical page count that row or page compression would require — a gap that widens with every batch load or continuous INSERT. &lt;code&gt;sys.dm_db_index_physical_stats&lt;/code&gt; joined to &lt;code&gt;sys.partitions&lt;/code&gt; surfaces every uncompressed index and heap in a database in one pass, and the script below converts that inventory into ready-to-execute &lt;code&gt;ALTER INDEX REBUILD&lt;/code&gt; and &lt;code&gt;ALTER TABLE REBUILD&lt;/code&gt; statements sorted by object size so the highest-impact targets come first.&lt;/p&gt;</description></item><item><title>List All Tables Across All Databases in SQL Server</title><link>https://www.sqlserver70.com/post/list-all-tables-across-all-databases-in-sql-server/</link><pubDate>Mon, 29 Jun 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/list-all-tables-across-all-databases-in-sql-server/</guid><description>
&lt;p&gt;A cross-database table inventory is one of the most common ad hoc requests in any SQL Server environment — and one of the few queries that cannot be answered by staying inside a single database context. &lt;code&gt;sys.databases&lt;/code&gt; exposes every online database on the instance; three-part name references to &lt;code&gt;sys.tables&lt;/code&gt; inside dynamic SQL pull the user-table list from each one. The script below accumulates results in a temp table and returns them sorted by database, schema, and table name, with a second variant that narrows results to a specific table name for cross-database search.&lt;/p&gt;</description></item><item><title>System-Versioned (Temporal) Tables in SQL Server</title><link>https://www.sqlserver70.com/post/sql-server-system-versioned-temporal-tables/</link><pubDate>Fri, 19 Jun 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-system-versioned-temporal-tables/</guid><description>
&lt;p&gt;Every &lt;code&gt;UPDATE&lt;/code&gt; and &lt;code&gt;DELETE&lt;/code&gt; on a system-versioned table leaves behind a complete, queryable history of the previous row — automatically, with no triggers and no audit-table plumbing. SQL Server records the period each row version was valid and answers point-in-time questions through a single &lt;code&gt;FOR SYSTEM_TIME&lt;/code&gt; clause. This is the temporal-tables feature, available since SQL Server 2016.&lt;/p&gt;
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;h2 id="purpose-and-overview"&gt;Purpose and Overview&lt;/h2&gt;
&lt;p&gt;A system-versioned temporal table is a user table that keeps a full history of data changes so you can analyze the data as it existed at any point in the past, not just as it is now. The feature is implemented as a pair of tables: the &lt;em&gt;current&lt;/em&gt; table holds the present rows, and a linked &lt;em&gt;history&lt;/em&gt; table stores prior versions. The Database Engine manages the relationship — on every update or delete, it closes the old row in the history table and stamps a new period of validity.&lt;/p&gt;</description></item><item><title>SQL Server Database Schema and Data Dictionary Queries</title><link>https://www.sqlserver70.com/post/sql-server-database-schema-data-dictionary-queries/</link><pubDate>Thu, 18 Jun 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-database-schema-data-dictionary-queries/</guid><description>
&lt;p&gt;Documenting a database starts with a data dictionary: every table, every column, its data type, length, nullability, and default. SQL Server exposes this through the ANSI-standard &lt;code&gt;INFORMATION_SCHEMA&lt;/code&gt; views, which produce a portable schema report that reads almost like English and runs unchanged on other ISO-compliant database engines.&lt;/p&gt;
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;h2 id="purpose-and-overview"&gt;Purpose and Overview&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;INFORMATION_SCHEMA&lt;/code&gt; is a set of views defined by the ISO SQL standard and implemented in a special schema present in every SQL Server database. Their purpose is insulation: they present an internal, system-table-independent view of metadata, so applications keep working even when the underlying system tables change between releases. That portability is the main reason to reach for them — a data-dictionary query written against &lt;code&gt;INFORMATION_SCHEMA&lt;/code&gt; will run on SQL Server, PostgreSQL, MySQL, and other engines that implement the standard.&lt;/p&gt;</description></item><item><title>How to Update Statistics in SQL Server</title><link>https://www.sqlserver70.com/post/sql-server-update-statistics-guide/</link><pubDate>Wed, 17 Jun 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-update-statistics-guide/</guid><description>
&lt;p&gt;A query that ran in milliseconds last month now scans a million rows and spills to tempdb. Nothing changed in the code — but the data grew, and the statistics the optimizer relies on went stale, so its row estimates drifted far from reality and it chose a bad plan. Refreshing statistics is the fix, and knowing how and when to do it is core DBA work.&lt;/p&gt;
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;h2 id="purpose-and-overview"&gt;Purpose and Overview&lt;/h2&gt;
&lt;p&gt;SQL Server's query optimizer is cost-based: it estimates how many rows each operator will process and picks a plan accordingly. Those estimates come from statistics objects — histograms and density vectors that describe the distribution of values in a column or index. When data changes but statistics do not, estimates degrade, and the optimizer can choose nested loops where a hash join belongs, or under-size a memory grant.&lt;/p&gt;</description></item><item><title>sp_pkeys and Primary Key Metadata in SQL Server</title><link>https://www.sqlserver70.com/post/sql-server-sp-pkeys-primary-key-metadata/</link><pubDate>Tue, 16 Jun 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-sp-pkeys-primary-key-metadata/</guid><description>
&lt;p&gt;Which columns make up a table's primary key, and in what order are they declared? For a single table, the system stored procedure &lt;code&gt;sp_pkeys&lt;/code&gt; answers in one line; for a database-wide inventory, the catalog views behind it give you full control. This post covers both, from the quick lookup to the complete metadata query.&lt;/p&gt;
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;h2 id="purpose-and-overview"&gt;Purpose and Overview&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;sp_pkeys&lt;/code&gt; returns primary key information for one table in the current database. It is an ODBC-catalog procedure — the T-SQL equivalent of the &lt;code&gt;SQLPrimaryKeys&lt;/code&gt; ODBC function — and its result set is deliberately compact: &lt;code&gt;TABLE_QUALIFIER&lt;/code&gt;, &lt;code&gt;TABLE_OWNER&lt;/code&gt;, &lt;code&gt;TABLE_NAME&lt;/code&gt;, &lt;code&gt;COLUMN_NAME&lt;/code&gt;, &lt;code&gt;KEY_SEQ&lt;/code&gt;, and &lt;code&gt;PK_NAME&lt;/code&gt;. The &lt;code&gt;KEY_SEQ&lt;/code&gt; column is the one that matters most: it gives the sequence number of each column within a multicolumn primary key, so a composite key returns one row per column in declared order.&lt;/p&gt;</description></item><item><title>sysindexes vs sys.indexes: Legacy and Modern Catalog Views</title><link>https://www.sqlserver70.com/post/sql-server-sysindexes-vs-sys-indexes-catalog-views/</link><pubDate>Mon, 15 Jun 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-sysindexes-vs-sys-indexes-catalog-views/</guid><description>
&lt;p&gt;Plenty of SQL Server scripts still in circulation read &lt;code&gt;rowcnt&lt;/code&gt; and &lt;code&gt;dpages&lt;/code&gt; straight out of &lt;code&gt;sysindexes&lt;/code&gt; — a habit inherited from SQL Server 2000, where that table was the canonical place to find row counts and page totals. The object survives only as the &lt;code&gt;sys.sysindexes&lt;/code&gt; compatibility view, is flagged for removal, and returns inaccurate numbers for partitioned tables. This post shows the modern replacement and how to migrate.&lt;/p&gt;
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;h2 id="purpose-and-overview"&gt;Purpose and Overview&lt;/h2&gt;
&lt;p&gt;In SQL Server 2000, &lt;code&gt;sysindexes&lt;/code&gt; was a real system table with one row per index and heap, and DBAs leaned on its &lt;code&gt;rowcnt&lt;/code&gt;, &lt;code&gt;dpages&lt;/code&gt;, &lt;code&gt;reserved&lt;/code&gt;, and &lt;code&gt;used&lt;/code&gt; columns for quick size and row-count reports. SQL Server 2005 rebuilt the metadata layer, and &lt;code&gt;sysindexes&lt;/code&gt; was retained only as &lt;code&gt;sys.sysindexes&lt;/code&gt;, a backward-compatibility view. Microsoft's documentation is explicit: this feature is included for backward compatibility, will be removed in a future version, and should not be used in new development.&lt;/p&gt;</description></item><item><title>SQL Server Catalog Views: sys.tables, sys.indexes, sys.objects</title><link>https://www.sqlserver70.com/post/sql-server-system-catalog-views-sys-tables-indexes-objects/</link><pubDate>Sun, 14 Jun 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-system-catalog-views-sys-tables-indexes-objects/</guid><description>
&lt;p&gt;The &lt;code&gt;sys&lt;/code&gt; catalog views are the supported, forward-compatible way to read SQL Server metadata, and three of them — &lt;code&gt;sys.objects&lt;/code&gt;, &lt;code&gt;sys.tables&lt;/code&gt;, and &lt;code&gt;sys.indexes&lt;/code&gt; — answer most day-to-day questions about what lives in a database. This script joins them into one inventory of every user table, its schema, its row count, and each index defined on it.&lt;/p&gt;
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;h2 id="purpose-and-overview"&gt;Purpose and Overview&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;sys.objects&lt;/code&gt; is the base catalog view: it contains one row for every user-defined, schema-scoped object in a database — tables, views, stored procedures, functions, constraints, triggers, and more — each identified by a unique &lt;code&gt;object_id&lt;/code&gt;. &lt;code&gt;sys.tables&lt;/code&gt; is a &lt;em&gt;derived&lt;/em&gt; view built on top of it. It returns every column &lt;code&gt;sys.objects&lt;/code&gt; returns, plus table-specific columns such as &lt;code&gt;temporal_type&lt;/code&gt; and &lt;code&gt;is_memory_optimized&lt;/code&gt;, but only for rows where the object is a user table. &lt;code&gt;sys.indexes&lt;/code&gt; then carries one row per index or heap of each tabular object, keyed back to the table by &lt;code&gt;object_id&lt;/code&gt;.&lt;/p&gt;</description></item><item><title>SQL Server DBCC Commands: The Complete DBA Reference Guide</title><link>https://www.sqlserver70.com/post/sql-server-dbcc-commands-reference/</link><pubDate>Mon, 08 Jun 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-dbcc-commands-reference/</guid><description>
&lt;p&gt;Every SQL Server instance ships with a parallel command vocabulary that never appears in an ORM, a stored procedure, or an application query: the DBCC statements. They validate physical page structure, reclaim file space, evict cached plans, reseed identity columns, and report internal counters that no &lt;code&gt;SELECT&lt;/code&gt; exposes — and a DBA who does not know which one to reach for in an incident reaches for the wrong one. This guide maps the DBCC surface area by family, shows a representative statement for each, and links to deep dives on the commands you run most.&lt;/p&gt;</description></item><item><title>SQL Server DBCC CHECKDB: Complete Guide and Repair Options</title><link>https://www.sqlserver70.com/post/sql-server-dbcc-checkdb-repair-options/</link><pubDate>Sun, 07 Jun 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-dbcc-checkdb-repair-options/</guid><description>
&lt;p&gt;Corruption rarely announces itself. A bit flips on a SAN, a controller loses power mid-write, a driver mishandles a flush — and the damaged page sits unread for weeks until a query finally touches it and the database throws error 824. By then the corruption may already be inside every backup you keep. &lt;code&gt;DBCC CHECKDB&lt;/code&gt; is the one statement that finds the damage early, while you still have a clean restore point, and this guide covers how to run it and what its repair options actually do.&lt;/p&gt;</description></item><item><title>SQL Server DBCC FREEPROCCACHE: Clear the Plan Cache Safely</title><link>https://www.sqlserver70.com/post/sql-server-dbcc-freeproccache-clear-plan-cache/</link><pubDate>Sat, 06 Jun 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-dbcc-freeproccache-clear-plan-cache/</guid><description>
&lt;p&gt;A stored procedure that returned in 20 milliseconds yesterday is timing out today, and nothing in the code changed. The data did not grow meaningfully, the indexes are intact, and statistics look current — yet the query is suddenly scanning a million rows it used to seek. The usual culprit is a cached execution plan compiled for the wrong parameter value, and &lt;code&gt;DBCC FREEPROCCACHE&lt;/code&gt; is the scalpel that removes it. The danger is using it as a sledgehammer.&lt;/p&gt;</description></item><item><title>SQL Server DBCC SHRINKDATABASE: When and When Not to Shrink</title><link>https://www.sqlserver70.com/post/sql-server-dbcc-shrinkdatabase-when-to-shrink/</link><pubDate>Fri, 05 Jun 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-dbcc-shrinkdatabase-when-to-shrink/</guid><description>
&lt;p&gt;A one-time data load doubled a data file overnight, the load is done, the rows are deleted, and the file is now mostly empty space. The reflex — reach for &lt;code&gt;DBCC SHRINKDATABASE&lt;/code&gt; and give the disk back — is one of the most common self-inflicted performance wounds in SQL Server administration. This post explains exactly what shrinking does to your indexes, when it is genuinely the right call, and how to reclaim space the few times you must without wrecking performance.&lt;/p&gt;</description></item><item><title>SQL Server DBCC CHECKIDENT: Check and Reseed Identity</title><link>https://www.sqlserver70.com/post/sql-server-dbcc-checkident-reseed-identity-columns/</link><pubDate>Thu, 04 Jun 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-dbcc-checkident-reseed-identity-columns/</guid><description>
&lt;p&gt;A logging table grew to forty million rows, the bulk of it was archived out, and now only the last week's data remains — but the next insert still gets identity value 40,000,001, leaving a vast gap below it. Or a botched data load left the seed sitting far above the real maximum, and future inserts skip millions of values. &lt;code&gt;DBCC CHECKIDENT&lt;/code&gt; closes both gaps in a single statement, and knowing how to check before you reseed keeps it from creating a worse problem than it solves.&lt;/p&gt;</description></item><item><title>DBCC DBREINDEX vs ALTER INDEX REBUILD in SQL Server</title><link>https://www.sqlserver70.com/post/sql-server-dbcc-dbreindex-vs-alter-index-rebuild/</link><pubDate>Wed, 03 Jun 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-dbcc-dbreindex-vs-alter-index-rebuild/</guid><description>
&lt;p&gt;Open the SQL Agent job history on an instance that has been running for a decade and you will still find &lt;code&gt;DBCC DBREINDEX&lt;/code&gt; in a nightly maintenance step, quietly doing its job. It works — which is exactly why nobody has touched it. But it has been deprecated for the better part of twenty years, it locks tables it does not need to lock, and it cannot do the things modern index maintenance takes for granted. This post compares it to its replacement and shows the one-afternoon migration to &lt;code&gt;ALTER INDEX&lt;/code&gt;.&lt;/p&gt;</description></item><item><title>SQL Server Duplicate and Overlapping Index Detection Script</title><link>https://www.sqlserver70.com/post/sql-server-duplicate-and-overlapping-index-detection/</link><pubDate>Mon, 25 May 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-duplicate-and-overlapping-index-detection/</guid><description>
&lt;p&gt;Duplicate and overlapping indexes waste storage, slow down every write, and split optimizer statistics across redundant copies. This T-SQL script compares each non-heap index in the current database on its first sixteen key columns and surfaces any pair of indexes that overlap, giving the DBA a short list of cleanup candidates.&lt;/p&gt;
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;h2 id="purpose-and-overview"&gt;Purpose and Overview&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;sp_BlitzIndex&lt;/code&gt; does deep usage and statistics analysis across every index. &lt;code&gt;sp_SQLskills_finddupes&lt;/code&gt; factors in INCLUDE columns and sort direction. The script below fills a narrower role between them: a portable, dependency-free first pass for the single most common form of index redundancy — two non-heap indexes on the same table that share a leading key column. The output is a candidate list ready for human triage, not a drop list to apply unattended.&lt;/p&gt;</description></item><item><title>SQL Server Get Table and Index Storage Size Report Script</title><link>https://www.sqlserver70.com/post/sql-server-get-table-and-index-storage-size/</link><pubDate>Sun, 24 May 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-get-table-and-index-storage-size/</guid><description>
&lt;p&gt;Knowing how much space each table and each index consumes is the foundation of capacity planning, archive policy, and index cleanup decisions on any SQL Server instance. This T-SQL script reads the partition-level storage statistics out of &lt;code&gt;sys.dm_db_partition_stats&lt;/code&gt; and reports the size of every index — clustered, non-clustered, and the heap row that represents the table itself — in both kilobytes and megabytes, schema- and table-named for easy review.&lt;/p&gt;
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;h2 id="purpose-and-overview"&gt;Purpose and Overview&lt;/h2&gt;
&lt;p&gt;Backup windows that creep, restore tests that get skipped, archive policies that never get written — all three trace back to the same missing input: a current, per-object inventory of bytes-on-disk. &lt;code&gt;sys.dm_db_partition_stats&lt;/code&gt; carries the page counts that answer the question; &lt;code&gt;sp_spaceused&lt;/code&gt; only aggregates at the table or database level and loses the index-by-index detail this kind of capacity work needs. The query below converts those page counts to KB and MB for every index and every table in the current database, ready to paste into the next capacity-planning spreadsheet.&lt;/p&gt;</description></item><item><title>SQL Server Object Dependencies Report: Find All References</title><link>https://www.sqlserver70.com/post/sql-server-object-dependencies-script/</link><pubDate>Sat, 23 May 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-object-dependencies-script/</guid><description>
&lt;p&gt;Before renaming a column, retiring a view, or dropping a stored procedure, every DBA needs the answer to one question: what else depends on this object? This T-SQL script reads &lt;code&gt;sys.sql_expression_dependencies&lt;/code&gt; and reports every referencing-and-referenced pair in the current database, with optional filtering to a single object name — the safe-refactoring impact analysis report.&lt;/p&gt;
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;h2 id="purpose-and-overview"&gt;Purpose and Overview&lt;/h2&gt;
&lt;p&gt;SQL Server tracks expression-level dependencies between user objects in the catalog view &lt;code&gt;sys.sql_expression_dependencies&lt;/code&gt;. Every reference from a stored procedure, function, view, trigger, default, computed column, or check constraint to another object — table, view, function, procedure, type, or column — produces a row. The view supersedes the legacy &lt;code&gt;sys.sql_dependencies&lt;/code&gt; view (deprecated in SQL Server 2008) and the older &lt;code&gt;sp_depends&lt;/code&gt; stored procedure (which has always been unreliable for cross-database and late-bound references). Modern dependency tracking is metadata-only, schema-bound-aware, and accurate across cross-database name resolutions.&lt;/p&gt;</description></item><item><title>SQL Server Find Columns by Data Type Across the Database</title><link>https://www.sqlserver70.com/post/sql-server-find-columns-by-data-type/</link><pubDate>Fri, 22 May 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-find-columns-by-data-type/</guid><description>
&lt;p&gt;&amp;quot;Where is every &lt;code&gt;datetime&lt;/code&gt; column?&amp;quot; — &amp;quot;Which tables still use &lt;code&gt;text&lt;/code&gt; instead of &lt;code&gt;varchar(max)&lt;/code&gt;?&amp;quot; — &amp;quot;Are any columns using a deprecated user-defined type?&amp;quot; These are recurring DBA questions during schema audits, deprecation sweeps, and pre-migration assessments. This T-SQL script joins &lt;code&gt;sys.columns&lt;/code&gt; to &lt;code&gt;sys.types&lt;/code&gt;, &lt;code&gt;sys.tables&lt;/code&gt;, and &lt;code&gt;sys.schemas&lt;/code&gt; to produce the canonical answer: every column in the current database with its full type signature.&lt;/p&gt;
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;h2 id="purpose-and-overview"&gt;Purpose and Overview&lt;/h2&gt;
&lt;p&gt;Which columns survived the move from &lt;code&gt;text&lt;/code&gt; to &lt;code&gt;varchar(max)&lt;/code&gt;? Which fields still carry the deprecated &lt;code&gt;Money_T&lt;/code&gt; UDT three releases after it was officially retired? Did a recent merge silently reintroduce &lt;code&gt;datetime&lt;/code&gt; on a column the design review thought it had migrated to &lt;code&gt;datetime2&lt;/code&gt;? A single catalog-view query against &lt;code&gt;sys.columns&lt;/code&gt; and &lt;code&gt;sys.types&lt;/code&gt; answers all three — and every other question of the form &amp;quot;where is every column of type X in this database?&amp;quot; — without an INFORMATION_SCHEMA round trip or a manual table-by-table sweep.&lt;/p&gt;</description></item><item><title>SQL Server List All Foreign Keys with Referenced Tables</title><link>https://www.sqlserver70.com/post/sql-server-list-all-foreign-keys-and-referenced-tables/</link><pubDate>Thu, 21 May 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-list-all-foreign-keys-and-referenced-tables/</guid><description>
&lt;p&gt;Every database with foreign keys eventually needs a single inventory: every constraint, its parent table and column, and the referenced table and column on the other side. This T-SQL script joins &lt;code&gt;sys.foreign_keys&lt;/code&gt; to &lt;code&gt;sys.foreign_key_columns&lt;/code&gt;, &lt;code&gt;sys.tables&lt;/code&gt;, and &lt;code&gt;sys.columns&lt;/code&gt; to produce the canonical foreign-key map of the current database — one row per column in every multi-column foreign key.&lt;/p&gt;
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;h2 id="purpose-and-overview"&gt;Purpose and Overview&lt;/h2&gt;
&lt;p&gt;The first symptom that a referential boundary has slipped is usually subtle: a &lt;code&gt;DELETE&lt;/code&gt; on a parent table that ran in seconds last quarter now takes minutes, or a join across the foreign key plans an unexplained scan on the child side. Almost every diagnosis trails back to the same short list — a constraint disabled by an unattended deployment, a NOT TRUSTED flag left behind by a &lt;code&gt;WITH NOCHECK&lt;/code&gt; bulk load, or a duplicate FK shadowing the one the optimizer was relying on. The inventory query below — &lt;code&gt;sys.foreign_keys&lt;/code&gt; joined to &lt;code&gt;sys.foreign_key_columns&lt;/code&gt;, &lt;code&gt;sys.tables&lt;/code&gt;, and &lt;code&gt;sys.columns&lt;/code&gt; — produces the complete relationship map in a single pass, so the foreign key in question is one filter away.&lt;/p&gt;</description></item><item><title>SQL Server VLF Count Report: Virtual Log File Analysis</title><link>https://www.sqlserver70.com/post/sql-server-vlf-count-report/</link><pubDate>Mon, 18 May 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-vlf-count-report/</guid><description>
&lt;p&gt;Excessive virtual log files (VLFs) inside the transaction log slow down database recovery, backup, and restore operations — often by minutes on large logs — and the problem compounds silently every time autogrowth fires with a small increment. This script queries DBCC LOGINFO across every online database on the instance, counts VLFs and active VLFs per database, and assigns a health assessment so DBAs can prioritize remediation.&lt;/p&gt;
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;h2 id="purpose-and-overview"&gt;Purpose and Overview&lt;/h2&gt;
&lt;p&gt;A SQL Server transaction log is internally divided into virtual log files. SQL Server creates VLFs automatically when the log file grows, and the number of VLFs per growth event depends on the size of the growth increment — small increments create fewer but more numerous VLFs over time, while a single large pre-allocation creates just a handful. When a log accumulates hundreds or thousands of VLFs, SQL Server must scan every VLF header during recovery, log backup, and restore, adding overhead that scales linearly with VLF count. Paul S. Randal of SQLskills.com documented the authoritative research on VLF overhead and the recommended thresholds in his foundational VLF series.&lt;/p&gt;</description></item><item><title>SQL Server Database File Space Usage Report</title><link>https://www.sqlserver70.com/post/sql-server-database-file-space-usage-report/</link><pubDate>Sun, 17 May 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-database-file-space-usage-report/</guid><description>
&lt;p&gt;Running out of space inside a data or log file halts SQL Server transactions immediately — and discovering that a volume is full only after an outage is too late. This script queries &lt;code&gt;sys.master_files&lt;/code&gt; and &lt;code&gt;sys.dm_os_volume_stats&lt;/code&gt; across every online database on the instance to report each file's allocated size, space actually written, free space inside the file, percent used, autogrowth configuration, and the amount of free space remaining on the underlying disk volume.&lt;/p&gt;</description></item><item><title>SQL Server Max Server Memory Calculator Script</title><link>https://www.sqlserver70.com/post/sql-server-max-server-memory-calculator-script/</link><pubDate>Sat, 16 May 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-max-server-memory-calculator-script/</guid><description>
&lt;p&gt;Every SQL Server installation ships with &lt;code&gt;max server memory&lt;/code&gt; set to its default value of 2,147,483,647 MB — effectively unlimited — which allows the Buffer Pool to consume all available RAM and starve the operating system of the memory it needs for network stack, disk I/O, and other critical processes. This script reads total physical RAM from &lt;code&gt;sys.dm_os_sys_info&lt;/code&gt;, applies Jonathan Kehayias's widely-adopted OS reservation formula, and compares the recommended cap to the current &lt;code&gt;sp_configure&lt;/code&gt; setting so DBAs can see at a glance whether action is needed.&lt;/p&gt;</description></item><item><title>SQL Server Identify Heap Tables Without Clustered Indexes</title><link>https://www.sqlserver70.com/post/sql-server-identify-heap-tables-without-clustered-indexes/</link><pubDate>Fri, 15 May 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-identify-heap-tables-without-clustered-indexes/</guid><description>
&lt;p&gt;A heap table — a table with no clustered index — stores rows in no particular order, forces full table scans for most non-indexed queries, and accumulates forwarded records every time an UPDATE causes a row to outgrow its page. This script queries &lt;code&gt;sys.tables&lt;/code&gt;, &lt;code&gt;sys.indexes&lt;/code&gt;, &lt;code&gt;sys.partitions&lt;/code&gt;, &lt;code&gt;sys.allocation_units&lt;/code&gt;, and &lt;code&gt;sys.dm_db_index_physical_stats&lt;/code&gt; to identify every heap in the current database, report its size and row count, and surface forwarded record counts so DBAs can prioritize which heaps need a clustered index.&lt;/p&gt;</description></item><item><title>SQL Server Copy-Only Backup Script for Dev Restore</title><link>https://www.sqlserver70.com/post/sql-server-copy-only-backup-script-dev-restore/</link><pubDate>Mon, 20 Apr 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-copy-only-backup-script-dev-restore/</guid><description>
&lt;p&gt;A copy-only backup is a special SQL Server backup that is independent of the normal backup sequence. It does not reset the differential base or interrupt the transaction log backup chain, making it the correct choice when you need an ad-hoc backup for a development or test environment restore without affecting production recovery options.&lt;/p&gt;
&lt;h2 id="purpose-and-overview"&gt;Purpose and Overview&lt;/h2&gt;
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;p&gt;In a standard SQL Server backup strategy, full backups establish a new differential base and log backups form a continuous chain. If you take an unplanned full backup mid-schedule, the next differential backup becomes larger than expected because the base changed. If you take an unplanned log backup, you may break an automated restore chain that depends on log sequence numbers.&lt;/p&gt;</description></item><item><title>SQL Server: Find Databases With No Recent Backup</title><link>https://www.sqlserver70.com/post/sql-server-find-databases-no-recent-backup/</link><pubDate>Sun, 19 Apr 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-find-databases-no-recent-backup/</guid><description>
&lt;p&gt;Finding databases that have not been backed up recently is one of the most important checks a DBA can run. A database that grows unnoticed without a backup is a data loss event waiting to happen. This script queries &lt;code&gt;msdb.dbo.backupset&lt;/code&gt; against &lt;code&gt;sys.databases&lt;/code&gt; to surface any online user database with no full backup in the last 24 hours, or that has never been backed up at all.&lt;/p&gt;
&lt;h2 id="purpose-and-overview"&gt;Purpose and Overview&lt;/h2&gt;
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;p&gt;SQL Server does not alert you by default when a database backup is overdue. If a scheduled backup job fails silently or a new database is created without being added to the backup plan, that database will accumulate changes with no recovery point. The only way to detect this proactively is to query the backup history in &lt;code&gt;msdb&lt;/code&gt;.&lt;/p&gt;</description></item><item><title>SQL Server Foreign Keys Without Indexes Script</title><link>https://www.sqlserver70.com/post/sql-server-foreign-keys-without-indexes-script/</link><pubDate>Sat, 18 Apr 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-foreign-keys-without-indexes-script/</guid><description>
&lt;p&gt;Foreign keys enforce referential integrity, but SQL Server does not automatically create indexes on the referencing (child) columns. An unindexed foreign key column forces a full table scan every time a join or a DELETE on the parent table triggers a referential check. This script identifies all foreign key columns in the current database that have no supporting index, and generates the &lt;code&gt;CREATE INDEX&lt;/code&gt; statements to fix them.&lt;/p&gt;
&lt;h2 id="purpose-and-overview"&gt;Purpose and Overview&lt;/h2&gt;
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;p&gt;When you run a &lt;code&gt;DELETE&lt;/code&gt; on a parent table, SQL Server must verify that no child rows reference the deleted parent. If the child table's foreign key column has no index, SQL Server performs a full table scan on the child table for every deleted parent row. On large tables this can make simple deletes extremely slow and cause blocking on the child table.&lt;/p&gt;</description></item><item><title>SQL Server Backup Compression: Enable and Verify</title><link>https://www.sqlserver70.com/post/sql-server-backup-compression-enable-verify-script/</link><pubDate>Fri, 17 Apr 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-backup-compression-enable-verify-script/</guid><description>
&lt;p&gt;Backup compression reduces the on-disk size of SQL Server backup files and can significantly shorten backup duration by reducing the amount of data written to disk. This post shows how to enable compression at the server level, verify the setting is active, and confirm compression ratios from backup history using the msdb catalog.&lt;/p&gt;
&lt;h2 id="purpose-and-overview"&gt;Purpose and Overview&lt;/h2&gt;
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;p&gt;SQL Server backup compression has been available since SQL Server 2008, but it was initially limited to Enterprise and Developer editions. From SQL Server 2016 SP1 onward it is available in all editions including Standard and Express. Despite this, many instances still run with compression disabled because the default setting is off.&lt;/p&gt;</description></item><item><title>SQL Server CPU Utilization History Report</title><link>https://www.sqlserver70.com/post/sql-server-cpu-utilization-history-report/</link><pubDate>Fri, 17 Apr 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-cpu-utilization-history-report/</guid><description>
&lt;p&gt;SQL Server records CPU utilization data internally every minute and stores the last 256 readings in a ring buffer. This T-SQL script reads that buffer to produce a CPU history report showing SQL Server CPU usage, other process CPU usage, and total server CPU load — without requiring any external monitoring tool.&lt;/p&gt;
&lt;h2 id="purpose-and-overview"&gt;Purpose and Overview&lt;/h2&gt;
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;p&gt;When a CPU spike occurs on a SQL Server host, the first question is whether SQL Server caused it or whether another process did. The &lt;code&gt;sys.dm_os_ring_buffers&lt;/code&gt; DMV stores CPU statistics that SQL Server collects from the Windows performance counters every 60 seconds, retaining the last 256 minutes of data.&lt;/p&gt;</description></item><item><title>SQL Server DBCC CHECKDB Last Run Date Report</title><link>https://www.sqlserver70.com/post/sql-server-dbcc-checkdb-last-run-date-report/</link><pubDate>Fri, 17 Apr 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-dbcc-checkdb-last-run-date-report/</guid><description>
&lt;p&gt;Running &lt;code&gt;DBCC CHECKDB&lt;/code&gt; on every database is one of the most important tasks for a SQL Server DBA. But in busy environments it is easy to lose track of which databases have been checked recently and which have not. This script loops through all user databases, reads the last known good &lt;code&gt;DBCC CHECKDB&lt;/code&gt; date from each one, and presents the results sorted from oldest to newest so you can spot gaps fast.&lt;/p&gt;</description></item><item><title>SQL Server Find Orphan Users Script</title><link>https://www.sqlserver70.com/post/sql-server-find-orphan-users-script/</link><pubDate>Fri, 17 Apr 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-find-orphan-users-script/</guid><description>
&lt;p&gt;Orphaned database users are a common problem after database restores, server migrations, or login deletions. A database user becomes orphaned when its SID does not match any server-level login SID. This script queries &lt;code&gt;sys.database_principals&lt;/code&gt; and &lt;code&gt;sys.server_principals&lt;/code&gt; to find every orphaned user in a database, and shows how to fix them.&lt;/p&gt;
&lt;h2 id="purpose-and-overview"&gt;Purpose and Overview&lt;/h2&gt;
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;p&gt;When you restore a database to a new server, or when a Windows account or SQL login is dropped at the server level, the corresponding database user record remains in the database. That user is now orphaned — it exists in &lt;code&gt;sys.database_principals&lt;/code&gt; but has no matching entry in &lt;code&gt;sys.server_principals&lt;/code&gt; by SID.&lt;/p&gt;</description></item><item><title>SQL Server MAXDOP Recommendation Script</title><link>https://www.sqlserver70.com/post/sql-server-maxdop-recommendation-script/</link><pubDate>Fri, 17 Apr 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-maxdop-recommendation-script/</guid><description>
&lt;p&gt;MAXDOP (max degree of parallelism) controls how many CPU threads SQL Server can use for a single parallel query. The default value of 0 allows SQL Server to use all available processors, which can cause large parallel queries to monopolise the server. This script reads the CPU topology from system DMVs and calculates Microsoft's recommended MAXDOP value for your instance.&lt;/p&gt;
&lt;h2 id="purpose-and-overview"&gt;Purpose and Overview&lt;/h2&gt;
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;p&gt;Setting MAXDOP incorrectly is one of the most common SQL Server configuration gaps. With MAXDOP 0 (unlimited), a single parallel query can consume every logical processor on the server, blocking other queries from getting CPU time. Setting MAXDOP too low, such as 1, disables parallelism entirely and slows down queries that benefit from parallel execution.&lt;/p&gt;</description></item><item><title>SQL Server TempDB Usage and Contention Report</title><link>https://www.sqlserver70.com/post/sql-server-tempdb-usage-contention-report/</link><pubDate>Fri, 17 Apr 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-tempdb-usage-contention-report/</guid><description>
&lt;p&gt;TempDB is a shared system database used by every session in SQL Server. When TempDB space grows or performance slows, it can be difficult to know which sessions are responsible. This script queries &lt;code&gt;sys.dm_db_session_space_usage&lt;/code&gt; to show a clear breakdown of TempDB consumption per session, including the current SQL statement being run.&lt;/p&gt;
&lt;h2 id="purpose-and-overview"&gt;Purpose and Overview&lt;/h2&gt;
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;p&gt;TempDB is used for temporary tables, table variables, sort operations, hash joins, cursors, and row versioning. Any session can allocate TempDB space, and heavy use can cause disk pressure or contention on allocation pages (GAM, SGAM, PFS).&lt;/p&gt;</description></item><item><title>SQL Server: Find Tables Without Primary Keys</title><link>https://www.sqlserver70.com/post/sql-server-find-tables-without-primary-keys/</link><pubDate>Thu, 16 Apr 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-find-tables-without-primary-keys/</guid><description>
&lt;p&gt;Tables without primary keys are heap structures with no guaranteed row ordering and no unique row identifier. They cause full table scans in queries that could use seeks, slow down DELETE and UPDATE operations, and are incompatible with SQL Server replication and Change Data Capture. This script finds every user table in the current database that has no primary key defined.&lt;/p&gt;
&lt;h2 id="purpose-and-overview"&gt;Purpose and Overview&lt;/h2&gt;
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;p&gt;A primary key enforces uniqueness on a column or set of columns and, by default, creates a clustered index that physically orders the table rows. Without a primary key, SQL Server stores rows in a heap: an unordered structure where updates and deletes must first locate rows through a full scan or a nonclustered index lookup followed by a separate heap RID lookup.&lt;/p&gt;</description></item><item><title>SQL Server Backup History: Duration and Size Report</title><link>https://www.sqlserver70.com/post/sql-server-backup-history-duration-size-report/</link><pubDate>Mon, 13 Apr 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-backup-history-duration-size-report/</guid><description>
&lt;h2 id="query-backup-duration-and-file-size-from-sql-server-history"&gt;Query Backup Duration and File Size from SQL Server History&lt;/h2&gt;
&lt;p&gt;This script queries SQL Server's msdb backup history to report the backup type, start and finish time, duration in minutes, and file size in both megabytes and gigabytes for every backup across all user databases on the server.&lt;/p&gt;
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;h2 id="purpose-and-overview"&gt;Purpose and Overview&lt;/h2&gt;
&lt;p&gt;Knowing that a backup completed is only part of the picture. DBAs also need to understand how long each backup took and how large the resulting files are. A backup that used to finish in ten minutes now taking an hour is worth investigating. A backup file that is half its usual size may indicate a failed or partial operation. This script, adapted from the DBA-Scripts collection by Bulent Gucuk, joins &lt;code&gt;sys.databases&lt;/code&gt; with &lt;code&gt;msdb.dbo.backupset&lt;/code&gt; and &lt;code&gt;msdb.dbo.backupmediafamily&lt;/code&gt; to surface both duration and size in a single result set. Running it regularly gives you a baseline so that anomalies are easy to spot.&lt;/p&gt;</description></item><item><title>SQL Server Long-Running Queries: Find Active Sessions</title><link>https://www.sqlserver70.com/post/sql-server-find-long-running-queries/</link><pubDate>Sun, 12 Apr 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-find-long-running-queries/</guid><description>
&lt;h2 id="find-active-long-running-queries-in-sql-server"&gt;Find Active Long-Running Queries in SQL Server&lt;/h2&gt;
&lt;p&gt;This script queries &lt;code&gt;sys.dm_exec_requests&lt;/code&gt; and &lt;code&gt;sys.dm_exec_sql_text&lt;/code&gt; to show all currently executing queries on the SQL Server instance, sorted by how long they have been running, along with CPU time, I/O activity, blocking status, and wait type information.&lt;/p&gt;
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;h2 id="purpose-and-overview"&gt;Purpose and Overview&lt;/h2&gt;
&lt;p&gt;A query that normally completes in seconds but has been running for many minutes is one of the most common performance problems a DBA investigates. The cause could be blocking from another session, a missing index, parameter sniffing producing a bad plan, or a sudden increase in data volume. The first step in diagnosing the problem is knowing what is running right now. SQL Server exposes this through the &lt;code&gt;sys.dm_exec_requests&lt;/code&gt; dynamic management view, which holds one row for every active request currently executing on the instance. Combined with &lt;code&gt;sys.dm_exec_sql_text&lt;/code&gt;, which returns the actual T-SQL text for a given SQL handle, this query gives you immediate visibility into what every session is doing, how long it has been running, and whether it is waiting on something.&lt;/p&gt;</description></item><item><title>SQL Server Agent Job Failure History Report</title><link>https://www.sqlserver70.com/post/sql-server-agent-job-failure-history-report/</link><pubDate>Sat, 11 Apr 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-agent-job-failure-history-report/</guid><description>
&lt;h2 id="report-sql-server-agent-job-status-and-failure-history"&gt;Report SQL Server Agent Job Status and Failure History&lt;/h2&gt;
&lt;p&gt;This script queries the SQL Server Agent job history stored in msdb to produce a detailed report of each job's most recent run status, failure and success counts over the last 60 days, total lifetime run counts, and job step information including subsystem type and proxy name.&lt;/p&gt;
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;h2 id="purpose-and-overview"&gt;Purpose and Overview&lt;/h2&gt;
&lt;p&gt;SQL Server Agent jobs automate the critical work of database backups, index maintenance, integrity checks, ETL loads, and more. When a job fails, the impact can range from a missed backup to a halted data pipeline. Knowing which jobs failed, how recently they failed, and whether failures are isolated events or a recurring pattern is essential for any DBA. This script, written by Jason Brimhall and published in the ktaranov/sqlserver-kit community repository, uses a series of common table expressions to build a rich job history report. It shows the most recent outcome for each job status type, counts failures and successes within the last 60 days relative to the latest run, and includes job step details. To report only failures, uncomment one line. Run this script in the context of the &lt;code&gt;msdb&lt;/code&gt; database.&lt;/p&gt;</description></item><item><title>SQL Server Kill Sessions: Filtered SPID Management</title><link>https://www.sqlserver70.com/post/sql-server-kill-all-connections-to-a-database/</link><pubDate>Sat, 11 Apr 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-kill-all-connections-to-a-database/</guid><description>
&lt;h2 id="kill-sql-server-sessions-with-targeted-filters-using-kill-and-dm_exec_sessions"&gt;Kill SQL Server Sessions with Targeted Filters Using KILL and dm_exec_sessions&lt;/h2&gt;
&lt;p&gt;This article covers a practical T-SQL script that kills one or more SQL Server sessions (SPIDs) while giving you precise control over which sessions are targeted. The script uses &lt;code&gt;sys.dm_exec_sessions&lt;/code&gt; to query active sessions and a cursor to issue &lt;code&gt;KILL&lt;/code&gt; commands only against sessions that match your chosen filters.&lt;/p&gt;
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;h2 id="purpose-and-overview"&gt;Purpose and Overview&lt;/h2&gt;
&lt;p&gt;When you need to disconnect users from a SQL Server database — whether before taking it offline, restoring it, or clearing stuck sessions — the built-in &lt;code&gt;KILL&lt;/code&gt; command works on one session at a time. Writing a loop to kill many sessions adds complexity. This script wraps that logic into a reusable tool with a safety switch and seven filter parameters so you can scope kills as broadly or narrowly as needed.&lt;/p&gt;</description></item><item><title>SQL Server Backup Status Report: All Databases</title><link>https://www.sqlserver70.com/post/sql-server-backup-status-report-last-backup-all-databases/</link><pubDate>Fri, 10 Apr 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-backup-status-report-last-backup-all-databases/</guid><description>
&lt;h2 id="check-sql-server-backup-age-across-all-databases"&gt;Check SQL Server Backup Age Across All Databases&lt;/h2&gt;
&lt;p&gt;This script queries the SQL Server backup history catalog to report the most recent full, differential, and transaction log backup for every database, including how many hours and minutes ago each backup completed.&lt;/p&gt;
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;h2 id="purpose-and-overview"&gt;Purpose and Overview&lt;/h2&gt;
&lt;p&gt;Database backups are your last line of defense against data loss. Knowing exactly when each database was last backed up — and how old that backup is right now — is a fundamental DBA responsibility. This script, originally written by Tim Ford, joins &lt;code&gt;master.sys.databases&lt;/code&gt; with the msdb backup history tables to produce a single report covering all backup types for all databases. It shows the backup finish time, the physical backup file path, and elapsed time in both hours and minutes, making it straightforward to spot any database that has missed a scheduled backup.&lt;/p&gt;</description></item><item><title>SQL Server User Permissions and Role Memberships</title><link>https://www.sqlserver70.com/post/sql-server-list-all-user-permissions-role-memberships/</link><pubDate>Fri, 10 Apr 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-list-all-user-permissions-role-memberships/</guid><description>
&lt;h2 id="audit-sql-server-database-user-permissions-and-role-memberships"&gt;Audit SQL Server Database User Permissions and Role Memberships&lt;/h2&gt;
&lt;p&gt;This script queries the SQL Server system catalog to produce a complete audit of database-level permissions, covering direct object grants to users, permissions inherited through database roles, and permissions granted to the public role — all in a single result set.&lt;/p&gt;
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;h2 id="purpose-and-overview"&gt;Purpose and Overview&lt;/h2&gt;
&lt;p&gt;Understanding who has access to what in a SQL Server database is a fundamental security and compliance requirement. Permissions in SQL Server are layered: a user may have rights granted directly, through membership in one or more database roles, or through the public role that every user belongs to by default. Auditing all three layers individually is time-consuming. This script, from the DBA-Scripts collection by Bulent Gucuk, uses three UNION-connected queries against the system catalog views to produce one flat result set covering every permission pathway for every user. Security teams can use the output for access reviews, compliance audits, and troubleshooting unexpected permission grants or denials.&lt;/p&gt;</description></item><item><title>SQL Server Search Stored Procedure and View Text</title><link>https://www.sqlserver70.com/post/sql-server-search-stored-procedure-view-text/</link><pubDate>Thu, 09 Apr 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-search-stored-procedure-view-text/</guid><description>
&lt;h2 id="search-stored-procedure-and-view-definitions-in-sql-server"&gt;Search Stored Procedure and View Definitions in SQL Server&lt;/h2&gt;
&lt;p&gt;This script searches the text of stored procedures, views, functions, and triggers in the current database using &lt;code&gt;sys.sql_modules&lt;/code&gt;, returning any programmable object whose name or definition contains a given keyword.&lt;/p&gt;
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;h2 id="purpose-and-overview"&gt;Purpose and Overview&lt;/h2&gt;
&lt;p&gt;Finding which stored procedures, views, or functions reference a specific table, column, or keyword is a routine DBA and developer task. It comes up when renaming a column, tracking down where a piece of business logic lives, auditing who calls a particular table, or investigating the impact of a schema change. SQL Server stores the source definition of every programmable object in &lt;code&gt;sys.sql_modules&lt;/code&gt;, making it straightforward to search across all object types in a single query. This script, adapted from the sqlserver-kit community collection by Ed Pollack and Konstantin Taranov, queries &lt;code&gt;sys.sql_modules&lt;/code&gt; joined to &lt;code&gt;sys.objects&lt;/code&gt; to search both object names and their full definitions, returning results with the object type, schema, and full source text.&lt;/p&gt;</description></item><item><title>SQL Server Error Log Search Script: xp_readerrorlog</title><link>https://www.sqlserver70.com/post/sql-server-error-log-search-script-xp-readerrorlog/</link><pubDate>Wed, 08 Apr 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-error-log-search-script-xp-readerrorlog/</guid><description>
&lt;h2 id="search-the-sql-server-error-log-with-xp_readerrorlog"&gt;Search the SQL Server Error Log with xp_readerrorlog&lt;/h2&gt;
&lt;p&gt;This script reads and filters the SQL Server error log using &lt;code&gt;xp_readerrorlog&lt;/code&gt; and &lt;code&gt;sys.sp_readerrorlog&lt;/code&gt;, supporting keyword search, date range filtering, and access to archived log files — all from T-SQL without touching the file system.&lt;/p&gt;
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;h2 id="purpose-and-overview"&gt;Purpose and Overview&lt;/h2&gt;
&lt;p&gt;The SQL Server error log records startup events, login failures, database state changes, backup completions, DBCC output, and any errors raised by the engine. When something goes wrong — a database goes suspect, a backup fails, or a login is repeatedly denied — the error log is the first place to look. SQL Server provides two built-in mechanisms for reading the log from T-SQL: the undocumented extended stored procedure &lt;code&gt;xp_readerrorlog&lt;/code&gt; and its documented wrapper &lt;code&gt;sys.sp_readerrorlog&lt;/code&gt;. Both support keyword filtering and date ranges, making them far more useful than opening the log file in SSMS's Log Viewer for scripted or automated diagnostic work. This script, adapted from the Read_errorlog.sql in the sqlserver-kit collection by Konstantin Taranov, demonstrates how to search one or more error log files efficiently from T-SQL.&lt;/p&gt;</description></item><item><title>SQL Server Verify Backup Files: RESTORE VERIFYONLY</title><link>https://www.sqlserver70.com/post/sql-server-verify-backup-files-restore-verifyonly/</link><pubDate>Mon, 06 Apr 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-verify-backup-files-restore-verifyonly/</guid><description>
&lt;h2 id="verify-sql-server-backup-files-with-restore-verifyonly"&gt;Verify SQL Server Backup Files with RESTORE VERIFYONLY&lt;/h2&gt;
&lt;p&gt;Backup files that cannot be restored are worthless, and many DBAs discover problems only when they attempt an actual restore under pressure. The scripts in this article use &lt;code&gt;RESTORE VERIFYONLY&lt;/code&gt; to confirm that backup files are readable and internally consistent before a real disaster occurs.&lt;/p&gt;
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;h2 id="purpose-and-overview"&gt;Purpose and Overview&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;RESTORE VERIFYONLY&lt;/code&gt; reads a backup file and validates its structure without writing any data to a database. It checks that the backup set is complete, that all volumes are readable, and that header metadata is intact. It does not verify that every data page is logically consistent, but it catches the most common failure modes: truncated files, corrupted headers, and missing media families.&lt;/p&gt;</description></item><item><title>SQL Server Point-in-Time Restore with STOPAT</title><link>https://www.sqlserver70.com/post/sql-server-point-in-time-restore-script-stopat/</link><pubDate>Sun, 05 Apr 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-point-in-time-restore-script-stopat/</guid><description>
&lt;h2 id="recover-a-sql-server-database-to-a-specific-point-in-time"&gt;Recover a SQL Server Database to a Specific Point in Time&lt;/h2&gt;
&lt;p&gt;SQL Server's &lt;code&gt;RESTORE LOG ... WITH STOPAT&lt;/code&gt; lets you roll a database back to any exact moment covered by your backup chain. This guide walks through querying backup history in &lt;code&gt;msdb&lt;/code&gt;, executing the restore sequence, and verifying the result.&lt;/p&gt;
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;h2 id="purpose-and-overview"&gt;Purpose and Overview&lt;/h2&gt;
&lt;p&gt;Data loss incidents — accidental deletes, bad deployments, runaway updates — rarely happen at a convenient backup boundary. Point-in-time restore solves this by replaying transaction logs only up to a target timestamp, leaving the database in a consistent state as of that moment.&lt;/p&gt;</description></item><item><title>SQL Server Top Queries by CPU and IO: dm_exec_query_stats</title><link>https://www.sqlserver70.com/post/sql-server-top-queries-by-cpu-and-io-sys-dm-exec-query-stats/</link><pubDate>Sat, 04 Apr 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-top-queries-by-cpu-and-io-sys-dm-exec-query-stats/</guid><description>
&lt;h2 id="find-the-top-cpu-and-io-consuming-queries-in-sql-server"&gt;Find the Top CPU and IO Consuming Queries in SQL Server&lt;/h2&gt;
&lt;p&gt;This script queries &lt;code&gt;sys.dm_exec_query_stats&lt;/code&gt; to identify the most resource-intensive queries currently cached in the plan cache, showing logical reads, writes, CPU time, and elapsed time — with the full query text and execution plan for each.&lt;/p&gt;
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;h2 id="purpose-and-overview"&gt;Purpose and Overview&lt;/h2&gt;
&lt;p&gt;When a SQL Server instance is under CPU or I/O pressure, the first diagnostic step is finding which queries are responsible. &lt;code&gt;sys.dm_exec_query_stats&lt;/code&gt; holds cumulative execution statistics for every query plan currently in the plan cache. Sorted by total logical reads or total CPU time, it quickly surfaces the queries doing the most work. This script, adapted from the DBA-Scripts collection by Bulent Gucuk, combines &lt;code&gt;sys.dm_exec_query_stats&lt;/code&gt; with &lt;code&gt;sys.dm_exec_sql_text&lt;/code&gt; and &lt;code&gt;sys.dm_exec_query_plan&lt;/code&gt; to return the query text and execution plan alongside the performance metrics — giving you everything needed to begin tuning in a single result set.&lt;/p&gt;</description></item><item><title>SQL Server Index Fragmentation: dm_db_index_physical_stats</title><link>https://www.sqlserver70.com/post/sql-server-index-fragmentation-sys-dm-db-index-physical-stats/</link><pubDate>Fri, 03 Apr 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-index-fragmentation-sys-dm-db-index-physical-stats/</guid><description>
&lt;h2 id="report-index-fragmentation-across-all-tables-in-sql-server"&gt;Report Index Fragmentation Across All Tables in SQL Server&lt;/h2&gt;
&lt;p&gt;This script queries &lt;code&gt;sys.dm_db_index_physical_stats&lt;/code&gt; to report external and internal fragmentation for every index in the current database, filtering to indexes with significant fragmentation and enough pages to make maintenance worthwhile.&lt;/p&gt;
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;h2 id="purpose-and-overview"&gt;Purpose and Overview&lt;/h2&gt;
&lt;p&gt;Index fragmentation is one of the most common causes of gradually degrading query performance in SQL Server. As rows are inserted, updated, and deleted, index pages become out of order (external fragmentation) and partially empty (internal fragmentation). Both conditions force SQL Server to read more pages than necessary to satisfy a query. The &lt;code&gt;sys.dm_db_index_physical_stats&lt;/code&gt; dynamic management function scans index structures and reports fragmentation levels, giving DBAs the data needed to decide which indexes should be rebuilt and which should be reorganized. This script, adapted from the DBA-Scripts collection by Bulent Gucuk, runs against all tables in the current database and filters the output to actionable results.&lt;/p&gt;</description></item><item><title>SQL Server Database Size Report for All Databases</title><link>https://www.sqlserver70.com/post/sql-server-database-size-report-all-databases/</link><pubDate>Thu, 02 Apr 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-database-size-report-all-databases/</guid><description>
&lt;h2 id="report-database-size-in-mb-and-gb-for-all-sql-server-databases"&gt;Report Database Size in MB and GB for All SQL Server Databases&lt;/h2&gt;
&lt;p&gt;This script queries &lt;code&gt;sys.master_files&lt;/code&gt; and &lt;code&gt;sys.databases&lt;/code&gt; to report the total allocated size in megabytes and gigabytes for every user database on the SQL Server instance, sorted by largest database first.&lt;/p&gt;
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;h2 id="purpose-and-overview"&gt;Purpose and Overview&lt;/h2&gt;
&lt;p&gt;Knowing how large each database is — and tracking how that size changes over time — is a core part of SQL Server capacity planning. Running out of disk space is one of the most disruptive events a DBA can face: it halts log backups, prevents transactions from committing, and can take databases offline. This script, adapted from the DBA-Scripts collection by Bulent Gucuk, reads allocated file sizes from &lt;code&gt;sys.master_files&lt;/code&gt; and joins to &lt;code&gt;sys.databases&lt;/code&gt; to produce a single server-wide size report. It runs from the master database, requires no cross-database queries, and takes milliseconds to execute — making it safe to run at any time, including during a production incident.&lt;/p&gt;</description></item><item><title>SQL Server Unused Indexes: sys.dm_db_index_usage_stats</title><link>https://www.sqlserver70.com/post/sql-server-unused-indexes-sys-dm-db-index-usage-stats/</link><pubDate>Wed, 01 Apr 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-unused-indexes-sys-dm-db-index-usage-stats/</guid><description>
&lt;h2 id="find-unused-nonclustered-indexes-in-sql-server"&gt;Find Unused Nonclustered Indexes in SQL Server&lt;/h2&gt;
&lt;p&gt;This script queries &lt;code&gt;sys.dm_db_index_usage_stats&lt;/code&gt; to identify nonclustered indexes that have never been used by any read operation since the last SQL Server restart, yet still carry write overhead with every INSERT, UPDATE, and DELETE on the table.&lt;/p&gt;
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;h2 id="purpose-and-overview"&gt;Purpose and Overview&lt;/h2&gt;
&lt;p&gt;Every nonclustered index in SQL Server has a cost: each time a row is inserted, updated, or deleted in the base table, SQL Server must also update every nonclustered index on that table. If an index is never used by the query optimizer — no seeks, scans, or lookups — it is pure overhead with no benefit. Over time, databases accumulate these unused indexes through abandoned development, changed application query patterns, or indexes added by automated tools without follow-up review. This script, from the DBA-Scripts collection by Bulent Gucuk, reads &lt;code&gt;sys.dm_db_index_usage_stats&lt;/code&gt; to expose every nonclustered index with zero read activity since the last server restart, and generates the DROP INDEX statement for each one.&lt;/p&gt;</description></item><item><title>SQL Server Blocking Detection: Find Blocked Sessions</title><link>https://www.sqlserver70.com/post/sql-server-blocking-detection-find-blocked-sessions/</link><pubDate>Sun, 01 Mar 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-blocking-detection-find-blocked-sessions/</guid><description>
&lt;p&gt;Blocking in SQL Server occurs when one session holds a lock that another session needs, causing the second session to wait. This T-SQL script queries the dynamic management views to show all currently blocked sessions, the blocking session, wait type, wait duration, and the SQL text involved.&lt;/p&gt;
&lt;h2 id="purpose-and-overview"&gt;Purpose and Overview&lt;/h2&gt;
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;p&gt;When users report slow queries or application timeouts, blocking is a common cause. SQL Server does not alert you to blocking by default — you must query the DMVs to see what is happening in real time.&lt;/p&gt;</description></item><item><title>SQL Server Missing Indexes Report: dm_db_missing_index</title><link>https://www.sqlserver70.com/post/sql-server-missing-indexes-report-sys-dm-db-missing-index-details/</link><pubDate>Sat, 28 Feb 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-missing-indexes-report-sys-dm-db-missing-index-details/</guid><description>
&lt;p&gt;SQL Server tracks index recommendations automatically as queries execute, storing them in the missing index DMVs. This T-SQL script queries those views to produce a ranked report of missing indexes, ordered by the estimated improvement they would provide, along with a ready-to-run &lt;code&gt;CREATE INDEX&lt;/code&gt; statement for each recommendation.&lt;/p&gt;
&lt;h2 id="purpose-and-overview"&gt;Purpose and Overview&lt;/h2&gt;
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;p&gt;Every time SQL Server executes a query that would benefit from an index that does not exist, it records the recommendation in &lt;code&gt;sys.dm_db_missing_index_details&lt;/code&gt;. Over time these recommendations accumulate and can reveal the highest-impact indexing changes you can make to improve query performance.&lt;/p&gt;</description></item><item><title>SQL Server Wait Statistics Report: dm_os_wait_stats</title><link>https://www.sqlserver70.com/post/sql-server-wait-statistics-report-sys-dm-os-wait-stats/</link><pubDate>Fri, 27 Feb 2026 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-wait-statistics-report-sys-dm-os-wait-stats/</guid><description>
&lt;p&gt;Wait statistics are one of the most reliable ways to identify performance bottlenecks in SQL Server. This T-SQL script queries &lt;code&gt;sys.dm_os_wait_stats&lt;/code&gt;, filters out background and idle wait types that do not indicate real problems, and ranks the remaining waits by their percentage of total wait time.&lt;/p&gt;
&lt;h2 id="purpose-and-overview"&gt;Purpose and Overview&lt;/h2&gt;
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
&lt;p&gt;Every time SQL Server cannot immediately process a request — because it is waiting for CPU, disk, memory, locks, or network — it records that wait in &lt;code&gt;sys.dm_os_wait_stats&lt;/code&gt;. Over time this DMV accumulates a comprehensive picture of where SQL Server spends time waiting.&lt;/p&gt;</description></item><item><title>sp_pkeys: SQL Server Primary Key Discovery Script</title><link>https://www.sqlserver70.com/post/sql-server-primary-key-discovery-script/</link><pubDate>Sat, 09 Aug 2025 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-primary-key-discovery-script/</guid><description>
&lt;h2 id="sql-server-primary-key-discovery-script-using-sp_pkeys-and-sysobjects"&gt;SQL Server Primary Key Discovery Script Using sp_pkeys and sysobjects&lt;/h2&gt;
&lt;h2 id="purpose"&gt;Purpose&lt;/h2&gt;
&lt;p&gt;This SQL Server script generates dynamic SQL statements that can be executed to discover primary keys across all user tables in a database. It's a powerful administrative tool that helps database administrators quickly identify primary key constraints without manually checking each table individually.
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;&lt;/p&gt;
&lt;h2 id="code-breakdown"&gt;Code Breakdown&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-sql" data-lang="sql"&gt;&lt;span class="line"&gt;&lt;span class="ln"&gt;1&lt;/span&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;select&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;select &amp;#34;Processing Table &amp;#39;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;&amp;#34;go&amp;#39;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;&amp;#39; sp_pkeys &amp;#39;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;go&amp;#39;&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="ln"&gt;2&lt;/span&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;sysobjects&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="ln"&gt;3&lt;/span&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;where&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;U&amp;#39;&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="ln"&gt;4&lt;/span&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;order&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;by&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Let's break down each component of this query:&lt;/p&gt;</description></item><item><title>SQL Server: Find Tables with Clustered Indexes for Rebuild</title><link>https://www.sqlserver70.com/post/sql-server-query-to-identify-tables-with-clustered-indexes-for-rebuilding/</link><pubDate>Sat, 09 Aug 2025 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-query-to-identify-tables-with-clustered-indexes-for-rebuilding/</guid><description>
&lt;h2 id="sql-server-query-to-identify-tables-with-clustered-indexes-for-rebuilding"&gt;SQL Server Query to Identify Tables with Clustered Indexes for Rebuilding&lt;/h2&gt;
&lt;p&gt;Database administrators frequently need to perform maintenance operations on clustered indexes to optimize database performance. This SQL Server query provides a quick and efficient way to identify all user tables that contain clustered indexes, which is typically the first step in a comprehensive index rebuilding strategy.
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;&lt;/p&gt;
&lt;h2 id="the-sql-query"&gt;The SQL Query&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-sql" data-lang="sql"&gt;&lt;span class="line"&gt;&lt;span class="ln"&gt;1&lt;/span&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;select&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;distinct&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="ln"&gt;2&lt;/span&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;sysobjects&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;sysindexes&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="ln"&gt;3&lt;/span&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;where&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;U&amp;#39;&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="ln"&gt;4&lt;/span&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;and&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="ln"&gt;5&lt;/span&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;and&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indid&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="ln"&gt;6&lt;/span&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;order&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;by&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="purpose-and-use-case"&gt;Purpose and Use Case&lt;/h2&gt;
&lt;p&gt;This query serves as a foundation script for database maintenance operations, specifically designed to:&lt;/p&gt;</description></item><item><title>SQL Server Object Permissions: Generate GRANT Statements</title><link>https://www.sqlserver70.com/post/sql-server-object-level-permissions-script-generate-grant-statements-for-database-security/</link><pubDate>Fri, 08 Aug 2025 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-object-level-permissions-script-generate-grant-statements-for-database-security/</guid><description>
&lt;h2 id="sql-server-object-level-permissions-script-generate-grant-statements-for-database-security"&gt;SQL Server Object-Level Permissions Script: Generate GRANT Statements for Database Security&lt;/h2&gt;
&lt;p&gt;Database security is a critical aspect of SQL Server administration, and managing permissions at the object level can be time-consuming when done manually. This SQL Server script automates the process of generating GRANT statements for all database objects, making it easier for database administrators to apply consistent permissions across their database schema.&lt;/p&gt;
&lt;h2 id="purpose-of-the-script"&gt;Purpose of the Script&lt;/h2&gt;
&lt;p&gt;This SQL Server script is designed to automatically generate GRANT permission statements for all user tables and stored procedures in a database. Instead of manually writing individual GRANT statements for each object, this script queries the system catalog to dynamically create the necessary permission commands for a specific database role called &lt;code&gt;SelectInsertUpdateDeleteExecSP&lt;/code&gt;.
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;&lt;/p&gt;</description></item><item><title>Generate SQL Server Space Usage Commands for All User Tables</title><link>https://www.sqlserver70.com/post/sql-server-generate-space-usage-commands-for-all-tables/</link><pubDate>Thu, 07 Aug 2025 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-generate-space-usage-commands-for-all-tables/</guid><description>
&lt;h2 id="generate-sql-server-space-usage-commands-for-all-user-tables"&gt;Generate SQL Server Space Usage Commands for All User Tables&lt;/h2&gt;
&lt;p&gt;Managing database storage is a critical aspect of SQL Server administration. Understanding how much space each table consumes helps database administrators make informed decisions about maintenance, archiving, and performance optimization. This article explores a powerful T-SQL script that automatically generates space usage commands for all user tables in your database.
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;&lt;/p&gt;
&lt;h2 id="the-sql-server-code"&gt;The SQL Server Code&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-sql" data-lang="sql"&gt;&lt;span class="line"&gt;&lt;span class="ln"&gt;1&lt;/span&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;select&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;sp_spaceused &amp;#39;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;go&amp;#39;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;sysobjects&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;where&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;U&amp;#39;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;order&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;by&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="purpose-and-overview"&gt;Purpose and Overview&lt;/h2&gt;
&lt;p&gt;This T-SQL query serves as a time-saving utility for database administrators who need to quickly assess the space usage of all user tables in a SQL Server database. Instead of manually typing &lt;code&gt;sp_spaceused&lt;/code&gt; commands for each table, this script automatically generates the complete set of commands ready for execution.&lt;/p&gt;</description></item><item><title>SQL Server UPDATE STATISTICS and SELECT Dynamic Scripts</title><link>https://www.sqlserver70.com/post/sql-server-dynamic-update-statistics-and-select-scripts/</link><pubDate>Wed, 06 Aug 2025 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-dynamic-update-statistics-and-select-scripts/</guid><description>
&lt;h2 id="sql-server-dynamic-update-statistics-and-select-scripts-for-database-maintenance"&gt;SQL Server Dynamic Update Statistics and Select Scripts for Database Maintenance&lt;/h2&gt;
&lt;h2 id="purpose"&gt;Purpose&lt;/h2&gt;
&lt;p&gt;These SQL Server scripts are designed to automatically generate database maintenance commands by dynamically creating UPDATE STATISTICS statements and SELECT queries for all user tables in a database. This approach is particularly useful for database administrators who need to perform bulk operations across multiple tables without manually writing individual commands for each table.
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;&lt;/p&gt;</description></item><item><title>SQL Server DBCC DBREINDEX with Timing: Index Maintenance</title><link>https://www.sqlserver70.com/post/sql-server-dbcc-dbreindex-script-with-timing-database-maintenance-guide/</link><pubDate>Tue, 05 Aug 2025 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-dbcc-dbreindex-script-with-timing-database-maintenance-guide/</guid><description>
&lt;h2 id="sql-server-dbcc-dbreindex-script-with-timing---database-maintenance-guide"&gt;SQL Server DBCC DBREINDEX Script with Timing - Database Maintenance Guide&lt;/h2&gt;
&lt;p&gt;Database maintenance is a critical aspect of SQL Server administration, and index rebuilding plays a vital role in maintaining optimal database performance. This comprehensive guide explores powerful DBCC DBREINDEX scripts that include timing functionality to help database administrators monitor and optimize their maintenance operations.
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;&lt;/p&gt;
&lt;h2 id="purpose-and-overview"&gt;Purpose and Overview&lt;/h2&gt;
&lt;p&gt;The DBCC DBREINDEX scripts presented here are designed to automate the process of rebuilding indexes on all user tables within a SQL Server database. These scripts generate dynamic SQL commands that can be executed to defragment and reorganize table indexes, ultimately improving query performance and database efficiency.&lt;/p&gt;</description></item><item><title>DBCC DBREINDEX: SQL Server Index Rebuild Script</title><link>https://www.sqlserver70.com/post/sql-server-dbcc-dbreindex-script-generator-for-database-maintenance/</link><pubDate>Mon, 04 Aug 2025 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-dbcc-dbreindex-script-generator-for-database-maintenance/</guid><description>
&lt;h2 id="sql-server-dbcc-dbreindex-script-generator-for-database-maintenance"&gt;SQL Server DBCC DBREINDEX Script Generator for Database Maintenance&lt;/h2&gt;
&lt;p&gt;Database maintenance is crucial for optimal SQL Server performance, and one of the most important tasks is maintaining database indexes. The &lt;code&gt;DBCC DBREINDEX&lt;/code&gt; command is a powerful tool for rebuilding indexes, but manually running it on every table can be time-consuming. This article explores automated script generation techniques to streamline your database maintenance workflow.
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;&lt;/p&gt;</description></item><item><title>SQL Server DBCC CHECKIDENT: Bulk Identity Column Analysis</title><link>https://www.sqlserver70.com/post/sql-server-dbcc-checkident-identity-management-script/</link><pubDate>Sun, 03 Aug 2025 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-dbcc-checkident-identity-management-script/</guid><description>
&lt;h2 id="sql-server-dbcc-checkident-complete-guide-to-identity-column-management-and-bulk-table-analysis"&gt;SQL Server DBCC CHECKIDENT: Complete Guide to Identity Column Management and Bulk Table Analysis&lt;/h2&gt;
&lt;h2 id="purpose-and-overview"&gt;Purpose and Overview&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;DBCC CHECKIDENT&lt;/code&gt; command is a critical database administration tool in Microsoft SQL Server designed to manage and monitor identity column values. This article explores a practical implementation that demonstrates both individual table identity checking and automated bulk analysis across all user tables in a database.
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;&lt;/p&gt;</description></item><item><title>SQL Server Table Row Count Report: Complete T-SQL Script</title><link>https://www.sqlserver70.com/post/sql-server-database-table-row-count-report-script/</link><pubDate>Sat, 02 Aug 2025 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-database-table-row-count-report-script/</guid><description>
&lt;h2 id="sql-server-database-table-row-count-report-script---complete-guide"&gt;SQL Server Database Table Row Count Report Script - Complete Guide&lt;/h2&gt;
&lt;p&gt;Database administrators frequently need to monitor table sizes and row counts across their SQL Server databases. This comprehensive guide explores a powerful T-SQL script that generates detailed table row count reports using cursor-based iteration and system table queries.
&lt;script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1012089347386563"
crossorigin="anonymous"&gt;&lt;/script&gt;
&lt;ins class="adsbygoogle"
style="display:inline-block;width:728px;height:90px"
data-ad-client="ca-pub-1012089347386563"
data-ad-slot="8593449130"&gt;&lt;/ins&gt;
&lt;script&gt;
(adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;&lt;/p&gt;
&lt;h2 id="purpose-and-overview"&gt;Purpose and Overview&lt;/h2&gt;
&lt;p&gt;The &amp;quot;Run Space Report&amp;quot; script serves as a database monitoring tool that captures essential table statistics including server name, execution timestamp, database name, table name, and total row counts. This information proves invaluable for capacity planning, performance optimization, and database maintenance activities.&lt;/p&gt;</description></item><item><title>Generate DBCC SHOWCONTIG Commands for All SQL Server Tables</title><link>https://www.sqlserver70.com/post/generate-dbcc-showcontig-commands-for-all-user-tables-in-sql-server/</link><pubDate>Fri, 01 Aug 2025 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/generate-dbcc-showcontig-commands-for-all-user-tables-in-sql-server/</guid><description>
&lt;p&gt;Before &lt;code&gt;sys.dm_db_index_physical_stats&lt;/code&gt; existed, checking fragmentation across a database meant looking up every user table's object ID and running &lt;code&gt;DBCC SHOWCONTIG&lt;/code&gt; against each one individually. This script closes that gap for the &lt;code&gt;sysobjects&lt;/code&gt;-based era of SQL Server administration: it queries &lt;code&gt;sysobjects&lt;/code&gt; for every table of type &lt;code&gt;U&lt;/code&gt;, concatenates a ready-to-run &lt;code&gt;DBCC SHOWCONTIG&lt;/code&gt; command for each one, and returns the full batch sorted alphabetically by table name — turning what would be dozens of hand-typed commands into a single copy-paste result set.&lt;/p&gt;</description></item><item><title>SQL Server Scripts and Commands: DBA Reference All Versions</title><link>https://www.sqlserver70.com/post/sql-server-scripts-database-commands/</link><pubDate>Fri, 01 Aug 2025 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-scripts-database-commands/</guid><description>
&lt;h2 id="sql-server-scripts-and-database-commands-65-70-80-90-100-1050-110-120-130-140-150-160-170"&gt;SQL Server Scripts and Database Commands 6.5, 7.0, 8.0, 9.0, 10.0, 10.50, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0&lt;/h2&gt;
&lt;p&gt;The corresponding version numbers and years: SQL Server 6.5 (1996), SQL Server 7.0 (1998), SQL Server 2000, SQL Server 2005, SQL Server 2008, SQL Server 2008 R2, SQL Server 2012, SQL Server 2014, SQL Server 2016, SQL Server 2017, SQL Server 2019, SQL Server 2022, and SQL Server 2025&lt;/p&gt;
&lt;p&gt;We are in the process of building a list of simple scripts used every day by DBA and others. If you have your own scripts you reuse in a text file on you workstation please send it to us.&lt;/p&gt;</description></item><item><title/><link>https://www.sqlserver70.com/post/sql-server-log-backup-restore-chain-script-guide/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-log-backup-restore-chain-script-guide/</guid><description>
&lt;h1 id="sql-server-log-backup-restore-chain-script-guidemd"&gt;sql-server-log-backup-restore-chain-script-guide.md&lt;/h1&gt;
&lt;hr&gt;
&lt;p&gt;title: &amp;quot;SQL Server Log Backup Restore Chain Guide&amp;quot;
description: &amp;quot;Learn how to apply SQL Server full, differential, and transaction log backups in the correct restore chain sequence using NORECOVERY and RECOVERY options.&amp;quot;
keywords:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;sql server restore chain&lt;/li&gt;
&lt;li&gt;transaction log backup restore&lt;/li&gt;
&lt;li&gt;NORECOVERY RECOVERY sql server&lt;/li&gt;
&lt;li&gt;restore database sql server&lt;/li&gt;
&lt;li&gt;full differential log restore&lt;/li&gt;
&lt;li&gt;msdb backupset query&lt;/li&gt;
&lt;li&gt;sql server backup restore sequence&lt;/li&gt;
&lt;li&gt;sql server log backup apply
tags:&lt;/li&gt;
&lt;li&gt;sql server&lt;/li&gt;
&lt;li&gt;restore&lt;/li&gt;
&lt;li&gt;backup&lt;/li&gt;
&lt;li&gt;transaction log&lt;/li&gt;
&lt;li&gt;NORECOVERY&lt;/li&gt;
&lt;li&gt;RECOVERY&lt;/li&gt;
&lt;li&gt;msdb&lt;/li&gt;
&lt;li&gt;database recovery
date: 2026-04-07
lastmod: 2026-05-21
showLastmod: true
categories:&lt;/li&gt;
&lt;li&gt;Scripts&lt;/li&gt;
&lt;li&gt;SQL Server Backup Recovery
series: SQL Server Backup Recovery&lt;/li&gt;
&lt;li&gt;admin-guide&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2 id="sql-server-restore-chain-full-differential-and-log-backups"&gt;SQL Server Restore Chain: Full, Differential, and Log Backups&lt;/h2&gt;
&lt;p&gt;Restoring a SQL Server database to a specific point in time requires applying backups in the correct sequence. Getting the order wrong — or using the wrong recovery option — will break the restore chain and leave the database unusable.&lt;/p&gt;</description></item><item><title/><link>https://www.sqlserver70.com/post/sql-server-restore-database-script-with-move-option/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-restore-database-script-with-move-option/</guid><description>
&lt;h1 id="sql-server-restore-database-script-with-move-optionmd"&gt;sql-server-restore-database-script-with-move-option.md&lt;/h1&gt;
&lt;hr&gt;
&lt;p&gt;title: &amp;quot;SQL Server Restore Database Script with MOVE&amp;quot;
description: &amp;quot;Use dynamic SQL with the MOVE option to restore a SQL Server database and remap data and log file paths to any target directory on the server.&amp;quot;
keywords:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;sql server restore database&lt;/li&gt;
&lt;li&gt;restore with move&lt;/li&gt;
&lt;li&gt;remap file paths&lt;/li&gt;
&lt;li&gt;dynamic sql restore&lt;/li&gt;
&lt;li&gt;RESTORE DATABASE&lt;/li&gt;
&lt;li&gt;RESTORE FILELISTONLY&lt;/li&gt;
&lt;li&gt;backup restore script&lt;/li&gt;
&lt;li&gt;sql server database recovery&lt;/li&gt;
&lt;li&gt;sp_executesql restore
tags:&lt;/li&gt;
&lt;li&gt;restore database&lt;/li&gt;
&lt;li&gt;backup recovery&lt;/li&gt;
&lt;li&gt;dynamic sql&lt;/li&gt;
&lt;li&gt;sql server&lt;/li&gt;
&lt;li&gt;RESTORE DATABASE&lt;/li&gt;
&lt;li&gt;RESTORE FILELISTONLY&lt;/li&gt;
&lt;li&gt;database administration&lt;/li&gt;
&lt;li&gt;scripts
date: 2026-04-09
lastmod: 2026-05-21
showLastmod: true
categories:&lt;/li&gt;
&lt;li&gt;Scripts&lt;/li&gt;
&lt;li&gt;SQL Server Backup Recovery
series: SQL Server Backup Recovery&lt;/li&gt;
&lt;li&gt;admin-guide&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2 id="restore-a-sql-server-database-with-file-path-remapping"&gt;Restore a SQL Server Database with File Path Remapping&lt;/h2&gt;
&lt;p&gt;Restoring a SQL Server database to a different server or folder often fails because the file paths in the backup do not match the target environment. The MOVE option in the RESTORE DATABASE statement lets you specify exactly where each data and log file should land.&lt;/p&gt;</description></item><item><title/><link>https://www.sqlserver70.com/post/sql-server-script-to-backup-all-databases-to-disk/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://www.sqlserver70.com/post/sql-server-script-to-backup-all-databases-to-disk/</guid><description>
&lt;h1 id="sql-server-script-to-backup-all-databases-to-diskmd"&gt;sql-server-script-to-backup-all-databases-to-disk.md&lt;/h1&gt;
&lt;hr&gt;
&lt;p&gt;title: &amp;quot;SQL Server Script to Backup All Databases to Disk&amp;quot;
description: &amp;quot;T-SQL script to back up all user databases to disk with dynamic date-stamped filenames. Automate full database backups in SQL Server using BACKUP DATABASE.&amp;quot;
keywords:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;sql server backup all databases&lt;/li&gt;
&lt;li&gt;backup database to disk&lt;/li&gt;
&lt;li&gt;date stamped backup filenames&lt;/li&gt;
&lt;li&gt;t-sql backup script&lt;/li&gt;
&lt;li&gt;sql server automated backup&lt;/li&gt;
&lt;li&gt;BACKUP DATABASE&lt;/li&gt;
&lt;li&gt;sys.databases&lt;/li&gt;
&lt;li&gt;sql server full backup&lt;/li&gt;
&lt;li&gt;dba backup script&lt;/li&gt;
&lt;li&gt;sql server database maintenance
tags:&lt;/li&gt;
&lt;li&gt;sql server&lt;/li&gt;
&lt;li&gt;backup&lt;/li&gt;
&lt;li&gt;database maintenance&lt;/li&gt;
&lt;li&gt;t-sql&lt;/li&gt;
&lt;li&gt;automation&lt;/li&gt;
&lt;li&gt;sys.databases&lt;/li&gt;
&lt;li&gt;dba scripts&lt;/li&gt;
&lt;li&gt;full backup
date: 2026-04-08
lastmod: 2026-05-21
showLastmod: true
categories:&lt;/li&gt;
&lt;li&gt;Scripts&lt;/li&gt;
&lt;li&gt;SQL Server Backup Recovery
series: SQL Server Backup Recovery&lt;/li&gt;
&lt;li&gt;admin-guide
codeMaxLines: 60
codeLineNumbers: true&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2 id="back-up-all-sql-server-user-databases-with-one-script"&gt;Back Up All SQL Server User Databases with One Script&lt;/h2&gt;
&lt;p&gt;Backing up every user database manually is time-consuming and error-prone. This T-SQL script loops through all online user databases and writes a compressed full backup for each one to disk, using a date- and time-stamped filename so backups never overwrite each other.&lt;/p&gt;</description></item></channel></rss>