13

次のクエリは、リンク サーバーから結果セットを取得することを認識しています。

SELECT * FROM openquery(DEVMYSQL, 
    'SELECT event_id, people_id, role_id, rank, last_updated FROM event_cast')

しかし、これは挿入に関しても同じですか?結果セットをプルダウンしますか、それとも列情報を取得するだけですか?

INSERT INTO openquery(DEVMYSQL, 
     'SELECT event_id, people_id, role_id, rank, last_updated FROM event_cast')

前者の場合、これは非常に非効率的です。返される結果セットを制限する必要がありINSERTますか?

これは基本的に、 と に関してはどのように機能するかについての質問OPENQUERYです。SELECTINSERT

助けていただければ幸いです。

4

3 に答える 3

10

INSERT で何を達成しようとしているのかわかりません。

正しい構文 (REMOTE サーバーに挿入する場合) は、

INSERT into openquery(MyServer, 'dbo.event_cast') values ('','')

選択は、追加情報を提供することなく、選択クエリが返すものを(無駄に)取得する挿入を遅らせるだけです。また、openquery を使用すると、挿入に次の構文をより正確に使用できます。

INSERT into myserver.mydatabase.dbo.event_Cast values('','')

ただし、LOCAL サーバーに挿入しようとしている場合は、select によって取得された値を次の構文で取得する必要があります。

INSERT into dbo.my_localtable SELECT * FROM openquery(DEVMYSQL, 'SELECT event_id, people_id, role_id, rank, last_updated FROM event_cast')

はい、文は列情報だけでなく値も挿入します。

テーブルをローカルに複製するだけの場合は、簡単です

SELECT top 1 * into new_local_event_cast FROM openquery(DEVMYSQL, 'SELECT event_id, people_id, role_id, rank, last_updated FROM event_cast');
TRUNCATE TABLE new_local_event_cast;

十分であろう

于 2013-03-07T20:59:00.033 に答える
4

Where the SELECT will return records, the INSERT will not return a result set except for the count of records affected. This can be suppressed by using SET NOCOUNT ON; however, I am not sure if suppression would refer to visibility or the row count actually coming over.

    INSERT INTO OPENQUERY(MYSERVER, 'SELECT [Drive_Letter] ,[MBFree],[Server] FROM [Blah].[dbo].[SQL_Drives]')
    SELECT 'X', 2, 'MyServer'

(1 row(s) affected)

As for records being returned from the INSERT, the only way to make that happen is to use a OUTPUT clause. The client machine will not have access to the OUTPUT INSERTED rows so those cannot be returned. If you try to run the following, you will receive an error:

INSERT INTO OPENQUERY(MYSERVER, 'SELECT [Drive_Letter] ,[MBFree],[Server] FROM [BLAH].[dbo].[SQL_Drives]')
OUTPUT INSERTED.*
SELECT 'X', 2, 'MyServer'

Msg 405, Level 16, State 1, Line 1
A remote table cannot be used as a DML target in a statement which includes an OUTPUT clause or a nested DML statement.


 -- EDIT RESULTS OF PROFILER ---------------------------      


-- Statements that occured on server called through OPENQUERY
    exec sp_cursoropen @p1 output,N'SELECT [Drive_Letter] ,[MBFree],[Server] FROM [MyServer].[dbo].[SQL_Drives]',@p3 output,@p4 output,@p5 output
    select @p1, @p3, @p4, @p5

    exec sp_cursor 180150009,4,0,N'[MyServer].[dbo].[SQL_Drives]',@Drive_Letter='X',@MBFree=2,@Server='MyServer'


--Statements that occured on client
    INSERT INTO OPENQUERY(MyServer, 'SELECT [Drive_Letter] ,[MBFree],[Server] FROM [MyServer].[dbo].[SQL_Drives]')
    SELECT 'X', 2, 'MyServer'
于 2013-03-07T00:14:12.660 に答える