Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2009-04-06
6
Medium Priority
?
1,607 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
6 Comments
 
LVL 31

Accepted Solution

by:
RiteshShah earned 1200 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 400 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 400 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
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…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

885 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