简体   繁体   中英

How to create tables in a linked table using OPENQUERY?

I have several tables that I would like to create in a linked server using OPENQUERY .

Basically, I have a query which fills a table (TableA) that is run on the linked server (ServerB). I am working on a way to run this query from a different server (ServerA) that has the other (ServerB) as a linked server.

So I have ServerA where I am attempting to run this query, but the query ends up being way too long so I've had to shorten it and break it out into about 8 different queries. For this, to work I need to put the results from each of the broken out queries into tables (Table1, Table2, etc.). When finished I will delete the tables as they will only be needed when this query is run from ServerA .

I've looked around the internet trying to find a way that works I've tried:

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;

Which gives me the error:

Server 'SQLINFWWDVP07' is not configured for RPC.

I've tried this:

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));'') ')

Which gives me the error:

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.

So I would then try this:

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')

Which gives me no errors, but also does not create the table in the linked server (ServerB).

I've also tried both ways but without the exec like this:

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')

And get the same results.

Is there a way to create a table in a linked server using OPENQUERY ?

I have also tried using select * into to create the table but would get the same results as the last two examples.

You can try to use EXEC ... AT

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

For RPC error. you can enable RPC OUT

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM