JetJack
JetJack

Reputation: 988

Linked Server query is not working properly

How to create a linked Server Query

Server1 name is Server, Table name is Employee Server2 name is RAPAN, Table name is Venus

I want to select a venus table (server2) through server1.

How to do this through query

Tried Query

EXEC sp_addlinkedserver 
   @server = 'server', 
   @srvproduct = '',
   @provider = 'SQLOLEDB', 
   @datasrc = 'RAPAN'
GO

Query Executed Successfully, But when it try to select * from venus through server1, it's giving error as invalid object name

What wrong in my query...

Upvotes: 1

Views: 758

Answers (1)

Oleg Dok
Oleg Dok

Reputation: 21766

  • You should add login mapping to this server
  • You should query with full naming convention like SELECT * FROM server1.RAPAN.dbo.venus

To add linked server use (and play with options on your needs):

EXEC sys.sp_addlinkedserver @server = 'RAPAN', @srvproduct = 'SQL Server'

Set optional options

EXEC sys.sp_serveroption @server = 'RAPAN', @optname = 'collation compatible', @optvalue = 'On'
EXEC sys.sp_serveroption @server = 'RAPAN', @optname = 'data access', @optvalue = 'On'
EXEC sys.sp_serveroption @server = 'RAPAN', @optname = 'lazy schema validation', @optvalue = 'On' 
EXEC sys.sp_serveroption @server = 'RAPAN', @optname = 'rpc', @optvalue = 'On'
EXEC sys.sp_serveroption @server = 'RAPAN', @optname = 'rpc out', @optvalue = 'On'

Add login (And read more)

EXEC sys.sp_addlinkedsrvlogin @rmtsrvname = 'RAPAN'

Upvotes: 1

Related Questions