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,
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] "Pedja" <Pedja@discussions.microsoft.com> wrote in message = news:6B10D995-BCCC-4A3D-9E3E-897AAA32E4C9@microsoft.com... > Hi, > I'm trying to develope a script which would be running against the = server=20 > every 10 minutes, and whose purpose would be to check availability of = all=20 > databases on the server. > I am using cursor and dynamic sql for that (see below). At the same = time, I=20 > want to return custom error message and to write event (error) to the=20 > application log. Problem is that after the error occured (in = sp_executesql),=20 > script stops execution and it doesn't get into error processing part = of the=20 > code. Does anybody have an idea how to overcome this? Here is the = code: >=20 > declare @dbName sysname, > @sql nvarchar(255), > @err int >=20 > declare crsDatabases cursor=20 > for=20 > select name from sysdatabases > where name not in ('northwind','pubs','tempdb','model') and=20 > status & 32 !=3D 32 and -- loading > status & 128 !=3D 128 and -- recovering > status & 512 !=3D 512 and -- offline > status & 4096 !=3D 4096 and -- single user > status & 32768 !=3D32768 and -- emergency mode > status & 1073741824 !=3D1073741824 -- cleanly shutdown > for read only >=20 > open crsDatabases > fetch next from crsDatabases into @dbName=20 > while @@fetch_status=3D0 > begin > -- This part of the code calculates number of objects as a test of = database=20 > availaibility: > select @sql =3D N'select count(*) from [' + @dbName + = N'].dbo.sysobjects=20 > with(nolock)' > exec @err =3D 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 >=20 > Thanks in advance,
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] "Arnie Rowland" wrote: > 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' > > -- > Arnie Rowland, Ph.D. > Westwood Consulting, Inc > > Most good judgment comes from experience. > Most experience comes from bad judgment. > - Anonymous > > > "Pedja" <Pedja@discussions.microsoft.com> wrote in message news:6B10D995-BCCC-4A3D-9E3E-897AAA32E4C9@microsoft.com... > > 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,
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] "Pedja" <Pedja@discussions.microsoft.com> wrote in message news:AA359A0F-DE41-4AE8-959C-22F4418868ED@microsoft.com... > 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 > > "Arnie Rowland" wrote: > >> 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' >> >> -- >> Arnie Rowland, Ph.D. >> Westwood Consulting, Inc >> >> Most good judgment comes from experience. >> Most experience comes from bad judgment. >> - Anonymous >> >> >> "Pedja" <Pedja@discussions.microsoft.com> wrote in message >> news:6B10D995-BCCC-4A3D-9E3E-897AAA32E4C9@microsoft.com... >> > 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, >> > Pedja
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] "Arnie Rowland" wrote: > 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 > > > "Pedja" <Pedja@discussions.microsoft.com> wrote in message > news:AA359A0F-DE41-4AE8-959C-22F4418868ED@microsoft.com... > > 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 > > > > "Arnie Rowland" wrote: > > > >> 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' > >> > >> -- > >> Arnie Rowland, Ph.D. > >> Westwood Consulting, Inc > >> > >> Most good judgment comes from experience. > >> Most experience comes from bad judgment. > >> - Anonymous > >> > >> > >> "Pedja" <Pedja@discussions.microsoft.com> wrote in message > >> news:6B10D995-BCCC-4A3D-9E3E-897AAA32E4C9@microsoft.com... > >> > 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, > >> > Pedja > >
Pedja (Pedja@discussions.microsoft.com) writes: [quoted text, click to view] > 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...
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
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] "Erland Sommarskog" wrote: > Pedja (Pedja@discussions.microsoft.com) writes: > > 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... > > 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 > http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
Pedja (Pedja@discussions.microsoft.com) writes: [quoted text, click to view] > 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...
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
Don't see what you're looking for? Try a search.
|