views:

337

answers:

4

I'm looking to filter the resultset of a stored procedure. What I'd like is something like the following (non-working) syntax:

IF EXISTS ( SELECT 1 FROM  (EXEC  sp_linkedservers) WHERE srv_name = 'myServer' )
    PRINT N'dropping linked servers'
GO

edit - this is just one example, I'd like a general solution if possible

A: 

try this:

-- add 'loopback' linkedserver 
if exists (select * from master..sysservers where srvname = 'loopback')
    exec sp_dropserver 'loopback'
go
exec sp_addlinkedserver @server = N'loopback',
    @srvproduct = N'',
    @provider = N'SQLOLEDB', 
    @datasrc = @@servername
go

select * into #t from openquery(loopback, 'set fmtonly on exec sp_who') 
select * from #t
drop table #t
go
Mladen Prajdic
so why exactly is this downvoted when it's the only way in sql server that you can do exactly what you want?
Mladen Prajdic
No offense, that is clever, but there are a couple reasons - openquery can be a security problem, so on many systems it's switched off; making a linked server is overkill for this problem; IMHO you are using a "side effect" of linked servers and openquery that others would not expect to see
onupdatecascade
that is completely true. i've assumed that the OP has a situation where he doesn't always know the exact output of a stored procedure. if you do know that then the declare table first and insert into from sproc is a great solution. if not then my suggestion is the only viable way.
Mladen Prajdic
+2  A: 

You could put the results of the stored procedure into a temporary table or table variable before this then query that table, applying whatever where clause to that you want.

[Edited]

Like this:

DECLARE @foo TABLE
(
SRV_NAME NVARCHAR(100),
SRV_PROVIDERNAME NVARCHAR(100),
SRV_PRODUCT NVARCHAR(100),
SRV_DATASOURCE NVARCHAR(100),
SRV_PROVIDERSTRING NVARCHAR(100),
SRV_LOCATION NVARCHAR(100),
SRV_CAT NVARCHAR(100)
)

INSERT INTO @foo
EXEC  sp_linkedservers

SELECT * FROM @foo WHERE SRV_PRODUCT = 'SQL Server'

You would, of course, change that final where clause to what you would want to filter on.

Mallioch
how do I do that?
Scott Weinstein
On SQL 2000 you'd need to use a temp table instead of a table variable
JNappi
and is there no way to do it w/o declaring the schema of the result set?
Scott Weinstein
A: 

Move the stored procedure to a table-valued user defined function. Keep the existing stored procedure, but just have it call this new function rather than duplicating the logic. Then use the function in your query.

Joel Coehoorn
It's a system stored proc. Can't change it.
onupdatecascade
A: 

Assuming you want it for the exact purpose outlined in your question, and not a general solution, then you could just query sys.servers (SQL 2005+) (or sysservers pre 2005), no need to use the sp_linkedservers stored procedure:

-- SQL 2005+
IF EXISTS ( SELECT 1 FROM sys.servers WHERE name = 'myServer' )
    PRINT N'dropping linked servers'
GO

-- SQL 2000
IF EXISTS ( SELECT 1 FROM sysservers WHERE srvname = 'myServer' )
    PRINT N'dropping linked servers'
GO
AdaTheDev