尝试在字符串字段上匹配 SQL 表更新

发布于 2024-12-11 08:55:28 字数 406 浏览 0 评论 0原文

确实可以在更新查询方面使用一些帮助...(SQL Serer 2008 R2 Express) 我有两个表,tblJPtblMaster

我只有一个在两个表之间匹配的字符串字段。

tblJP AND tblMaster

我需要使用 tblMaster.Long_text 更新 tblJP.LangString

tblJP.short_text = tblMaster.short_text AND tblMaster.Lang = 'jp'

任何帮助将不胜感激。我正在尝试各种逻辑和语法,从创建临时表到其他类型的连接,但都没有成功。

Could really use some help with an update query...(SQL Serer 2008 R2 Express)
I have two tables, tblJP and tblMaster.

I only have a string field that matches between the two tables.

tblJP AND tblMaster

I need to update tblJP.LangString with tblMaster.Long_text when

tblJP.short_text = tblMaster.short_text AND tblMaster.Lang = 'jp'

Any help would be greatly appreciated. I am spinning my wheels trying all sorts of logic and syntax from creating temp tables to other types of joins all with no luck.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

魂牵梦绕锁你心扉 2024-12-18 08:55:28

使用 INNER JOIN 进行简单更新就可以解决问题。

UPDATE     tblJP
SET        tblJP.LangString = tblMaster.Long_Text
FROM       tblJP
INNER JOIN tblMaster ON tblMaster.alt_text = tblJP.short_text
WHERE      tblMaster.Lang = 'jp'

警告:在没有首先针对开发服务器进行测试的情况下,切勿针对生产服务器运行更新语句 - 尤其是当其他人编写 SQL 时。

A simple update with an INNER JOIN should do the trick.

UPDATE     tblJP
SET        tblJP.LangString = tblMaster.Long_Text
FROM       tblJP
INNER JOIN tblMaster ON tblMaster.alt_text = tblJP.short_text
WHERE      tblMaster.Lang = 'jp'

WARNING: Never run an update statement against your production server without first testing it against a development server - especially when someone else wrote the SQL.

信愁 2024-12-18 08:55:28

您还可以使用 MERGE

MERGE INTO tblJP
USING (SELECT *
       FROM   tblMaster
       WHERE  Lang = 'jp') AS SOURCE
ON SOURCE.alt_text = tblJP.short_text
WHEN MATCHED THEN
  UPDATE SET LangString = SOURCE.Long_Text;  

如果 JOIN 返回多行,您将收到错误提示 The MERGE 语句尝试更新或删除同一行多次。

You could also use MERGE

MERGE INTO tblJP
USING (SELECT *
       FROM   tblMaster
       WHERE  Lang = 'jp') AS SOURCE
ON SOURCE.alt_text = tblJP.short_text
WHEN MATCHED THEN
  UPDATE SET LangString = SOURCE.Long_Text;  

In the event that the JOIN returns multiple rows you will be alerted to the problem with an error The MERGE statement attempted to UPDATE or DELETE the same row more than once.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文