3

是否可以在 OPENQUERY 中执行“使用”语句?

这有效:

SELECT *
FROM OPENQUERY(
    server_name, 
    'select * from AdventureWorks.Person.Address'
)

这不会:

SELECT *
FROM OPENQUERY(
    server_name, 
    'use AdventureWorks; select * from Person.Address'
)
4

1 回答 1

0

试试这个——

DECLARE 
      @SQL NVARCHAR(500)
    , @ServerName NVARCHAR(100) = 'server_name'
    , @DbName NVARCHAR(100) = DB_NAME() -- current db_name

SELECT @SQL = '
SELECT *
FROM OPENQUERY(
    '+ @ServerName + ', 
    ''SELECT * FROM [' + @DbName + '].Person.Address''
)'

PRINT @SQL
EXEC sys.sp_executesql @SQL
于 2013-08-01T06:16:06.417 回答