all groups > sql server programming > july 2006 >
You're in the

sql server programming

group:

Dynamic SQL error processing question


Dynamic SQL error processing question Pedja
7/31/2006 5:48:01 PM
sql server programming:
Hi,
I'm trying to develope a script which would be running against the server
every 10 minutes, and whose purpose would be to check availability of all
databases on the server.
I am using cursor and dynamic sql for that (see below). At the same time, I
want to return custom error message and to write event (error) to the
application log. Problem is that after the error occured (in sp_executesql),
script stops execution and it doesn't get into error processing part of the
code. Does anybody have an idea how to overcome this? Here is the code:

declare @dbName sysname,
@sql nvarchar(255),
@err int

declare crsDatabases cursor
for
select name from sysdatabases
where name not in ('northwind','pubs','tempdb','model') and
status & 32 != 32 and -- loading
status & 128 != 128 and -- recovering
status & 512 != 512 and -- offline
status & 4096 != 4096 and -- single user
status & 32768 !=32768 and -- emergency mode
status & 1073741824 !=1073741824 -- cleanly shutdown
for read only

open crsDatabases
fetch next from crsDatabases into @dbName
while @@fetch_status=0
begin
-- This part of the code calculates number of objects as a test of database
availaibility:
select @sql = N'select count(*) from [' + @dbName + N'].dbo.sysobjects
with(nolock)'
exec @err = sp_executesql @sql
-- This is where it stops in case of the error. It doesn't go further...
if (@err<>0)
begin
raiserror('Database: %s is unavailable.',16,1,@dbName) with log
close crsDatabases
deallocate crsDatabases
break
end
fetch next from crsDatabases into @dbName
end
close crsDatabases
deallocate crsDatabases
go

Thanks in advance,
Re: Dynamic SQL error processing question Arnie Rowland
8/1/2006 12:02:19 AM
Pedja,

First comment: Please do not have the Northwind and Pubs databases on a =
production server -the security issues are substantial.

I think that if you examined the use of sp_MSForEachDb (undocumented =
system stored procedure), you may find that you could eliminate the =
cursor, eliminate 75% of your code and have better control of the error =
conditions. You'll have to Google for help since it is not in BOL.

Here is an example that should get you thinking:

EXECUTE sp_MSForEachDB 'Use ?; EXECUTE sp_HelpDB'

--=20
Arnie Rowland, Ph.D.
Westwood Consulting, Inc

Most good judgment comes from experience.=20
Most experience comes from bad judgment.=20
- Anonymous


[quoted text, click to view]
Re: Dynamic SQL error processing question Pedja
8/1/2006 6:01:02 AM
Somehow, I prefer using cursor to undocumented stored procedures, especially
since it is a small cursor, which doesn't take a lot of memory. I think that
the error control is equally bad in both cases. sp_helpdb reads from master
database, so I don't think it's really good test if the database is available
or not.
As a response, I need to get back custom error message from the code ("if
database is available or not"), and code should go through all databases,
regardless of the error (and that is not happening with my version of the
code)...
Pedja

[quoted text, click to view]
Re: Dynamic SQL error processing question Arnie Rowland
8/1/2006 10:15:29 AM
Yes, you are correct, sp_Help pulls from master.

The use of 'sp_Help was ONLY a demonstration. I had hoped that you would be
able to extrapolate a use of sp_MSForEachDb for your needs. I was not even
attempting to create a solution for you -but to give you a tool so you could
do it yourself.

As you said, your "script stops execution and it doesn't get into error
processing part of the code". I offered you an option that would allow you
to create a script that would operate as you desired.

--
Arnie Rowland, Ph.D.
Westwood Consulting, Inc

Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous


[quoted text, click to view]

Re: Dynamic SQL error processing question Pedja
8/1/2006 11:31:02 AM
Problem with sp_MSForEachDB is that it doesn't go through all databases (i.e.
it skips offline, suspect), and I need this script exactly because of those
dbs...
Thanks anyway,
Pedja

[quoted text, click to view]
Re: Dynamic SQL error processing question Erland Sommarskog
8/2/2006 10:42:27 PM
Pedja (Pedja@discussions.microsoft.com) writes:
[quoted text, click to view]

If I understand your posts correctly, this plain SELECT should do what
you need:

SELECT name, databasepropertyex(name, 'Status')
FROM master..sysdatabases
WHERE convert(varchar(20), databasepropertyex(name, 'Status')) IN
('OFFLINE', 'SUSPECT')


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
Re: Dynamic SQL error processing question Pedja
8/5/2006 8:03:01 AM
no, but since i never heard for "databasepropertyex" before, your answer
might be very useful to me (when checking if database is in suspect status).
My question was related to error processing from dynamic sql statements: how
to continue execution of script after dynamic t-sql statement (sp_executesql)
failed and to process that error...
Thanks Erland

[quoted text, click to view]
Re: Dynamic SQL error processing question Erland Sommarskog
8/5/2006 10:41:31 PM
Pedja (Pedja@discussions.microsoft.com) writes:
[quoted text, click to view]

Generally, before venturing into system tables, it can be a good idea
to see if there is any xxxxproperty function that fills your needs. Don't
forget about Objectprorty which has information about tables, stored
procedures etc.

Not the least is this a good thing if you are on SQL 2000, since the
system tables becomes "compatibility views" in SQL 2005 and are
deprecated. So the property functions makes the transition a little
easier.

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
AddThis Social Bookmark Button