Solved

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

Posted on 2009-04-06
6
1,586 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 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:udayakumarlm
udayakumarlm 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

705 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now