[英]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.