Help for Linked server code

Kenny Gua 431 Reputation points
2021-05-26T03:10:30.69+00:00

Hi, I am unable to run the following query from server:cd_pdserv02.DBTBB_BACK and getting the linked server error. How I can grant the access of linked server to run the query. OR is there anyway to run the query successfully. Thanks for your help

Select * from [cd_pdserv01].DBTRB_BACK.dbo.[rule]
--Could not find server 'cd_pdserv01' in sys.servers.

Can I run the above query with OPENDATASOURCE? How I can run with OPENDATASOURCE?

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,492 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,707 questions
{count} votes

Accepted answer
  1. AmeliaGu-MSFT 14,006 Reputation points Microsoft External Staff
    2021-05-26T07:35:41.793+00:00

    Hi @Kenny Gua ,
    Please check if your linked server exists using this query:

    SELECT * FROM sys.servers  
    

    If it not exists, we need to add a new linked server, for example:

    EXEC master.dbo.sp_addlinkedserver @server = N'servername', @srvproduct=N'SQL Server'  
    Go  
    EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname = N'servername', @locallogin = NULL , @useself = N'False', @rmtuser = N'remoteuser', @rmtpassword = N'password'  
    GO  
    

    Please refer to this article which might help.
    In addition, we also can use OPENDATASOURCE to create an ad hoc connection to another server, and Ad Hoc Distributed Queries option should be enabled in order to open a connection to a remote server,
    for example:

    EXEC sp_configure 'show advanced options', 1  
    RECONFIGURE WITH OVERRIDE  
    EXEC sp_configure 'Ad Hoc Distributed Queries', 1  
    RECONFIGURE WITH OVERRIDE  
    Go  
    SELECT *    
    FROM OPENDATASOURCE('SQLNCLI',    
        'Data Source=servername; Catalog=databasename;User ID=SQLLogin;Password=userpassword;')    
    .DBTRB_BACK.dbo.[rule];  
    

    Best Regards,
    Amelia


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.