<?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>youdidwhatwithtsql.com &#187; views</title>
	<atom:link href="http://www.youdidwhatwithtsql.com/tag/views/feed" rel="self" type="application/rss+xml" />
	<link>http://www.youdidwhatwithtsql.com</link>
	<description>making DBAs everywhere curse!</description>
	<lastBuildDate>Tue, 31 Jan 2012 12:21:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>View or function &#8216;dbo.Viewname&#8217; has more column names specified than columns defined</title>
		<link>http://www.youdidwhatwithtsql.com/view-or-function-dbo-tablename-has-more-column-names-specified-than-columns-defined/546</link>
		<comments>http://www.youdidwhatwithtsql.com/view-or-function-dbo-tablename-has-more-column-names-specified-than-columns-defined/546#comments</comments>
		<pubDate>Mon, 18 Jan 2010 21:08:22 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[views]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/view-or-function-dbo-tablename-has-more-column-names-specified-than-columns-defined/546</guid>
		<description><![CDATA[If you ever encounter this SQL Server error when selecting from a view then somebody has probably dropped columns from the base table. Here&#8217;s a quick run through of the problem. ?View Code TSQLCREATE TABLE dbo.Contact &#40; Id INTEGER IDENTITY&#40;1,1&#41; NOT NULL PRIMARY KEY CLUSTERED, FirstName VARCHAR&#40;50&#41; NOT NULL, LastName VARCHAR&#40;50&#41; NOT NULL, DOB DATETIME [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/view-or-function-dbo-tablename-has-more-column-names-specified-than-columns-defined/546">View or function &#8216;dbo.Viewname&#8217; has more column names specified than columns defined</a></p>
]]></description>
			<content:encoded><![CDATA[<p>If you ever encounter this <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a> error when selecting from a view then somebody has probably dropped columns from the base table. Here&#8217;s a quick run through of the problem.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p546code9'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p5469"><td class="code" id="p546code9"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> dbo.<span style="color: #202020;">Contact</span>
<span style="color: #808080;">&#40;</span>
	Id <span style="color: #0000FF;">INTEGER</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span> <span style="color: #0000FF;">CLUSTERED</span>,
	FirstName <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">50</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	LastName <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">50</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	DOB <span style="color: #0000FF;">DATETIME</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	Phone <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">30</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>,
	Email <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">200</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>,
	Mobile <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">20</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>,
	Website <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">100</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NULL</span>
<span style="color: #808080;">&#41;</span>;</pre></td></tr></table></div>

<p>Insert a test record.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p546code10'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p54610"><td class="code" id="p546code10"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Contacts</span>
<span style="color: #808080;">&#40;</span>
	FirstName,
	LastName,
	DOB,
	Email,
	Website
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">VALUES</span>
<span style="color: #808080;">&#40;</span>
	<span style="color: #FF0000;">'Rhys'</span>,
	<span style="color: #FF0000;">'Campbell'</span>,
	<span style="color: #FF0000;">'01-Jun-80'</span>,
	<span style="color: #FF0000;">'noone@tempinbox.com'</span>,
	<span style="color: #FF0000;">'http://www.youdidwhatwithtsql.com'</span>
<span style="color: #808080;">&#41;</span>;</pre></td></tr></table></div>

<p>Create a view on this table.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p546code11'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p54611"><td class="code" id="p546code11"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">VIEW</span> vw_Contacts
<span style="color: #0000FF;">AS</span>
	<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
	<span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">Contacts</span>;</pre></td></tr></table></div>

<p>Verify the view is functional</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p546code12'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p54612"><td class="code" id="p546code12"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span> vw_Contacts;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/view_results1.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="sql server view results" border="0" alt="view results thumb1 View or function dbo.Viewname has more column names specified than columns defined" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/view_results_thumb1.png" width="644" height="62" /></a> </p>
<p>Now drop a column from the <strong>Contacts </strong>table.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p546code13'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p54613"><td class="code" id="p546code13"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">TABLE</span> dbo.<span style="color: #202020;">Contacts</span> <span style="color: #0000FF;">DROP</span> <span style="color: #0000FF;">COLUMN</span> Phone;</pre></td></tr></table></div>

<p>Now try selecting from the view again.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p546code14'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p54614"><td class="code" id="p546code14"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span> vw_Contacts;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/sql_server_view_error.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="View or function &#39;vw_Contacts&#39; has more column names specified than columns defined" border="0" alt="sql server view error thumb View or function dbo.Viewname has more column names specified than columns defined" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/sql_server_view_error_thumb.png" width="644" height="72" /></a> </p>
<p>So what&#8217;s going on here? The view is expecting the <strong>Phone</strong> column to still be in the <strong>Contact</strong> table. We created our view with an asterisk so this shouldn&#8217;t matter right? <a href="http://en.wikipedia.org/wiki/Shooting_Stars#Catchphrases" target="_blank">Uvavu</a>! SQL Server stores metadata about the view when you create it. Changes to underlying tables can cause issues. Luckily the fix is easy.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p546code15'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p54615"><td class="code" id="p546code15"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">sp_refreshview</span> <span style="color: #FF0000;">'vw_Contacts'</span>;</pre></td></tr></table></div>


<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p546code16'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p54616"><td class="code" id="p546code16"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span> vw_Contacts;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/fixed_sql_server_view.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="fixed sql server view" border="0" alt="fixed sql server view thumb View or function dbo.Viewname has more column names specified than columns defined" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/fixed_sql_server_view_thumb.png" width="644" height="64" /></a> </p>
<p>This procedure will update the metadata stored by the view making it functional again. Presumably this is akin to dropping and recreating the view. This is a fairly trivial example but in a large system, with lots of views, and lots of developers, this could cause big headaches. I recommend you read the <a href="http://msdn.microsoft.com" target="_blank">MSDN</a> page for <a href="http://msdn.microsoft.com/en-us/library/ms187821.aspx" target="_blank">sp_refreshview</a>. There&#8217;s a couple of useful scripts in the comments section for making light work of this.</p>
<p><map name='google_ad_map_546_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/546?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_546_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=546&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fview-or-function-dbo-tablename-has-more-column-names-specified-than-columns-defined%2F546' title="View or function dbo.Viewname has more column names specified than columns defined" alt=" View or function dbo.Viewname has more column names specified than columns defined" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/view-or-function-dbo-tablename-has-more-column-names-specified-than-columns-defined/546">View or function &#8216;dbo.Viewname&#8217; has more column names specified than columns defined</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/view-or-function-dbo-tablename-has-more-column-names-specified-than-columns-defined/546" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/view-or-function-dbo-tablename-has-more-column-names-specified-than-columns-defined/546/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Views or functions cannot reference themselves directly or indirectly</title>
		<link>http://www.youdidwhatwithtsql.com/views-or-functions-cannot-reference-themselves-directly-or-indirectly/519</link>
		<comments>http://www.youdidwhatwithtsql.com/views-or-functions-cannot-reference-themselves-directly-or-indirectly/519#comments</comments>
		<pubDate>Thu, 14 Jan 2010 21:18:38 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[self-reference]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[views]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/views-or-functions-cannot-reference-themselves-directly-or-indirectly/519</guid>
		<description><![CDATA[Today I received the following SQL Server error which I had never encountered before. Msg 4429, Level 16, State 1, Line 1 View or function &#8216;Table_1&#8242; contains a self-reference. Views or functions cannot reference themselves directly or indirectly. Msg 4413, Level 16, State 1, Line 1 Could not use view or function &#8216;Test.Table_1&#8242; because of [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/views-or-functions-cannot-reference-themselves-directly-or-indirectly/519">Views or functions cannot reference themselves directly or indirectly</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Today I received the following <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a> error which I had never encountered before.</p>
<blockquote><p>Msg 4429, Level 16, State 1, Line 1      <br />View or function &#8216;Table_1&#8242; contains a self-reference. Views or functions cannot reference themselves directly or indirectly.       <br />Msg 4413, Level 16, State 1, Line 1       <br />Could not use view or function &#8216;Test.Table_1&#8242; because of binding errors.</p>
</blockquote>
<p>Here&#8217;s a quick walk-through of the issue.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p519code22'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p51922"><td class="code" id="p519code22"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Table_1<span style="color: #808080;">&#93;</span>
<span style="color: #808080;">&#40;</span>
	<span style="color: #808080;">&#91;</span>id<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">int</span><span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #808080;">&#91;</span>test<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">nchar</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #000;">10</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>
<span style="color: #808080;">&#41;</span>;</pre></td></tr></table></div>

<p>Insert some test data</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p519code23'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p51923"><td class="code" id="p519code23"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Table_1</span>
<span style="color: #808080;">&#40;</span>
	test
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'One'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Two'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Three'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Four'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Five'</span>;</pre></td></tr></table></div>

<p>Add a schema called &#8216;Test&#8217; to the database by running the TSQL below. We will then create a view in this schema.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p519code24'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p51924"><td class="code" id="p519code24"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">SCHEMA</span> Test;</pre></td></tr></table></div>

<p>Now create this view.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p519code25'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p51925"><td class="code" id="p519code25"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">VIEW</span> Test.<span style="color: #202020;">Table_1</span>
<span style="color: #0000FF;">AS</span>
	<span style="color: #0000FF;">SELECT</span> Id, Test
	<span style="color: #0000FF;">FROM</span> Table_1;</pre></td></tr></table></div>

<p>Now try to select from this view.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/sql_view_self_reference.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="sql server view self reference error" border="0" alt="sql view self reference thumb Views or functions cannot reference themselves directly or indirectly" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/sql_view_self_reference_thumb.png" width="644" height="72" /></a>&#160;</p>
<p>I&#8217;d been creating lots of views for a third party application we&#8217;ve started to deploy at work. I&#8217;d simply created a new schema and named the views after the corresponding table in the database. This one was my fault for being a copy and paste monkey! I&#8217;d forgotten to specify the schema in one view. The fix?</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p519code26'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p51926"><td class="code" id="p519code26"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">VIEW</span> Test.<span style="color: #202020;">Table_1</span>
<span style="color: #0000FF;">AS</span>
	<span style="color: #0000FF;">SELECT</span> Id, Test
	<span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">Table_1</span>;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/view_results.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="view results" border="0" alt="view results thumb Views or functions cannot reference themselves directly or indirectly" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/view_results_thumb.png" width="156" height="144" /></a> </p>
<p>Even though my user <a href="http://msdn.microsoft.com/en-us/library/ms190387.aspx" target="_blank">default schema</a> was set to dbo <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a> got its pants in a twist over this. So there you have it; it&#8217;s definitely good practice to specify schemas in your TSQL! </p>
<p><map name='google_ad_map_519_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/519?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_519_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=519&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fviews-or-functions-cannot-reference-themselves-directly-or-indirectly%2F519' title="Views or functions cannot reference themselves directly or indirectly" alt=" Views or functions cannot reference themselves directly or indirectly" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/views-or-functions-cannot-reference-themselves-directly-or-indirectly/519">Views or functions cannot reference themselves directly or indirectly</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/views-or-functions-cannot-reference-themselves-directly-or-indirectly/519" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/views-or-functions-cannot-reference-themselves-directly-or-indirectly/519/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

