0

I am trying to write a SQL script that will pull up information on where certain data is stored (i.e. which server and DB it is in). I have the part where it is pulling up the information and putting it into a separate DB on the same server, but much of the data that I'm wanting to collect is on other servers. Is there a way in the same script to connect to several different DB's on several different servers to get the data that I want. Essentially I'm trying to get Names, emails and phone numbers for people in our various DB's. Here is what I have so far:

if OBJECT_ID('tempdb..#TrainingSessionInfo') is not null
    drop table #TrainingSessionInfo

if OBJECT_ID('PSBackup.dbo.InvestigoUniversitySessions','U') is not null
    drop table PSBackup.dbo.InvestigoUniversitySessions

declare @StartDate date = null
        ,@EndDate date = null

set @StartDate = '1/1/2014'
set @EndDate = '6/17/2014'
--exec spGetTrainingSessionInfo @StartDate,@EndDate

create table #TrainingSessionInfo(cntID int,datDate datetime,strDesc varchar(255),strFirmID varchar(255),strTrainer varchar(255)
        ,meetingID varchar(255),Attendees int)
insert into #TrainingSessionInfo
exec spGetTrainingSessionInfo @StartDate,@EndDate

create table PSBackup.dbo.InvestigoUniversitySessions (lngSessionID int
        ,strUserID int
        ,strFirmID varchar(255)
        ,cntIF int
        ,strDSNServer varchar(255))
insert into PSBackup.dbo.InvestigoUniversitySessions (lngSessionID,strUserID,strFirmID,cntIF,strDSNServer)
select lngSessionID
        ,strUserID
        ,strFirmID
        ,cntID
        ,s.strDSNServer
from tblTrainingSessionSignUp tss
join Sites s on s.FirmID = tss.strFirmID
where tss.lngSessionID in(select cntID from #TrainingSessionInfo)

select * from PSBackup.dbo.InvestigoUniversitySessions order by lngSessionID desc,strFirmID,strUserID desc
1
  • in short yes you can. you need to set up linked servers and use the format [server].[database].[schema].[table]. there is probably a better solution to your problem since it sounds like you have multiple storage devices and maintaining this type of script will get frustrating for you or anyone after you. Commented Jun 18, 2014 at 13:54

1 Answer 1

1

you can use a linked server object.
the linked server objects on the single central sql server machine you are working on must be configured in advance but once they are properly set you can access remote data with the usual syntax:

select top 10 * from [linked server name].[remote database name].[schema].[table]

no change is required on the remote servers.

Sign up to request clarification or add additional context in comments.

7 Comments

The servers are already in place, I can't change them. I can only access them.
i don't get your comment... why change your servers?
You said that they have to be configured. I don't have access privileges that will allow me to make any changes to the servers.
... please find in my edit the explanation of what a linked server is.
Okay, this I think I can do. I do have another question though. Once this link is created how do you remove it? It doesn't specify in the link you sent.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.