Link to home
Start Free TrialLog in
Avatar of rmk
rmk

asked on

How to Automatically "recompile" views

I have a SQL Server 2000 database with hundreds of views and stored procedures. Views are often nested several levels. As a very simple example I have a view called vw1 that looks like:

select tbl1.* from tbl1 inner join tbl2 on tbl1.colx = tbl2.colx

If I then add a new column to tbl1, this new column is not reflected in the vw1 view. To get the column to show up I have to open vw1 in design mode, uncheck the * in the diagram pane, and then recheck the * in the diagram pane. I have to do the same thing for every view that is base on this view if a select * is used. It seems strange to me that if a view only involves 1 table, e.g. select * from tbl1, the new column shows up automatically. With hundreds of views, it's all too easy to miss one when a new column is added to an underlying table. Is there some transact sql command or some other mechanism to automatically "recompile" all views?
Avatar of jayrod
jayrod
Flag of United States of America image

are there any indexes on this view? if so that would cause a problem with scema changes.

another route you could go would be to put schema-binding on each of the views. So that if any of the dependent schema changes it would force you to go change the view as well.

While the above isn't automatic it would be a good way to make sure changes are made when you want them to be
Avatar of arbert
arbert

you can use sp_refreshview, but this isn't exactly what you want....Views aren't compiled.....

Schemabinding just forces you to change the view--doesn't automatically do it for you.  Personally, I think it's a bad idea to have the system automatically change it for you.  

Evaluate your change, the impact it has on the system, and the dependencies.  If you have SQL Server adding columns in view for you, you might totally change query plans and not even be aware of it.
Avatar of rmk

ASKER

There are no indexes on the views.
Avatar of Scott Pletcher
The script below may help.  It will do an sp_refreshView on *all* non-system views.  As you know, it is best to refresh a view if needed, otherwise you can abend or strange results.

Unfortunately, I had to comment out the code to check to limit to a certain base table name(s) because there are not always entries in information_schema.view_table_usage for multi-table views.


DECLARE @tables NVARCHAR(4000)
DECLARE @view SYSNAME
DECLARE @sql NVARCHAR(500)

SET @tables = NULL


SET @sql = N'EXEC sp_refreshView '

DECLARE viewCsr CURSOR READ_ONLY FAST_FORWARD FOR
SELECT sob.name
FROM sysobjects sob WITH (NOLOCK)
LEFT OUTER JOIN information_schema.view_table_usage vtu ON sob.name = vtu.view_name
WHERE sob.xtype = 'V'
AND sob.name NOT LIKE 'sys%'
--AND @tables IS NULL
--OR CHARINDEX(OBJECT_NAME(vtu.table_name), N',' + @tables + N',') > 0

OPEN viewCsr

FETCH NEXT FROM viewCsr INTO @view
WHILE @@FETCH_STATUS = 0
BEGIN
      PRINT @sql + @view
      EXEC(@sql + @view)      
      FETCH NEXT FROM viewCsr INTO @view
END

DEALLOCATE viewCsr
Avatar of rmk

ASKER

That's getting pretty close to what I need. Since I'm an application developer and not a dba I was not aware of sp_refreshview which I now think of as a great tool. I've tested how it works and it's a big help. That still leaves me with the problem of nested views and order of dependency, i.e. somehow I have to be smart enough to do the refreshes from the lowest levels moving up to the topmost levels or the refresh is meaningless. So if I add a column to a table, I need to walk through all the select * views that use that table from bottom to top; or if I add a column to a vies, I need to walk through all the select * views that use that view from botton to top. As I document all of this it's becoming clearer to me that there is probably not a quick fix and that I need to more carefully evaluate the impact of such changes and be prepared to meticulously walk through all of my object dependencies.
I'm tellin ya... you love schema binding... it's the trick :P j/k

ya it doesn't seem that there is an easy solution
ASKER CERTIFIED SOLUTION
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rmk

ASKER

Last time I tried to play with schema binding I ran into all kinds of restrictions. Guess it's time I make a commitment to learning how to use this stuff.