Possible to use “INSERT INTO @VARIABLE EXEC() AT LINKED_SERVER” syntax? (SQL Server 2005)

StackOverflow https://stackoverflow.com/questions/4101209

Pergunta

I am trying to execute a query on a linked server, but I need the results locally.

DECLARE @test TABLE
(
greeting CHAR(5)
)

INSERT INTO @test
EXEC('select ''hello'' as greeting')

SELECT * FROM @test

Uses an EXEC() and INSERT INTO but, obviously the query is executing locally.

DECLARE @test TABLE
(
greeting CHAR(5)
)

INSERT INTO @test
EXEC('select ''hello'' as greeting') AT LINKED_SERVER

SELECT * FROM @test

Does not work at all.

SELECT greeting FROM OpenQuery(LINKED_SERVER,'SELECT''hello'' AS greeting')

Accomplishes exactly what I want, but I need to be using a dynamic string, and the only way to make that work is to make my entire query a huge string and put it into an EXEC(), which I don't want to do since it is really ugly....

Thanks for any help!

Foi útil?

Solução

You could use the following:

INSERT INTO @test
EXEC('select TOP 1 ''hello'' as greeting FROM LINKED_SERVER.SomeDB.dbo.SysObjects')

And assuming you don't want to actually just get the word "hello"... you probably want to do something like actually selecting a value from a table, then the above should be even more likely what you want.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top