Solved

How to get retrieve data from another database on another computer using stored procedures?

Posted on 2009-04-06
6
1,601 Views
Last Modified: 2012-05-06
Let's say that the IP address of my computer is 192.168.3.109.

 In my Microsoft SQL Server Management Studio Express, I have 2 databases:
 - DB_ONE
 - DB_TWO

I have created the stored procedure below in the database called DB_ONE. It will retrieve all records from the table called "MY_TABLE" in the "DB_TWO" database. So far this works fine.

But now I have another computer with IP address of 192.168.3.110 and in its Microsoft SQL Server Management Studio Express there's a database called DB_THREE, and there's a table called HIS_TABLE.

How do I modify the stored procedure below (that's running on 192.168.3.109) to retrieve all records from "HIS_TABLE" in "DB_THREE" (which is running on 192.168.3.110)?

Is this even possible in the first place? Thanks.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
 
CREATE PROCEDURE sp_test
AS
BEGIN
	SET NOCOUNT ON;
	select * from DB_TWO.dbo.MY_TABLE
END
GO

Open in new window

0
Comment
Question by:killdurst
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 31

Accepted Solution

by:
RiteshShah earned 300 total points
ID: 24075752
you can set your new computer's database as linked database and use it.
0
 
LVL 12

Assisted Solution

by:udaya kumar laligondla
udaya kumar laligondla earned 100 total points
ID: 24075754
in the following code change the  ServerName.DBName.dbo.TableName as you require and the SP will be able to fetech data from any server
 
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
 
CREATE PROCEDURE sp_test
AS
BEGIN
	SET NOCOUNT ON;
	select * from  ServerName.DBName.dbo.TableName
END

Open in new window

0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24075758
0
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
LVL 31

Expert Comment

by:RiteshShah
ID: 24075773
the way udayakumarlm said, it also ok but you have to have full rights over another sql server or have to have certificates of each other installed on each server.

in short, if you are trying to access database in 192.168.3.110 from 192.168.3.109 and you have logged in SQL Server with SA user in 192.168.3.109 than that user should be able to connect to 192.168.3.110 as well. this is true for both SQL authentication as well as windows authentication.
0
 
LVL 1

Author Comment

by:killdurst
ID: 24075778
Man, you guys are fast. I'm gonna try these out and update you soon.
0
 
LVL 3

Assisted Solution

by:sateeshcv
sateeshcv earned 100 total points
ID: 24077196
--GETTING ROWS FROM ANOTHER COMPUTER BY CREATING LINKED SERVER
--creating linked server for remoting accessing
EXEC SP_ADDLINKEDSERVER
      @SERVER='ANYNAME',
      @SRVPRODUCT='',      
      @DATASRC='SERVERNAME',      
      @PROVIDER='SQLNCLI';
      --OPTIONAL      @PROVSTR='Initial Catalog=DB_THREE;'

--CREATING REMOTE SERVERLOGIN FOR SERVER WITH USERNAME AND PASSWORD    
EXEC SP_ADDLINKEDSRVLOGIN 'ANYNAME', 'false', null, 'sa', 'PASSWORD'

CREATE PROCEDURE sp_test
AS
BEGIN
      SET NOCOUNT ON;
      select * from ANYNAME.DB_THREE.dbo.HIS_TABLE;
END
GO

NOTE: BEFORE DOING PLEASE GET THE SERVER NAME FOR THE COMPUTER: 192.168.3.110 AND REPLACE THE NAME IN THE @DATASRC VARIABLE.
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question