views:

70

answers:

2

I have a database project in my VS2010 solution. I recently changed a view and and changed a number of functions to use this view instead of going directly against a table. But now when I deploy I get errors on most of these functions because the column asked for does not exists in the view yet. The update of the view happends later than the update of UDF's. Is there any way to change this behaviour? Wouldn't the best thing be if the deploy script updated in this order: tables, views, SP and UDF. It seems like tables is updated first, but the views are just thrown in somewhere in the middle of the deploy script.

+1  A: 

Since UDFs may be used in views, and views may be used in UDFs, it would have to analyse all of them to determine a coherent deployment order - it would have to parse all of the SQL. And who knows what it's to do if you have dependencies on other databases.

Edit

There's no documented/supported way to force a deployment order, so far as I can see. With some minimal testing, it appears to me that UDFs (at least table valued) are always deployed before views.

Edit 2

Even stranger, it turns out it does do the dependency analysis. Looking at the .dbschema output file for a db project, I can see it produces a <Relationship Name="BodyDependencies"> element for the function, that does list the view/columns it depends on. But the deployment sql script still puts the view later on. Curious.

Edit 3

Probably final edit. I think, in general, that the problem is unsolvable. (Admittedly, the following only errors because I've specified schemabinding). If the old function definition relies on the old view definition, and the new function definition relies on the new view definition, there's no right way to alter the database using ALTERs:

create table dbo.T1 (
    ID int not null,
    C1 varchar(10) not null,
    C2 varchar(10) not null,
    C3 varchar(10) not null
)
go
create view dbo.V1
with schemabinding
as
    select ID,C1,C2
    from dbo.T1
go
create function dbo.F1()
returns table
with schemabinding
as
    return select ID,C1,C2 from dbo.V1 where ID=1
go
alter view dbo.V1
with schemabinding
as
    select ID,C1,C3
    from dbo.T1
go
alter function dbo.F1()
returns table
with schemabinding
as
    return select ID,C1,C3 from dbo.V1 where ID=1
go

result:

Msg 3729, Level 16, State 3, Procedure V1, Line 4
Cannot ALTER 'dbo.V1' because it is being referenced by object 'F1'.
Msg 207, Level 16, State 1, Procedure F1, Line 5
Invalid column name 'C3'.
Damien_The_Unbeliever
I see...I wasnt't aware of use of UDF's in views but that's true, you could probably use a scalar or a UDF without parameters in a view.But is there a way to get the deploy to run my view update before the UDF's? Since I know that it muste be updated and it doesn't have any other dependencies. But I don't want to manuelly update it on every database.
Markus
Even though it isn't a solution, it is an answer that it's not possible to solve right now. Too bad. Thanks for helping me out.Guess I'll do some Pre-Deployment checking instead and solve it that way.
Markus
A: 

I too have the same problem, and strangely this worked correctly in the 2008 version of dbproj.

I would suspect this is some sort of bug with 2010 version?

pnmcosta