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 @tes开发者_运维百科t
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!
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.
精彩评论