简体   繁体   English

如何从本地SQL Server表更新链接表列

[英]How to update linked table column from local SQL Server table

I have a local SQL Server table and a remote linked MySQL table. 我有一个本地SQL Server表和一个远程链接的MySQL表。

I need to pull email addresses from the local table to update the remote. 我需要从本地表中提取电子邮件地址以更新远程服务器。

This T-SQL statement works for a single update: 该T-SQL语句适用于单个更新:

UPDATE openquery(SKYCOMLINK2, 'select tng_id, tng_account, tng_email from user_list where tng_account = 12345 and tng_status = ''A''') 
SET tng_email = 'blah@blah.com';

What I want to do is, for every RemoteTable record with a status of 'A', I want to pull an email address from a local SQL Server table (example for a single record): 我想要做的是,对于状态为“ A”的每个RemoteTable记录,我都希望从本地SQL Server表中提取一个电子邮件地址(例如单个记录的示例):

select email 
from LocalTable 
where id = 12345

So in English: for every active record in RemoteTable (could be multiples) look for a corresponding record in LocalTable of the same account number (one record per account number) and pull the email address from it to populate the matching records in RemoteTable . 因此,用英语来说:对于RemoteTable每个活动记录(可能是倍数),请在LocalTableLocalTable具有相同帐号(每个帐号一个记录)的对应记录,并LocalTable提取电子邮件地址以填充RemoteTable的匹配记录。 If it makes it easier, the LocalTable can be the driver as below (in quasi SQL-English): 如果使它更容易,则LocalTable可以是以下驱动程序(准SQL-英语):

update RemoteTable 
set RemoteTable.email = LocalTable.email 
where RemoteTable.accountNum = LocalTable.accountNum 
  and LocalTable.status = 'a' 
  and RemoteTable.status = 'a'

How can I do that? 我怎样才能做到这一点? Thanks in advance. 提前致谢。

If MySQL doesn't have four part names and SQL Server won't accept the reference without one, you might try serverName.databaseName..tableName. 如果MySQL没有四个部分名称,并且SQL Server没有一个部分名称将不接受引用,则可以尝试使用serverName.databaseName..tableName。 Leaving the schema name blank implies the default schema. 将模式名称保留为空白表示默认模式。 However, per this question , that may not work with all versions of SQL Server, MySQL, and the ODBC driver. 但是,对于这个问题 ,这可能不适用于所有版本的SQL Server,MySQL和ODBC驱动程序。

Here is another question that provides multiple ways of solving the problem: Do I have to use OpenQuery to query a MySQL Linked Server from SQL Server? 这是提供解决问题的多种方法的另一个问题: 是否必须使用OpenQuery从SQL Server查询MySQL Linked Server?

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

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