dbTalk Databases Forums  

How to know is a sql server is not available?

microsoft.public.sqlserver.dts microsoft.public.sqlserver.dts


Discuss How to know is a sql server is not available? in the microsoft.public.sqlserver.dts forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Don Juan
 
Posts: n/a

Default How to know is a sql server is not available? - 10-02-2009 , 05:05 AM






Hi I need to create a SP with the following fake code:
(Sql Server 2005)
....
If DB1 is available and DB2 is available
Insert into DB3
Select field1, field2 from DB1

Insert into DB3
Select field1, field2 from DB2
Where status = 1
Else
Insert into DB3
Select 'One of databases is not available','0'
End

What I need is to know if a database is Restoring.. or Offline...

Thanks in advanced

Reply With Quote
  #2  
Old   
Dan Guzman
 
Posts: n/a

Default Re: How to know is a sql server is not available? - 10-02-2009 , 05:27 AM






Quote:
What I need is to know if a database is Restoring.. or Offline...
Check sys.databases for the desired database database states (online,
read_write and multi-user). You'll also need to execute the conditional
code using dynamic SQL to avoid errors if the databases are not available:

IF (SELECT COUNT(*)
FROM sys.databases
WHERE state = 0 --online
AND is_read_only = 0
AND user_access = 0 --multi-user
AND name IN(N'DB1',N'DB2')) = 2
BEGIN
EXEC sp_executesql N'
Insert into DB3...
Select field1, field2 from DB1

Insert into DB3...
Select field1, field2 from DB2
Where status = 1
'
END
ELSE
BEGIN
EXEC sp_executesql N'
Insert into DB3...
Select ''One of databases is not available'',''0''
';
END

--
Hope this helps.

Dan Guzman
SQL Server MVP
http://weblogs.sqlteam.com/dang/


"Don Juan" <Juanete (AT) gmail (DOT) com> wrote

Quote:
Hi I need to create a SP with the following fake code:
(Sql Server 2005)
...
If DB1 is available and DB2 is available
Insert into DB3
Select field1, field2 from DB1

Insert into DB3
Select field1, field2 from DB2
Where status = 1
Else
Insert into DB3
Select 'One of databases is not available','0'
End

What I need is to know if a database is Restoring.. or Offline...
Thanks in advanced

Reply With Quote
  #3  
Old   
Dooza
 
Posts: n/a

Default Re: How to know is a sql server is not available? - 10-02-2009 , 05:39 AM



Don Juan wrote:
Quote:
Hi I need to create a SP with the following fake code:
(Sql Server 2005)
...
If DB1 is available and DB2 is available
Insert into DB3
Select field1, field2 from DB1

Insert into DB3
Select field1, field2 from DB2
Where status = 1
Else
Insert into DB3
Select 'One of databases is not available','0'
End

What I need is to know if a database is Restoring.. or Offline...
Thanks in advanced
There is this useful SP that can check remote or linked servers:
http://www.sqldbatips.com/showcode.asp?ID=38

Steve

Reply With Quote
  #4  
Old   
Damien
 
Posts: n/a

Default Re: How to know is a sql server is not available? - 10-02-2009 , 09:02 AM



On Oct 2, 11:05*am, "Don Juan" <Juan... (AT) gmail (DOT) com> wrote:
Quote:
Hi I need to create a SP with the following fake code:
(Sql Server 2005)
...
If DB1 is available and DB2 is available
* * Insert into DB3
* * Select field1, field2 from DB1

* * Insert into DB3
* * Select field1, field2 from DB2
* * Where status = 1
Else
* * Insert into DB3
* * Select 'One of databases is not available','0'
End

What I need is to know if a database is Restoring.. or Offline...

Thanks in advanced
You might do better to wrap the two inserts in a TRY/CATCH and a
transaction - what if the databases disappear between the time the
check runs and the time the insert runs?

Damien

Reply With Quote
  #5  
Old   
Don Juan
 
Posts: n/a

Default Re: How to know is a sql server is not available? - 10-05-2009 , 06:46 AM



Thanks!

"Dooza" <steveNO (AT) SPAM (DOT) dooza.tv> wrote

Quote:
Don Juan wrote:
Hi I need to create a SP with the following fake code:
(Sql Server 2005)
...
If DB1 is available and DB2 is available
Insert into DB3
Select field1, field2 from DB1

Insert into DB3
Select field1, field2 from DB2
Where status = 1
Else
Insert into DB3
Select 'One of databases is not available','0'
End

What I need is to know if a database is Restoring.. or Offline...
Thanks in advanced

There is this useful SP that can check remote or linked servers:
http://www.sqldbatips.com/showcode.asp?ID=38

Steve

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.