<?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 Structure on SQL Server Scripts</title><link>https://www.sqlserver70.com/series/sql-server-structure/</link><description>Recent content in SQL Server Structure on SQL Server Scripts</description><generator>Hugo -- gohugo.io</generator><language>en</language><copyright>SQLServer70.com</copyright><lastBuildDate>Fri, 22 May 2026 00:00:00 +0000</lastBuildDate><atom:link href="https://www.sqlserver70.com/series/sql-server-structure/index.xml" rel="self" type="application/rss+xml"/><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 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: 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>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></channel></rss>