2017-10-05 95 views
0

我有幾張表,我想在使用OPENQUERY的鏈接服務器中創建。如何使用OPENQUERY在鏈接表中創建表?

基本上,我有一個查詢填充鏈接服務器(ServerB)上運行的表(TableA)。我正致力於從其他服務器(ServerB)作爲鏈接服務器運行此查詢的方式。

所以我有ServerA在那裏我試圖運行這個查詢,但查詢結果太長,所以我不得不縮短它,並分解成約8個不同的查詢。爲此,爲了工作,我需要將每個分解查詢的結果放入表格中(表格1,表格2等)。完成後,我將刪除這些表格,因爲只有當此查詢從ServerA運行時才需要它們。

我環顧四周互聯網試圖找到工作我試過的方式:

DECLARE @table NVARCHAR(MAX), @sql NVARCHAR(MAX); 
DECLARE @LinkedServerName nvarchar(100) 
DECLARE @LinkedDbName nvarchar(100) 
SET @LinkedServerName = 'SQLINFWWDVP07' 
SET @LinkedDbName = 'Varasset' 
SET @table = N'CREATE TABLE dbo.WOResults(id INT NULL);'; 
SET @sql = N'EXEC ' + QUOTENAME(@LinkedServerName) + N'.' 
    + QUOTENAME(@LinkedDbName) + N'.sys.sp_executesql @table;'; 
EXEC sys.sp_executesql @sql, N'@table NVARCHAR(MAX)', @table; 

這給我的錯誤:

Server 'SQLINFWWDVP07' is not configured for RPC.

我已經試過這樣:

SELECT * FROM OPENQUERY(SQLINFWWDVP07,' 
exec (''create table [ext].[WOResults]([Id] uniqueidentifier,[AltWorkOrderId] nvarchar(50),[WORK ORDER STATUS] nvarchar(30),[DESCRIPTION] nvarchar(200), 
    [STATE] uniqueidentifier,[PROJ APVL] nvarchar(30),[EST COMP] nvarchar(30),[ACT COMP] nvarchar(30),[cd_M6OrderNo] nvarchar(50),[CDD Date] nvarchar(30), 
    [FinancialProject] uniqueidentifier,[AssignedLocation] uniqueidentifier,[PROJ TYPE] uniqueidentifier,[CLASS OF PLANT] uniqueidentifier, 
    [Justification Code] uniqueidentifier,[CLOSING NOTES] nvarchar(MAX),[READY FOR SERVICE EST] nvarchar(30),[READY FOR SERVICE ACT] nvarchar(30), 
    [EOJ] nvarchar(30),[AUDIT PICK] nvarchar(30),[AUDIT COMP] nvarchar(30),[AUDIT STATUS] nvarchar(100),[FullyReceivedDate] nvarchar(30), 
    [% MTL RECEIVED] decimal(18,5),[ASSIGNED USER] nvarchar(200),[WORK ORDER NOTES] nvarchar(MAX));'') ') 

這給我的錯誤:

Cannot process the object " (Then the whole of the query) The OLE DB provider "SQLNCLI11" for linked server "SQLINFWWDVP07" indicates that either the object has no columns or the current user does not have permissions on that object.

所以我會再試試這個:

SELECT * FROM OPENQUERY(SQLINFWWDVP07,' 
exec (''create table [ext].[WOResults]([Id] uniqueidentifier,[AltWorkOrderId] nvarchar(50),[WORK ORDER STATUS] nvarchar(30),[DESCRIPTION] nvarchar(200), 
    [STATE] uniqueidentifier,[PROJ APVL] nvarchar(30),[EST COMP] nvarchar(30),[ACT COMP] nvarchar(30),[cd_M6OrderNo] nvarchar(50),[CDD Date] nvarchar(30), 
    [FinancialProject] uniqueidentifier,[AssignedLocation] uniqueidentifier,[PROJ TYPE] uniqueidentifier,[CLASS OF PLANT] uniqueidentifier, 
    [Justification Code] uniqueidentifier,[CLOSING NOTES] nvarchar(MAX),[READY FOR SERVICE EST] nvarchar(30),[READY FOR SERVICE ACT] nvarchar(30), 
    [EOJ] nvarchar(30),[AUDIT PICK] nvarchar(30),[AUDIT COMP] nvarchar(30),[AUDIT STATUS] nvarchar(100),[FullyReceivedDate] nvarchar(30), 
    [% MTL RECEIVED] decimal(18,5),[ASSIGNED USER] nvarchar(200),[WORK ORDER NOTES] nvarchar(MAX));'') 
IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ''ext'' AND TABLE_NAME = ''WOResults'') 
BEGIN select ''Table Created'' END') 

這使我沒有錯誤,而且在連接服務器(服務器B)不創建表。

我也試了兩種方式,但沒有exec這樣的:

SELECT * FROM OPENQUERY(SQLINFWWDVP07,' 
create table [ext].[WOResults]([Id] uniqueidentifier,[AltWorkOrderId] nvarchar(50),[WORK ORDER STATUS] nvarchar(30),[DESCRIPTION] nvarchar(200), 
    [STATE] uniqueidentifier,[PROJ APVL] nvarchar(30),[EST COMP] nvarchar(30),[ACT COMP] nvarchar(30),[cd_M6OrderNo] nvarchar(50),[CDD Date] nvarchar(30), 
    [FinancialProject] uniqueidentifier,[AssignedLocation] uniqueidentifier,[PROJ TYPE] uniqueidentifier,[CLASS OF PLANT] uniqueidentifier, 
    [Justification Code] uniqueidentifier,[CLOSING NOTES] nvarchar(MAX),[READY FOR SERVICE EST] nvarchar(30),[READY FOR SERVICE ACT] nvarchar(30), 
    [EOJ] nvarchar(30),[AUDIT PICK] nvarchar(30),[AUDIT COMP] nvarchar(30),[AUDIT STATUS] nvarchar(100),[FullyReceivedDate] nvarchar(30), 
    [% MTL RECEIVED] decimal(18,5),[ASSIGNED USER] nvarchar(200),[WORK ORDER NOTES] nvarchar(MAX)) 
IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ''ext'' AND TABLE_NAME = ''WOResults'') 
BEGIN select ''Table Created'' END') 

,並得到相同的結果。

有沒有辦法使用OPENQUERY在鏈接服務器中創建表?

我也嘗試使用select * into來創建表,但會得到與最後兩個例子相同的結果。

回答

1

你可以嘗試使用EXEC ... AT

EXEC ('create table tempdb.dbo.t(id int)') At [192.1.1.2] 

對於RPC錯誤。您可以啓用RPC OUT

enter image description here

+0

我只是想,雖然我不得不使用的服務器名稱,因爲IP只是說,它無法找到,在鏈接的服務器列表。我收到了與上面第一個示例中相同的錯誤消息:'服務器'SQLINFWWDVP07'未配置爲RPC。' – Mike

+1

@Mike,對於RPC錯誤,您只需啓用RPC OUT,否則無法寫入遠程服務器 –

+0

完美,工作! – Mike