Solved

How to Automatically "recompile" views

Posted on 2004-04-08
8
1,100 Views
Last Modified: 2012-06-27
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?
0
Comment
Question by:rmk
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 3

Expert Comment

by:jayrod
Comment Utility
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
0
 
LVL 34

Expert Comment

by:arbert
Comment Utility
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.
0
 

Author Comment

by:rmk
Comment Utility
There are no indexes on the views.
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
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
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:rmk
Comment Utility
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.
0
 
LVL 3

Expert Comment

by:jayrod
Comment Utility
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
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 250 total points
Comment Utility
Honestly, to deal with nested views, just run it two (or three, if necessary).  The first time will get the lowest-level views, the next run will get the next-up views, and so on.
0
 

Author Comment

by:rmk
Comment Utility
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.
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now