简体   繁体   English

从一个 SQL 服务器到另一个在源上删除表的直通查询

[英]Passthrough Query from one SQL Server to another that drops a table on the source

I have a SQL Server in Spain and one in the US and there is a domain trust between the two with linked servers on each for access to the other.我在西班牙有一个 SQL 服务器,在美国有一个服务器,两者之间存在域信任,每个服务器上都有链接服务器,可以访问另一个服务器。

I would like to be able to run the below query on the US SQL Server without having to maintain a stored proc on the US Server in order to run it.我希望能够在美国 SQL 服务器上运行以下查询,而无需在美国服务器上维护存储过程以运行它。 Is there a way to create a passthrough query from the SQL Server in Spain?有没有办法从西班牙的 SQL 服务器创建直通查询? I've already tried using OPENQUERY and OPENROWSET and it's just not working as they only seem to work with select statements that return results:我已经尝试过使用 OPENQUERY 和 OPENROWSET ,但它不起作用,因为它们似乎只适用于返回结果的 select 语句:

DROP TABLE IF EXISTS [Global].[dbo].[WW_Customer_Receivables]

SELECT * 

INTO [global].[dbo].[ww_customer_receivables]

FROM 
    [LinkedServerObject-Spain].[global].dbo.ww_customer_receivables

If you want to execute DDL statement on your linked server with openquery you can with the following trick:如果您想使用openquery在链接服务器上执行 DDL 语句,您可以使用以下技巧:

SELECT *
FROM OPENQUERY(linkedserver, '
DROP TABLE IF EXISTS [Global].[dbo].[WW_Customer_Receivables]
SELECT @@RowCount')

The SELECT @@RowCount returns a result set. SELECT @@RowCount返回一个结果集。 So OPENQUERY works.所以OPENQUERY有效。 This trick works for all DDL operations like create, alter, drop.这个技巧适用于所有 DDL 操作,如创建、更改、删除。 Or if you want to perform inserts/updates/deletes that don't return a result set.或者,如果您想执行不返回结果集的插入/更新/删除。

Same trick is applied here Where they have a dummy select foobar.同样的技巧在这里应用他们有一个虚拟 select foobar。

If you want to execute the into statement from the openquery you can do it like this:如果你想从openquery执行into语句,你可以这样做:

DROP TABLE IF EXISTS [Global].[dbo].[WW_Customer_Receivables]

SELECT *
INTO [global].[dbo].[ww_customer_receivables]
FROM OPENQUERY([LinkerServerObject-US], '
    SELECT *
    FROM [global].dbo.ww_customer_receivables')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从一个表中提取数据以用于SQL Server的另一查询中? - Pulling data from one table to use in another query for SQL Server? SQL Server-查询以生成从一个表到另一个表的JOINs路径? - SQL Server - A query to generate a JOINs path from one table to another? 将表从一个数据库复制到另一个数据库时,sql server 2008将锁定源表 - will sql server 2008 lock source table while copying table from one database to another 更新查询从SQL Server 2008年源表中的多个匹配的行吗? - update query for more than one matched rows from source table in SQL server 2008? 从一个表到另一个表的SQL更新查询 - SQL Update Query From one Table to another 访问大型SQL表的直通查询 - Acces passthrough query for large sql table 从一个表中选择,插入到另一个表中 oracle sql 查询 - select from one table, insert into another table oracle sql query 可以从一台服务器对另一台SQL服务器进行查询 - Is is possible to do a query on another SQL server from one server SQL Server 在查询后以静默方式删除事务 - SQL Server silently drops a transaction after a query sql / plsql-将源表中的行插入目标中,目标中的一列取决于源表中的另一列 - sql/plsql - insert rows from source table to target with one column in target depending on another column in Source table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM