sql - 来自 Select 的 SQL 更新
问题描述
update table1
set month = x
select FORMAT(getdate(), 'MMM yyyy') x
from table1
where currenmonth >= dateadd(month, -1, datefromparts(year(getdate()), month(getdate()), 1))
and currentmonth < datefromparts(year(getdate()), month(getdate()), 1)
我收到一个错误:
列名“x”无效
不知道为什么我会收到这个错误。需要帮忙。谢谢。
解决方案
这甚至不是一个正确的查询。
检查此示例(来自https://chartio.com/resources/tutorials/how-to-update-from-select-in-sql-server/):
UPDATE
books
SET
books.primary_author = authors.name
FROM
books
INNER JOIN
authors
ON
books.author_id = authors.id
WHERE
books.title = 'The Hobbit'
所以你的查询应该是......
update table1
set month = FORMAT(getdate(), 'MMM yyyy')
from table1
where
currenmonth >= dateadd(month, -1, datefromparts(year(getdate()), month(getdate()), 1))
and currentmonth < datefromparts(year(getdate()), month(getdate()), 1)
推荐阅读
- deep-learning - Best way to handle batch during training and inference in Pytorch with GPU
- python - multiple combinations of one column based on another
- parse-platform - 从 cURL 调用云代码时设置用户(用于测试)
- collision-detection - speed increases after elastic collision in 2d?
- html - When the tag type is inline-block, when text or images are inserted, the layout is not kept horizontal
- python - 如何在矩阵的每一行中保持最小的非零值?
- c# - 如何从中断模式异常中获取更多信息
- javascript - 使用 JavaScript 或 jQuery 进入视口时如何加载内容?
- javascript - 根据数组中单个子文档上的字段值查找文档
- spring-boot - Spring Boot 中的自定义 REST API,用于在 Hyperledger Fabric 中创建 Peers、Orderer、Channels 等