<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Search results matching tags 'SQL Server Data Tools', 'Visual Studio 2010', and 'juneau'</title><link>http://sqlblog.com/search/SearchResults.aspx?o=DateDescending&amp;tag=SQL+Server+Data+Tools,Visual+Studio+2010,juneau&amp;orTags=0</link><description>Search results matching tags 'SQL Server Data Tools', 'Visual Studio 2010', and 'juneau'</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>Workaround for datadude deployment bug - NullReferenceException</title><link>http://sqlblog.com/blogs/jamie_thomson/archive/2011/11/21/workaround-for-datadude-deployment-bug.aspx</link><pubDate>Mon, 21 Nov 2011 08:56:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:39903</guid><dc:creator>jamiet</dc:creator><description>
&lt;p&gt;I have come across a bug in Visual Studio 2010 Database Projects (&lt;a href="http://www.sqlservercentral.com/questions/tags/visual+studio/" target="_blank"&gt;aka datadude aka DPro aka Visual Studio Database Development Tools aka Visual Studio Team Edition for Database Professionals aka Juneau aka SQL Server Data Tools&lt;/a&gt;) that other people may encounter so, for the purposes of googling, I'm writing this blog post about it. Through my own googling I discovered that a Connect bug had already been raised about it (&lt;a href="https://connect.microsoft.com/VisualStudio/feedback/details/693158/vs2010-database-project-deploy-sqldeploytask-task-failed-unexpectedly-nullreferenceexception#details" target="_blank"&gt;VS2010 Database project deploy - “SqlDeployTask” task failed unexpectedly, NullReferenceException&lt;/a&gt;), and coincidentally enough it was raised by my former colleague Tom Hunter (whom I have &lt;a href="http://sqlblog.com/blogs/jamie_thomson/archive/2010/11/25/sql-server-2008-sp1-cu-6-includes-small-changes-to-dtsx-files.aspx" target="_blank"&gt;mentioned here before as the superhuman Tom Hunter&lt;/a&gt;) although it has not (at this time) received a reply from Microsoft. Tom provided a repro, namely that this syntactically valid function definition:&lt;/p&gt;
&lt;code style="font-size:12px;"&gt;&lt;span style="color:blue;"&gt;CREATE FUNCTION &lt;/span&gt;&lt;span style="color:black;"&gt;[dbo].[Function1]&lt;br&gt;&lt;/span&gt;&lt;span style="color:gray;"&gt;()&lt;br&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;RETURNS TABLE&lt;br&gt;AS&lt;br&gt;RETURN &lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;WITH &lt;/span&gt;&lt;span style="color:black;"&gt;cte &lt;/span&gt;&lt;span style="color:blue;"&gt;AS &lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;SELECT &lt;/span&gt;&lt;span style="color:black;"&gt;1 &lt;/span&gt;&lt;span style="color:blue;"&gt;AS &lt;/span&gt;&lt;span style="color:black;"&gt;[c1]&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;FROM &lt;/span&gt;&lt;span style="color:black;"&gt;[$(Database3)].[dbo].[Table1]&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:gray;"&gt;)&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:blue;"&gt;SELECT &lt;/span&gt;&lt;span style="color:black;"&gt;1 &lt;/span&gt;&lt;span style="color:blue;"&gt;AS &lt;/span&gt;&lt;span style="color:black;"&gt;[c1]&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:blue;"&gt;FROM &lt;/span&gt;&lt;span style="color:black;"&gt;cte&lt;br&gt;&lt;/span&gt;&lt;span style="color:gray;"&gt;) &lt;/span&gt;&lt;/code&gt;

&lt;p&gt;would produce this nasty unhelpful error upon deployment:&lt;br&gt;&lt;/p&gt;
&lt;font color="red"&gt;
&lt;p&gt;C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\TeamData\Microsoft.Data.Schema.TSqlTasks.targets(120,5): Error MSB4018: The "SqlDeployTask" task failed unexpectedly.&lt;br&gt;&lt;span style="font-weight:bold;"&gt;System.NullReferenceException: Object reference not set to an instance of an object&lt;/span&gt;.&lt;br&gt;&amp;nbsp;&amp;nbsp; at Microsoft.Data.Schema.Sql.SchemaModel.SqlModelComparerBase.VariableSubstitution(SqlScriptProperty propertyValue, IDictionary`2 variables, Boolean&amp;amp; isChanged)&lt;br&gt;&amp;nbsp;&amp;nbsp; at Microsoft.Data.Schema.Sql.SchemaModel.SqlModelComparerBase.ArePropertiesEqual(IModelElement source, IModelElement target, ModelPropertyClass propertyClass, ModelComparerConfiguration configuration)&lt;br&gt;&amp;nbsp;&amp;nbsp; at Microsoft.Data.Schema.SchemaModel.ModelComparer.CompareProperties(IModelElement sourceElement, IModelElement targetElement, ModelComparerConfiguration configuration, ModelComparisonChangeDefinition changes)&lt;br&gt;&amp;nbsp;&amp;nbsp; at Microsoft.Data.Schema.SchemaModel.ModelComparer.CompareElementsWithoutCompareName(IModelElement sourceElement, IModelElement targetElement, ModelComparerConfiguration configuration, Boolean parentExplicitlyIncluded, Boolean compareElementOnly, ModelComparisonResult result, ModelComparisonChangeDefinition changes)&lt;br&gt;&amp;nbsp;&amp;nbsp; at Microsoft.Data.Schema.SchemaModel.ModelComparer.CompareElementsWithSameType(IModelElement sourceElement, IModelElement targetElement, ModelComparerConfiguration configuration, ModelComparisonResult result, Boolean ignoreComparingName, Boolean parentExplicitlyIncluded, Boolean compareElementOnly, Boolean compareFromRootElement, ModelComparisonChangeDefinition&amp;amp; changes)&lt;br&gt;&amp;nbsp;&amp;nbsp; at Microsoft.Data.Schema.SchemaModel.ModelComparer.CompareChildren(IModelElement sourceElement, IModelElement targetElement, ModelComparerConfiguration configuration, Boolean parentExplicitlyIncluded, Boolean compareParentElementOnly, ModelComparisonResult result, ModelComparisonChangeDefinition changes, Boolean isComposing)&lt;br&gt;&amp;nbsp;&amp;nbsp; at Microsoft.Data.Schema.SchemaModel.ModelComparer.CompareElementsWithoutCompareName(IModelElement sourceElement, IModelElement targetElement, ModelComparerConfiguration configuration, Boolean parentExplicitlyIncluded, Boolean compareElementOnly, ModelComparisonResult result, ModelComparisonChangeDefinition changes)&lt;br&gt;&amp;nbsp;&amp;nbsp; at Microsoft.Data.Schema.SchemaModel.ModelComparer.CompareElementsWithSameType(IModelElement sourceElement, IModelElement targetElement, ModelComparerConfiguration configuration, ModelComparisonResult result, Boolean ignoreComparingName, Boolean parentExplicitlyIncluded, Boolean compareElementOnly, Boolean compareFromRootElement, ModelComparisonChangeDefinition&amp;amp; changes)&lt;br&gt;&amp;nbsp;&amp;nbsp; at Microsoft.Data.Schema.SchemaModel.ModelComparer.CompareChildren(IModelElement sourceElement, IModelElement targetElement, ModelComparerConfiguration configuration, Boolean parentExplicitlyIncluded, Boolean compareParentElementOnly, ModelComparisonResult result, ModelComparisonChangeDefinition changes, Boolean isComposing)&lt;br&gt;&amp;nbsp;&amp;nbsp; at Microsoft.Data.Schema.SchemaModel.ModelComparer.CompareElementsWithoutCompareName(IModelElement sourceElement, IModelElement targetElement, ModelComparerConfiguration configuration, Boolean parentExplicitlyIncluded, Boolean compareElementOnly, ModelComparisonResult result, ModelComparisonChangeDefinition changes)&lt;br&gt;&amp;nbsp;&amp;nbsp; at Microsoft.Data.Schema.SchemaModel.ModelComparer.CompareElementsWithSameType(IModelElement sourceElement, IModelElement targetElement, ModelComparerConfiguration configuration, ModelComparisonResult result, Boolean ignoreComparingName, Boolean parentExplicitlyIncluded, Boolean compareElementOnly, Boolean compareFromRootElement, ModelComparisonChangeDefinition&amp;amp; changes)&lt;br&gt;&amp;nbsp;&amp;nbsp; at Microsoft.Data.Schema.SchemaModel.ModelComparer.CompareAllElementsForOneType(ModelElementClass type, ModelComparerConfiguration configuration, ModelComparisonResult result, Boolean compareOrphanedElements)&lt;br&gt;&amp;nbsp;&amp;nbsp; at Microsoft.Data.Schema.SchemaModel.ModelComparer.CompareStore(ModelStore source, ModelStore target, ModelComparerConfiguration configuration)&lt;br&gt;&amp;nbsp;&amp;nbsp; at Microsoft.Data.Schema.Build.SchemaDeployment.CompareModels()&lt;br&gt;&amp;nbsp;&amp;nbsp; at Microsoft.Data.Schema.Build.SchemaDeployment.PrepareBuildPlan()&lt;br&gt;&amp;nbsp;&amp;nbsp; at Microsoft.Data.Schema.Build.SchemaDeployment.Execute(Boolean executeDeployment)&lt;br&gt;&amp;nbsp;&amp;nbsp; at Microsoft.Data.Schema.Build.SchemaDeployment.Execute()&lt;br&gt;&amp;nbsp;&amp;nbsp; at Microsoft.Data.Schema.Tasks.DBDeployTask.Execute()&lt;br&gt;&amp;nbsp;&amp;nbsp; at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()&lt;br&gt;&amp;nbsp;&amp;nbsp; at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask, Boolean&amp;amp; taskResult)&lt;br&gt;&amp;nbsp;&amp;nbsp; Done executing task "SqlDeployTask" -- FAILED.&lt;br&gt;&amp;nbsp; Done building target "DspDeploy" in project "Lloyds.UKTax.DB.UKtax.dbproj" -- FAILED.&lt;br&gt;&amp;nbsp;Done executing task "CallTarget" -- FAILED.&lt;br&gt;Done building target "DBDeploy" in project &lt;br&gt;&lt;/p&gt;
&lt;/font&gt;
&lt;p&gt;It turns out there are a certain set of circumstances that need to be met for this error to occur:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The object being deployed is an inline function&amp;nbsp; (may also exist for multistatement and scalar functions - I haven't tested that)&lt;br&gt;&lt;/li&gt;

&lt;li&gt;That object includes SQLCMD variable references&lt;/li&gt;

&lt;li&gt;The object has already been deployed successfully&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Just to reiterate that last bullet point, the error does not occur when you deploy the function for the first time, only on the subsequent deployment.&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;Luckily I have a direct line into a guy on the development team so I fired off an email on Friday evening and today (Monday) I received a reply back telling me that there is a simple fix, one simply has to remove the parentheses that wrap the SQL statement. So, in the case of Tom's repro, the function definition simply needs to be changed to:&lt;/p&gt;
&lt;p&gt;
&lt;code style="font-size:12px;"&gt;&lt;span style="color:blue;"&gt;CREATE FUNCTION &lt;/span&gt;&lt;span style="color:black;"&gt;[dbo].[Function1]&lt;br&gt;&lt;/span&gt;&lt;span style="color:gray;"&gt;()&lt;br&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;RETURNS TABLE&lt;br&gt;AS&lt;br&gt;RETURN &lt;/span&gt;&lt;span style="color:green;"&gt;--(&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;WITH &lt;/span&gt;&lt;span style="color:black;"&gt;cte &lt;/span&gt;&lt;span style="color:blue;"&gt;AS &lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;SELECT &lt;/span&gt;&lt;span style="color:black;"&gt;1 &lt;/span&gt;&lt;span style="color:blue;"&gt;AS &lt;/span&gt;&lt;span style="color:black;"&gt;[c1]&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;FROM &lt;/span&gt;&lt;span style="color:black;"&gt;[$(Database3)].[dbo].[Table1]&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:gray;"&gt;)&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:blue;"&gt;SELECT &lt;/span&gt;&lt;span style="color:black;"&gt;1 &lt;/span&gt;&lt;span style="color:blue;"&gt;AS &lt;/span&gt;&lt;span style="color:black;"&gt;[c1]&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color:blue;"&gt;FROM &lt;/span&gt;&lt;span style="color:black;"&gt;cte&lt;br&gt;&lt;/span&gt;&lt;span style="color:green;"&gt;--) &lt;/span&gt;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;i&gt;I have commented out the offending parentheses rather than removing them just to emphasize the point.
&lt;/i&gt;&lt;/p&gt;
&lt;p&gt;Thereafter the function will deploy fine. I tested this out on my own project this morning and can confirm that this fix does indeed work.&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;I have been told that the bug CAN be reproduced in the Release Candidate (RC) 0 build of SQL Server Data Tools in SQL Server 2012 so am hoping that a fix makes it in for the Release-To-Manufacturing (RTM) build.&lt;/p&gt;

&lt;p&gt;Hope this helps&lt;/p&gt;

&lt;p&gt;&lt;a href="http://twitter.com/jamiet" target="_blank"&gt;@jamiet&lt;/a&gt; &lt;/p&gt;</description></item></channel></rss>