使用来自日期匹配的 select 语句的多个值更新表
我在更新表格时遇到问题,我确信它非常简单,但我在这里兜圈子。
我想要更新的表“table1”数据的格式如下:
[Month] Figure
----------------------------------
2010-05-01 00:00:00.000 1.0000
2010-06-01 00:00:00.000 1.0000
2010-07-01 00:00:00.000 1.0000
2010-08-01 00:00:00.000 1.0000
包含更新数字的表“data1”的格式如下:
[Month] Figure
----------------------------------
2010-05-01 00:00:00.000 0.7212
2010-08-01 00:00:00.000 1.2351
我正在使用的 SQL 和错误消息如下。
UPDATE t1
SET t1.figure = (SELECT figure from data1)
FROM table1 t1 JOIN data1 d1
ON (t1.[month] = d1.[month])
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.
我需要一个 while 循环来遍历每一行吗?
我希望最终的结果如下:
[Month] Figure
----------------------------------
2010-05-01 00:00:00.000 0.7212
2010-06-01 00:00:00.000 1.0000
2010-07-01 00:00:00.000 1.0000
2010-08-01 00:00:00.000 1.2351
非常感谢。
I'm having a problem updating a table and im sure its pretty straight forward but im going round and round in circles here.
Table 'table1' data I want to update is formatted as follows:
[Month] Figure
----------------------------------
2010-05-01 00:00:00.000 1.0000
2010-06-01 00:00:00.000 1.0000
2010-07-01 00:00:00.000 1.0000
2010-08-01 00:00:00.000 1.0000
Table 'data1' that contains the updated figures is formatted as follows:
[Month] Figure
----------------------------------
2010-05-01 00:00:00.000 0.7212
2010-08-01 00:00:00.000 1.2351
The SQL I'm using and the error message is as follows.
UPDATE t1
SET t1.figure = (SELECT figure from data1)
FROM table1 t1 JOIN data1 d1
ON (t1.[month] = d1.[month])
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.
Would i need a while loop to go through each row?
I wish the end result to be as follows:
[Month] Figure
----------------------------------
2010-05-01 00:00:00.000 0.7212
2010-06-01 00:00:00.000 1.0000
2010-07-01 00:00:00.000 1.0000
2010-08-01 00:00:00.000 1.2351
Much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
UPDATE FROM
语法来实现此目的。查看语法此处和此处。
You can use the
UPDATE FROM
syntax for this.Have a look at the syntax here and here.
SQL Server 2008:
SQL Server 2008 之前版本:
请注意,
UPDATE..FROM
语法是专有的,当目标行与多个源行匹配时,可能会产生不可预测的结果。SQL Server 2008:
Pre-SQL Server 2008:
Note the
UPDATE..FROM
syntax is proprietary and can yield unpredictable results when a target row matches more than one source row.