Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Linked Server

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2003-05-15 : 07:19:08
coUgar writes "How to programmatically determine the existence of a linked server availability. OR how to determine the non-availability of the Linked server link / machine / resource trying to be accessed from within a Stored Procedure (which will be called by the user on some event).

The main issue here is the the local system administrator does not want to be responsible for any errors generated on an external machine , which is set up as a linked server, and does not want his service to crash due to an external server.

I want to determine from within a SProc the availability of the external "resource" (as my administrator puts it ) and handle the errors if any.

What i am looking for is an analogy of the On Error Resume next stmt of ASP in SQL."

nr
SQLTeam MVY

12543 Posts

Posted - 2003-05-15 : 11:30:08
If you do this you will have to do all your queries in dynamic sql.
And how will you know if the network is going to have problems half way through a query.

All queries should have error handling but the error may not be trappable (and the m/c may crash) so you will need to cater for these situations.
That should cover the linked servers not being available.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

jasper_smith
SQL Server MVP & SQLTeam MVY

846 Posts

Posted - 2003-05-15 : 15:01:41
To check if a remote server is available you can use something like this
 
CREATE PROCEDURE sp_serverup
(
@server sysname,
@islinked char(1)='N',
@up int OUTPUT
)
AS
/*
1 = Running
-1 = Down
*/
SET NOCOUNT ON
DECLARE @hr int
DECLARE @sql int
DECLARE @cnt int ; SET @cnt = 0
DECLARE @server_resolved sysname
DECLARE @status int ; SET @status = 0

IF @islinked='Y'
BEGIN

SELECT @cnt=COUNT(*)
FROM master..sysservers
WHERE srvname=@server
AND isremote=1

IF @cnt<>1
BEGIN
RAISERROR('Linked server name cannot be resolved',16,1)
RETURN(1)
END

SELECT @server_resolved = datasource
FROM master..sysservers
WHERE srvname=@server
AND isremote=1

END
ELSE
BEGIN

SELECT @server_resolved = @server

END

EXEC @hr = sp_OACreate 'SQLDMO.SQLServer', @sql OUTPUT
IF @hr<>0 EXEC sp_OAGetErrorInfo @sql

EXEC @hr = sp_OASetProperty @sql ,'LoginSecure','True'
EXEC @hr = sp_OASetProperty @sql ,'LoginTimeout',10
EXEC @hr = sp_OAMethod @sql,'Connect',null,@server_resolved
IF @hr<>0 EXEC sp_OAGetErrorInfo @sql

EXEC @hr = sp_OAGetProperty @sql ,'Status',@status OUTPUT
EXEC @hr = sp_OAMethod @sql,'DisConnect',null
EXEC @hr=sp_OADestroy @sql

SELECT @up = CASE WHEN @status = 1
THEN 1 ELSE -1 END

RETURN



HTH
Jasper Smith

0x73656c6563742027546f6f206d7563682074696d65206f6e20796f75722068616e6473203f27
Go to Top of Page
   

- Advertisement -