Wednesday, February 15, 2012

Error handling in sql server

Hi,
I have a job looping through a table containing names of databases. Based
on some criterias I want to detach some of the databases.
So far all is good.
If for some reason the detach fails, the job fails and quits.
I would like to know if I in any way can get the job to continue the loop
without aborting the job?
Today the log can look like:
Detached database p2419983 [SQLSTATE 01000] - detached ok
Msg 15010, Sev 16: The database 'p2489947' does not exist. Use sp_helpdb
to show available databases. [SQLSTATE 42000] - failed to detach
I am aware that I should solve the real problem here (database does not
exist), but I would like to know anyway...
Any help will be appreciated.
Thanks,Hi Gurba,
Some really good article about that can be found here:
http://www.sommarskog.se/error-handling-I.html
HTH, jens Suessmeyer.|||If a function does not exist, then the db_id() function will return NULL.
For example:
if db_id(@.dbname) is not null
begin
. . .
. . .
end
"Gurba" <gurbao@.hotmail.com> wrote in message
news:Xns97097A4DAA9F6gurbaohotmailcom@.12
9.250.171.65...
> Hi,
> I have a job looping through a table containing names of databases. Based
> on some criterias I want to detach some of the databases.
> So far all is good.
> If for some reason the detach fails, the job fails and quits.
> I would like to know if I in any way can get the job to continue the loop
> without aborting the job?
> Today the log can look like:
> Detached database p2419983 [SQLSTATE 01000] - detached ok
> Msg 15010, Sev 16: The database 'p2489947' does not exist. Use sp_helpdb
> to show available databases. [SQLSTATE 42000] - failed to detach
> I am aware that I should solve the real problem here (database does not
> exist), but I would like to know anyway...
> Any help will be appreciated.
> Thanks,

No comments:

Post a Comment