使用 SQL 批量更新记录 [英] Bulk Record Update with SQL

查看:28
本文介绍了使用 SQL 批量更新记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 SQL Server 2008 环境中有两个表,结构如下

I have two tables in a SQL Server 2008 environment with the following structure

Table1
- ID
- DescriptionID
- Description

Table2
- ID
- Description

Table1.DescriptionID 映射到 Table2.ID.但是,我不再需要它了.我想进行批量更新以将 Table1 的 Description 属性设置为 Table2 中与其关联的值.换句话说,我想做这样的事情:

Table1.DescriptionID maps to Table2.ID. However, I do not need it any more. I would like to do a bulk update to set the Description property of Table1 to the value associated with it in Table2. In other words I want to do something like this:

UPDATE
  [Table1] 
SET
  [Description]=(SELECT [Description] FROM [Table2] t2 WHERE t2.[ID]=Table1.DescriptionID)

但是,我不确定这是否是合适的方法.有人可以告诉我怎么做吗?

However, I'm not sure if this is the appropriate approach. Can someone show me how to do this?

推荐答案

你的方法是正确的,下面是另一种方法:

Your way is correct, and here is another way you can do it:

update      Table1
set         Description = t2.Description
from        Table1 t1
inner join  Table2 t2
on          t1.DescriptionID = t2.ID

嵌套选择只是进行连接的漫长过程.

The nested select is the long way of just doing a join.

这篇关于使用 SQL 批量更新记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆