Nate Garvey


In our system, we need to execute queries using linked servers. However, the linked server name is not well known, it is a value stored in the system. Is there a way this can be done

CREATE FUNCTION dbo.stfGetSupervisorOrgUnitAssignments(@SupervisorID INT)

RETURNS @ReturnTable TABLE(Org_Unit INT PRIMARY KEY)

AS

BEGIN

DECLARE @Server VARCHAR(100)

SELECT @Server = dbo.stfGetLinkedServerName()

INSERT INTO @ReturnTable(Org_Unit)

SELECT ReplicationID FROM OPENQUERY(@Server, '....')

END

Any help would be greatly appreciated.




Re: Dynamically determine Linked Servers

Arnie Rowland


Linked servers are listed in the sys.servers table (or sysservers in SQL 2000).







Re: Dynamically determine Linked Servers

Nate Garvey

Ok, so once I look it up, how can I use it It appears openquery () and Execute() at Linked Server both require a hard coded server name. Neither allows for a parameter or dynamically passing a server.






Re: Dynamically determine Linked Servers

Arnie Rowland

That's correct.

Have you attempted using sp_executeql -after first having created the string to execute






Re: Dynamically determine Linked Servers

Nate Garvey

That helped. Thanks. One more question on this. Is there away to use the result set from the query

SET @SQLString = N'SELECT ReplicationID FROM OPENQUERY(' + @Server + ',

Select SupervisorOrgAssignment.SupervisorID, OrgUnit2.ReplicationID

from

SupervisorOrgAssignment

join OrganizationUnit OrgUnit1 on SupervisorOrgAssignment.organizationunitID=OrgUnit1.ID

join organizationHierarchy on OrgUnit1.ID =

case orgunit1.OrganizationLevelID

when 1 then organizationHierarchy.organizationunitID1

when 2 then OrganizationHierarchy.organizationunitID2

when 3 then organizationHierarchy.organizationunitID3

when 4 then organizationHierarchy.organizationunitID4

when 5 then organizationHierarchy.organizationunitID5

when 6 then organizationHierarchy.organizationunitID6

when 7 then organizationHierarchy.organizationunitID7

when 8 then organizationHierarchy.organizationunitID8

when 9 then organizationHierarchy.organizationunitID9

END

join OrganizationUnit OrgUnit2 on OrganizationHierarchy.organizationunitID=OrgUnit2.ID)

WHERE

SupervisorID = @SupervisorIDParam

AND ReplicationID IS NOT NULL';

SET @ParmDefinition = N'@SupervisorIDParam int';

INSERT INTO @ReturnTable

EXECUTE sp_executesql @SQLString,

@ParmDefinition,

@SupervisorIDParam = @SupervisorID

This statement is being called in a function that returns a table





Re: Dynamically determine Linked Servers

PascoDan

I run into this problem all the time.

You need to create a table (temp is fine) before you run your dynamic SQL. Then alter/use your dynamic SQL to populate the table. Then return the table in your function.

HTH

Dan





Re: Dynamically determine Linked Servers

Arnie Rowland

You can EITHER have the sp_executesql insert data into a #Temp table, (or a actual table) -but you cannot use a @Table variable.

The sp_executesql executes in a seperate process and does not have acces to any variable in the calling process. You can pass paramters in and have them populated. Refer to Books Online, Topic: "Using sp_executesql"