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

<channel>
	<title>invalid column Archives - Dragonsoft Technology View</title>
	<atom:link href="https://blog.dragonsoft.us/tag/invalid-column/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.dragonsoft.us/tag/invalid-column/</link>
	<description>Talk about Technologies, Software Architecture and Management</description>
	<lastBuildDate>Sun, 08 May 2022 00:10:05 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0</generator>

<image>
	<url>https://blog.dragonsoft.us/wp-content/uploads/2022/04/logo-main-bw-150x150.png</url>
	<title>invalid column Archives - Dragonsoft Technology View</title>
	<link>https://blog.dragonsoft.us/tag/invalid-column/</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">2577970</site>	<item>
		<title>SQL Server: Tables, Views and missing columns</title>
		<link>https://blog.dragonsoft.us/2010/02/26/tables-views-missing-columns/</link>
					<comments>https://blog.dragonsoft.us/2010/02/26/tables-views-missing-columns/#respond</comments>
		
		<dc:creator><![CDATA[Serguei Dosyukov]]></dc:creator>
		<pubDate>Fri, 26 Feb 2010 13:42:30 +0000</pubDate>
				<category><![CDATA[Fun stuff with SQL Server]]></category>
		<category><![CDATA[invalid column]]></category>
		<category><![CDATA[invalid data]]></category>
		<category><![CDATA[refresh metadata]]></category>
		<category><![CDATA[refresh object]]></category>
		<category><![CDATA[refresh view]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[sp_refreshsqlmodule]]></category>
		<category><![CDATA[sp_refreshview]]></category>
		<category><![CDATA[sql server]]></category>
		<category><![CDATA[view schema]]></category>
		<guid isPermaLink="false">http://blog.dragonsoft.us/?p=1109</guid>

					<description><![CDATA[<p>When it comes to non-schema-bound-views (NSB View)&#160;it is important to remember that there is no schema bound between objects used in the view and metadata for a view. Let&#8217;s look at the following scenario: An attempt to use the view would give the following result: So far we are OK.<a class="moretag" href="https://blog.dragonsoft.us/2010/02/26/tables-views-missing-columns/"> Read more</a></p>
<p>The post <a href="https://blog.dragonsoft.us/2010/02/26/tables-views-missing-columns/">SQL Server: Tables, Views and missing columns</a> appeared first on <a href="https://blog.dragonsoft.us">Dragonsoft Technology View</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">When it comes to <strong>non-schema-bound-views</strong> (NSB View)&nbsp;it is important to remember that <strong>there is no schema bound between objects used in the view and metadata for a view</strong>.</p>



<p class="wp-block-paragraph">Let&#8217;s look at the following scenario:</p>



<pre class="wp-block-preformatted"><pre class="brush: sql; title: ; notranslate">-- Create Table_1
CREATE TABLE dbo.Table_1 (Col_A int NULL, Col_B numeric(18, 0) NULL, Col_C nvarchar(50) NULL) ON &#x5B;PRIMARY]
GO;
--
-- Add data
INSERT dbo.Table_1 (Col_A, Col_B, Col_C) VALUES (10, 10.5, &#039;ABC&#039;);
INSERT dbo.Table_1 (Col_A, Col_B, Col_C) VALUES (20, 0.5, &#039;DEFG&#039;);
--
-- Create View
CREATE view &#x5B;dbo].&#x5B;vw_MyView] AS
SELECT TBL.*, (TBL.Col_A * TBL.Col_B) AS ColMult FROM dbo.Table_1 TBL
GO;</pre></pre>



<p class="wp-block-paragraph">An attempt to use the view would give the following result:</p>



<pre class="wp-block-preformatted"><pre class="brush: plain; title: ; notranslate">Col_A, Col_B, Col_C, ColMult
=================================
10, 10.5, ABC, 105
20, 0.5, DEFG, 10
</pre></pre>



<p class="wp-block-paragraph">So far we are OK. But &#8220;fun&#8221; starts when a new column is&nbsp;added to the table</p>



<p class="wp-block-paragraph"><strong>Case #1 &#8211; bad:</strong></p>



<pre class="wp-block-preformatted"><pre class="brush: sql; title: ; notranslate">ALTER TABLE dbo.Table_1 ADD Col_D nvarchar(10) NOT NULL DEFAULT &#039;A&#039;</pre></pre>



<p class="wp-block-paragraph">Result of the view:</p>



<pre class="wp-block-preformatted"><pre class="brush: plain; title: ; notranslate">Col_A, Col_B, Col_C, ColMult
=================================
10, 10.5, ABC, A
20, 0.5, DEFG, A</pre></pre>



<p class="wp-block-paragraph">Well&#8230; OK, it is not as bad as it seems since we would find the problem almost immediately in application since types of expected and returned data are incompatible. Bad situation, but manageable.</p>



<p class="wp-block-paragraph"><strong>Case #2 &#8211; catastrophic:<br /></strong></p>



<pre class="wp-block-preformatted"><pre class="brush: sql; title: ; notranslate">ALTER TABLE dbo.Table_1 ADD Col_D int NOT NULL DEFAULT 0</pre></pre>



<p class="wp-block-paragraph">Let&#8217;s look at the result now:</p>



<pre class="wp-block-preformatted"><pre class="brush: plain; title: ; notranslate">Col_A, Col_B, Col_C, ColMult
=================================
10, 10.5, ABC, 0
20, 0.5, DEFG, 0</pre></pre>



<p class="wp-block-paragraph">ColMult now contains 0 (zero)!!! If we have significant logic which relays on that, then big troubles are coming our way.<br />Now this is a catastrophic situation. And not easily visible. And of course work just fine on the Developer&#8217;s machine&#8230;<br />Testing&#8230; Testing&#8230; Testing&#8230;</p>



<p class="wp-block-paragraph"><strong>What just happened here?!!!</strong></p>



<p class="wp-block-paragraph">This is when knowing <strong>non-schema-bound-views</strong> definition is important. And let&#8217;s remember &#8211; this is the default state of any new view created.</p>



<p class="wp-block-paragraph">First, what is the difference between schema-bound views and not bound?<br />In order to create a schema-bound view special view attributes needs to be specified. Let&#8217;s adjust original view definition as following:</p>



<pre class="wp-block-preformatted"><pre class="brush: sql; title: ; notranslate">--&amp;amp;nbsp;Alter View
ALTER VIEW &#x5B;dbo].&#x5B;vw_MyView] WITH SCHEMABINDING
AS
SELECT TBL.*, (TBL.Col_A*TBL.Col_B) AS ColMult FROM Table_1 TBL</pre></pre>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>SCHEMABINDING<br />Binds the view to the schema of the underlying table or tables. When SCHEMABINDING is specified, the base table or tables cannot be modified in a way that would affect the view definition. The view definition itself must first be modified or dropped to remove dependencies on the table that is to be modified. When you use SCHEMABINDING, the select_statement must include the two-part names (schema.object) of tables, views, or user-defined functions that are referenced. All referenced objects must be in the same database.</p></blockquote>



<p class="wp-block-paragraph">If we try executing the script above to add the column error would occur: &#8220;Syntax &#8216;*&#8217; is not allowed in schema-bound objects.&#8221;</p>



<p class="wp-block-paragraph">As you can see from the definition of the attribute, the view needs to be defined explicitly with all columns listed. So if we were to have columns specified explicitly, then no problem would occur in the first place, and is considered a good practice.</p>



<p class="wp-block-paragraph">In our scenario above this is not an intent. Our view is designed to adjust itself to a table structure changes.<br />Inconvenience&#8230; or advantage&#8230; I let you decide.</p>



<p class="wp-block-paragraph"><strong>Finding a cure</strong></p>



<p class="wp-block-paragraph">Is there a way to fix the problem?</p>



<p class="wp-block-paragraph">A dirty way of forcing the view metadata to be refreshed would simply recreate the view using the original script, but imagine you have 30+ of them &#8211; Not good, time-consuming, and error-prone.</p>



<p class="wp-block-paragraph">There is a simple, universal and &#8220;human-error protected&#8221; way &#8211; after changes are applied to tables, simply refresh metadata of every view defined:</p>



<pre class="wp-block-preformatted"><pre class="brush: sql; title: ; notranslate">DECLARE @Table_Name varchar(120)
DECLARE Refresh_Views CURSOR FOR
SELECT Table_Name FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = &#039;VIEW&#039; AND TABLE_SCHEMA = &#039;dbo&#039;
OPEN Refresh_Views
FETCH NEXT FROM Refresh_Views INTO @Table_Name
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC sp_refreshview @Table_Name
print &#039;refreshed View for: &#039; + @Table_Name
FETCH NEXT FROM Refresh_Views INTO @Table_Name
END
CLOSE Refresh_Views
DEALLOCATE Refresh_Views</pre></pre>



<p class="wp-block-paragraph">The script sp_refreshview would tell SQL Server to update the metadata for all non-schema-bound views in the current database.</p>



<p class="wp-block-paragraph"><strong>Note:</strong> Alternative: We can also use a more generic command &#8211; sp_refreshsqlmodule &#8211; which &#8220;updates the metadata for the specified non-schema-bound stored procedure, user-defined function, view, DML trigger, database-level DDL trigger, or server-level DDL trigger in the current database. Persistent metadata for these objects, such as data types of parameters, can become outdated because of changes to their underlying objects.&#8221; This may be even better since it would allow addressing any possible schema issues with any objects and not just views.</p>
<hr/><span style="font-size: 7pt">Copyright &copy; 2026 <strong><a href="https://blog.dragonsoft.us">Dragonsoft Technology View</a></strong>. This Feed is for personal non-commercial use only.</span><p>The post <a href="https://blog.dragonsoft.us/2010/02/26/tables-views-missing-columns/">SQL Server: Tables, Views and missing columns</a> appeared first on <a href="https://blog.dragonsoft.us">Dragonsoft Technology View</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.dragonsoft.us/2010/02/26/tables-views-missing-columns/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1109</post-id>	</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Lazy Loading (feed)
Minified using Disk
Database Caching 22/86 queries in 0.052 seconds using Disk

Served from: blog.dragonsoft.us @ 2026-06-08 21:23:16 by W3 Total Cache
-->