2

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!

4
  • Your middle version works for me in a simple test case. What is the exact problem you're experiencing with it? Commented Nov 4, 2010 at 20:57
  • It says that a distributed transaction isn't supported by the server. I probably should mention that the linked server isn't another SQL Server but an OLE DB. Commented Nov 4, 2010 at 21:07
  • But it is good to know, at least that what I am trying to do probably just isn't possible with an OLE db since the syntax is valid. Commented Nov 4, 2010 at 21:16
  • @user468341: That is a horse of a different color then. Commented Nov 4, 2010 at 21:28

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.