是否可以在 OPENQUERY 中执行“使用”语句?
这有效:
SELECT *
FROM OPENQUERY(
server_name,
'select * from AdventureWorks.Person.Address'
)
这不会:
SELECT *
FROM OPENQUERY(
server_name,
'use AdventureWorks; select * from Person.Address'
)
是否可以在 OPENQUERY 中执行“使用”语句?
这有效:
SELECT *
FROM OPENQUERY(
server_name,
'select * from AdventureWorks.Person.Address'
)
这不会:
SELECT *
FROM OPENQUERY(
server_name,
'use AdventureWorks; select * from Person.Address'
)
试试这个——
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