Querying a linked sql server

后端 未结 7 1781
醉话见心
醉话见心 2020-12-08 20:08

I added a linked server, which is showing in the linked server list, but when I query it, it throws an error with the db server name.

EXEC sp_helpserver
EXE         


        
相关标签:
7条回答
  • 2020-12-08 20:33

    try Select * from openquery("aa-db-dev01",'Select * from users') ,the database connection should be defined in he linked server configuration

    0 讨论(0)
  • 2020-12-08 20:44

    You need to remove the quote marks from around the name of the linked server. It should be like this:

    Select * from openquery(aa-db-dev01,'Select * from TestDB.dbo.users')

    0 讨论(0)
  • 2020-12-08 20:47

    You can use:

    SELECT * FROM [aa-db-dev01].[TestDB].[dbo].[users];
    
    0 讨论(0)
  • 2020-12-08 20:47

    The accepted answer works for me.

    Also, in MSSQLMS, you can browse the tree in the Object Explorer to the table you want to query.

    [Server] -> Server Objects -> Linked Servers -> [Linked server] -> Catalogs -> [Database] -> [table]

    then Right click, Script Table as, SELECT To, New Query Window

    And the query will be generated for you with the right FROM, which you can use in your JOIN

    0 讨论(0)
  • 2020-12-08 20:54

    I use open query to perform this task like so:

    select top 1 *
    INTO [DATABASE_TO_INSERT_INTO].[dbo].[TABLE_TO_SELECT_INTO]
    from openquery(
        [LINKED_SERVER_NAME],
        'select * from [DATABASE_ON_LINKED_SERVER].[dbo].[TABLE_TO_SELECT_FROM]'
    )
    

    The example above uses open query to select data from a database on a linked server into a database of your choosing.

    Note: For completeness of reference, you may perform a simple select like so:

    select top 1 * from openquery(
        [LINKED_SERVER_NAME],
        'select * from [DATABASE_ON_LINKED_SERVER].[dbo].[TABLE_TO_SELECT_FROM]'
    )
    
    0 讨论(0)
  • 2020-12-08 20:54

    If linked server name is IP address following code is true:

    select * from [1.2.3.4,1433\MSSQLSERVER].test.dbo.Table1
    

    It's just, note [] around IP address section.

    0 讨论(0)
提交回复
热议问题