You could create a system to run these stored procedures. I've created some sample code here that I tested quickly.
/*
This table is the same on every server, and contains the source Stored Proc with
variables such as [LinkedServer].[LinkedDatabase].[LinkedSchema].[LinkedTable]
that will be replaced by the RunProc stored proc below.
*/
CREATE TABLE DistributedProcs
(
DistributedProcID INT NOT NULL PRIMARY KEY CLUSTERED CONSTRAINT
PK_DistributedProcs IDENTITY(1,1)
, ProcName nvarchar(255)
, ProcText nvarchar(max)
);
/*
This is a test stored proc that can be ran against any linked server.
*/
INSERT INTO DistributedProcs (ProcName, ProcText)
VALUES ('TestProc',
'SELECT * from [LinkedServer].[LinkedDatabase].[LinkedSchema].[LinkedTable]');
/*
This table contains replacements for
[LinkedServer].[LinkedDatabase].[LinkedSchema].[LinkedTable] for
each stored procedure in DistributedProcs
*/
CREATE TABLE DistributedProcVars
(
DistributedProcVarsID INT NOT NULL PRIMARY KEY CLUSTERED
CONSTRAINT PK_DistributedProcVars IDENTITY(1,1)
, DistributedProcID INT
, LinkedServer nvarchar(255)
, LinkedDatabase nvarchar(255)
, LinkedSchema nvarchar(255)
, LinkedTable nvarchar(255)
);
INSERT INTO DistributedProcVars (DistributedProcID, LinkedServer,
LinkedDatabase, LinkedSchema, LinkedTable)
VALUES (1, 'MyServer','MyDatabase','sys','tables');
GO
/*
This stored procedure runs the actual stored procedure contained
in DistributedProcs
*/
CREATE PROCEDURE RunProc
(
@ProcNameToRun nvarchar(255)
)
AS
BEGIN
DECLARE @ProcID INT;
DECLARE @LinkedServer nvarchar(255);
DECLARE @LinkedDatabase nvarchar(255);
DECLARE @LinkedSchema nvarchar(255);
DECLARE @LinkedTable nvarchar(255);
DECLARE @ProcText nvarchar(max);
SELECT @LinkedServer = [LinkedServer]
, @LinkedDatabase = [LinkedDatabase]
, @LinkedSchema = [LinkedSchema]
, @LinkedTable = [LinkedTable]
, @ProcText = [ProcText]
FROM DistributedProcs DP
INNER JOIN DistributedProcVars DPV ON
DP.DistributedProcID = DPV.DistributedProcID
WHERE DP.ProcName = @ProcNameToRun;
SET @ProcText = REPLACE(@ProcText, '[LinkedServer]',@LinkedServer);
SET @ProcText = REPLACE(@ProcText, '[LinkedDatabase]',@LinkedDatabase);
SET @ProcText = REPLACE(@ProcText, '[LinkedSchema]',@LinkedSchema);
SET @ProcText = REPLACE(@ProcText, '[LinkedTable]',@LinkedTable);
EXEC sp_executesql @ProcText;
END
GO
SELECT * FROM DistributedProcs;
SELECT * FROM DistributedProcVars;
EXEC RunProc 'TestProc';
This creates a table called DistributedProcs that contains the text of the stored procedures that you want to run on multiple servers. You'd create this table on each server, along with DistributedProcVars to store the names of the servers/databases/schemas/tables for each stored proc.
Then instead of running the actual stored proc, you run RunProc with the ProcName of the stored procedure you want executed.
Not perfect, but does what you need, I believe.
SELECT <fields> FROM <linked server>but use the same view name across all servers to keep code maintained – JNK♦ Oct 10 '12 at 14:38