DACFX Issue microsoft/DacFx#106
A function is order to deploy after the view that uses it and thus the view fails to deploy.
DacfX isn't putting the dependency into the model between the view and the function
- Use SELECT rather than VALUES i.e.
CREATE VIEW [dbo].[ViewNeedingFunction]
AS SELECT E.Id
FROM dbo.EmptyTable E
CROSS APPLY (SELECT (dbo.ScalarFunction() )) v(a)
- Refernce function in a non executing expression (a CASE expression that is can't be reached)
CREATE VIEW [dbo].[ViewNeedingFunction]
AS SELECT E.Id
FROM dbo.EmptyTable E
CROSS APPLY (VALUES (dbo.ScalarFunction() )) v(a)
WHERE CASE WHEN 1=1 THEN 1 ELSE dbo.ScalarFunction() END =1
- Use a deployment contributor
No example to show on this
- Create objects
CREATE VIEW [dbo].[ViewNeedingFunction]
AS SELECT v.a
FROM (VALUES (dbo.ScalarFunction() )) v(a)
GO
CREATE FUNCTION [dbo].[ScalarFunction]()
RETURNS INT
AS
BEGIN
RETURN 100
END
- Build project FunctionViewOrderWithCrossApply.sqlproj
- Access the dacpac (rename as zip)
- Open the model.xml
| Note there is no reference to the scalar function in the
QueryDependencies
Relationship
section
<Element Type="SqlView" Name="[dbo].[ViewNeedingFunction]">
<Property Name="QueryScript">
<Value><![CDATA[ SELECT v.a
FROM (SELECT (dbo.ScalarFunction() )) v(a)]]></Value>
</Property>
<Property Name="IsAnsiNullsOn" Value="True" />
<Relationship Name="Columns">
<Entry>
<Element Type="SqlComputedColumn" Name="[dbo].[ViewNeedingFunction].[a]" />
</Entry>
</Relationship>
<Relationship Name="QueryDependencies">
<Entry>
<References Name="[dbo].[ScalarFunction]" />
</Entry>
</Relationship>
<Relationship Name="Schema">
<Entry>
<References ExternalSource="BuiltIns" Name="[dbo]" />
</Entry>
</Relationship>
<Annotation Type="SysCommentsObjectAnnotation">
<Property Name="Length" Value="100" />
<Property Name="StartLine" Value="1" />
<Property Name="StartColumn" Value="1" />
<Property Name="HeaderContents" Value="CREATE VIEW [dbo].[ViewNeedingFunction]
	AS" />
</Annotation>
</Element>
<Element Type="SqlView" Name="[dbo].[ViewNeedingFunction]">
<Property Name="QueryScript">
<Value><![CDATA[ SELECT v.a
FROM (VALUES (dbo.ScalarFunction() )) v(a)]]></Value>
</Property>
<Property Name="IsAnsiNullsOn" Value="True" />
<Relationship Name="Columns">
<Entry>
<Element Type="SqlComputedColumn" Name="[dbo].[ViewNeedingFunction].[a]" />
</Entry>
</Relationship>
<Relationship Name="Schema">
<Entry>
<References ExternalSource="BuiltIns" Name="[dbo]" />
</Entry>
</Relationship>
<Annotation Type="SysCommentsObjectAnnotation">
<Property Name="Length" Value="100" />
<Property Name="StartLine" Value="1" />
<Property Name="StartColumn" Value="1" />
<Property Name="HeaderContents" Value="CREATE VIEW [dbo].[ViewNeedingFunction]
	AS" />
</Annotation>
</Element>