Querying a linked sql server

后端 未结 7 1815
醉话见心
醉话见心 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: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]'
    )
    

提交回复
热议问题