473,805 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

I'm sure this is an easy one...Error trap to skip over a "bad" object.

Hello, I have the following code to iterate through each view in a SQL
Server and call the "sp_refreshview " command against it. It works
great until it finds a view that is damaged, or otherwise cannot be
refreshed. Then the whole routine stops working.

Can someone please help me re-write this code so that any views that
fail the "sp_refreshview " command get skipped. I'm sure it's just a
matter of putting some basic error trapping into the loop, but I've had
a few goes at it and failed.

Many thanks.
DECLARE @DatabaseObject varchar(255)
DECLARE ObjectCursor CURSOR
FOR SELECT table_name FROM information_sch ema.tables WHERE table_type =
'view'
OPEN ObjectCursor
FETCH NEXT FROM ObjectCursor INTO @DatabaseObject
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC sp_refreshview @DatabaseObject
Print @DatabaseObject + ' was successfully refreshed.'
FETCH NEXT FROM ObjectCursor INTO @DatabaseObject
END
CLOSE ObjectCursor
DEALLOCATE ObjectCursor
GO

Mar 30 '06 #1
5 2685
(ro******@gmail .com) writes:
Hello, I have the following code to iterate through each view in a SQL
Server and call the "sp_refreshview " command against it. It works
great until it finds a view that is damaged, or otherwise cannot be
refreshed. Then the whole routine stops working.

Can someone please help me re-write this code so that any views that
fail the "sp_refreshview " command get skipped. I'm sure it's just a
matter of putting some basic error trapping into the loop, but I've had
a few goes at it and failed.


If you are on SQL 2005, lookup TRY-CATCH in Books Online.

If you are on SQL 2000, you could possibly do the linked-server trick:
http://www.sommarskog.se/error-handl...linked-servers.

I'm not into refreshing views myself, but I can't think of a way to
detect this condition before-hand.

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Mar 30 '06 #2
Thanks Erland,

I need to refresh all views because there are some views that have
embedded views within them, using a Select * statement. When the
underlying view changes (new column etc), the parent view does not pick
up the new column in the embedded view that it references.

Using SQL Server 2000. Surely there must be a simple way to trap the
error and skip over it right?

Perhaps just after the following line...

EXEC sp_refreshview @DatabaseObject

....you examine @@Error and ignore or continue in the loop? Sorry, I'm
primarily a VB developer, so this TSQL has got me a little puzzled.

I'll give your website a read. Thanks again.

Mar 30 '06 #3
> ...you examine @@Error and ignore or continue in the loop?

Some errors will abort the batch so you are SOL after the error. If the
linked server doesn't work for you, you might try preceeding the
sp_refreshview with a select statement with SET FMTONLY ON. That will allow
you detect the error and skip the sp_refreshview for problem views.

DECLARE @DatabaseObject nvarchar(261)
DECLARE ObjectCursor CURSOR FAST_FORWARD READ_ONLY
FOR SELECT
QUOTENAME(TABLE _SCHEMA) +
'.' +
QUOTENAME(TABLE _NAME)
FROM INFORMATION_SCH EMA.TABLES
WHERE table_type = 'VIEW'

OPEN ObjectCursor
WHILE 1 = 1
BEGIN
FETCH NEXT FROM ObjectCursor INTO @DatabaseObject
IF @@FETCH_STATUS = -1 BREAK
PRINT 'Refreshing view ' + @DatabaseObject
EXEC ('SET FMTONLY ON SELECT * FROM ' + @DatabaseObject )
IF @@ERROR = 0
BEGIN
EXEC sp_refreshview @DatabaseObject
PRINT 'View ' + @DatabaseObject + ' refreshed'
END
ELSE
BEGIN
PRINT 'Error refreshing view ' + @DatabaseObject
END
END

CLOSE ObjectCursor
DEALLOCATE ObjectCursor
GO
--
Hope this helps.

Dan Guzman
SQL Server MVP

<ro******@gmail .com> wrote in message
news:11******** *************@u 72g2000cwu.goog legroups.com...
Thanks Erland,

I need to refresh all views because there are some views that have
embedded views within them, using a Select * statement. When the
underlying view changes (new column etc), the parent view does not pick
up the new column in the embedded view that it references.

Using SQL Server 2000. Surely there must be a simple way to trap the
error and skip over it right?

Perhaps just after the following line...

EXEC sp_refreshview @DatabaseObject

...you examine @@Error and ignore or continue in the loop? Sorry, I'm
primarily a VB developer, so this TSQL has got me a little puzzled.

I'll give your website a read. Thanks again.

Apr 1 '06 #4
[My newsserver had an outage, and my original post got lost. Now that it's
back, I'm reposting]

(ro******@gmail .com) writes:
I need to refresh all views because there are some views that have
embedded views within them, using a Select * statement.
Did anyone tell you that this is bad practice? :-)
Using SQL Server 2000. Surely there must be a simple way to trap the
error and skip over it right?

Perhaps just after the following line...

EXEC sp_refreshview @DatabaseObject

...you examine @@Error and ignore or continue in the loop? Sorry, I'm
primarily a VB developer, so this TSQL has got me a little puzzled.


The problem is that there are quite few errors that abort the batch, and
those you cannot trap easily in SQL 2000. I seem to recall that refreshview
errors belongs to this group. The linked-server trick is a serious kludge,
but for this case it could be worth the pain.

Then again, if you are a VB developer, just code the loop in a VB program
or in VB script. That's probably easier than setting up linked servers for
this task.

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.seBooks Online for SQL
Server 2005
athttp://www.microsoft.c om/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000
athttp://www.microsoft.c om/sql/prodinfo/previousversion s/books.mspx
Apr 2 '06 #5
Hi Dan,

Many thanks for your response. This code does exactly what I'm after.
It skipped over the bad queries and kept refreshing the good ones. I
modified the following line to speed it up a little bit.

EXEC ('SET FMTONLY ON SELECT * FROM ' + @DatabaseObject + 'Where 1=0')

Notice the Where 1 = 0 clause? Much quicker now.

Thanks Dan and Erland. Problem solved.

p.s. Erland. I am going to start another thread on the evils of
embedded queries. I have heard a lot of people say that this is a bad
practice, however I've never heard any really compelling evidence to
say why.

Apr 3 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2199
by: Warren Oates | last post by:
I ran into an interesting gotcha with unix timestamps. I've got a page where the user inputs the date with drop-down boxes (easy to deal with), that my script sees as (say) $d $m $y, not necessarily in that order, but you get the idea, nicely formatted with leading zeroes and so on. Then I run it through checkdate($m, $d, $y) just to make sure the user doesn't think there's 31 days in September or whatnot.
12
1957
by: Georg Brandl | last post by:
Hello, in follow-up to the recent "dictionary accumulator" thread, I wrote a little module with several subclassed dicts. Comments (e.g. makes it sense to use super), corrections, etc.? Is this PEP material? Docstrings, Documentation and test cases are to be provided later.
7
20386
by: Pablo J Royo | last post by:
Hello: i have a function that reads a file as an argument and returns a reference to an object that contains some information obtained from the file: FData &ReadFile(string FilePath); But , for example, when the file doesnt exists, i should not return any reference to a bad constructed object, so i need something as a NULL reference object. I suppose i could return a pointer instead, but i have some code written with references which...
6
2320
by: Java script Dude | last post by:
We just discovered another IE bug. When an html form contains an element with a name of `name` IE's internal index screws up the .name property of the containing form to point to the bad element object instead of containing the string name property of the form. Here is the simplified test case: <html>
3
2709
by: Pavils Jurjans | last post by:
Hello, I have bumped upon this problem: I do some client-side form processing with JavaScript, and for this I loop over all the forms in the document. In order to identify them, I read their "name" property (which sources from "name" HTML attribue). The problem is, that if the form contains form control named "name", it overwrites the form name property. In fact, I'm quite surprised that it's so easy to spoil any of the form object...
11
9433
by: Daniele Benegiamo | last post by:
Is the following program standard-compliant? I've compiled it with some compilers and no errors or warnings are produced, but this is not a demonstration. The "incriminated" part is the declaration: extern "C" struct X; that is then defined as:
5
3971
by: Alfredo Magallón Arbizu | last post by:
Hi, I have an ASP.NET app that works perfectly in Windows Server 2003, but fails in Windows 2000. It fails when trying to read the data in an Excel Workbook (range.value)... The error is: COMException (0x80020008): Bad Variable Type
9
2288
by: Christopher Benson-Manica | last post by:
In your experience, what is the prevalence of "good" C++ (i.e., the code makes full use of solid language features and the STL) versus C++ written in ignorance (or defiance) of one or more key features of the language? -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
0
3783
by: masterjuan | last post by:
Networks Hacking (hack C:/ drives, severs...)and security holes all on my website & hacking commands and I explain ways of erasing your tracks so you dont get caught doing "bad" things... What do you think? check out my website its about hacking networks and step by step guides of how to do it all. Any suggestions on information or anything you think would be interesting to write about please tell me. Also what do you think of the...
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10607
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10359
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10364
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7645
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6875
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5677
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4317
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.