SQL 我如何像这样更新?
我正在尝试更新 [Temp_LTGData] 表中的所有 SQL 行,将 [CORP_REG_NO] 值设置为同一表中 [CUSTOMER_NUMBER] 匹配的另一行中的值。
最终我需要用相当多的列来做这件事,有谁知道这是否可以做到?
我似乎无法像在选择查询中那样使用 LTGSource 别名:(
Update [MandS].[dbo].[Temp_LTGData] LTGSource
Set [CORP_REG_NO] = (SELECT [CORP_REG_NO]
FROM [MandS].[dbo].[Temp_LTGData]
WHERE ([CORP_REG_NO] IS NULL
AND [CUSTOMER_NUMBER] = LTGSource.[CUSTOMER_NUMBER] ))
where [CORP_REG_NO] IS NULL
感谢大家的反馈,这是一些非常棒的东西,我什至学到了解决这个问题的一些不同方法(为你们所有人投票)。
I'm trying to update all SQL rows in the [Temp_LTGData] table setting the [CORP_REG_NO] value to the value in another row in the same table where the [CUSTOMER_NUMBER] matches.
Ultimately I need to do this with quite a few columns, does anyone know if this can be done?
I can't seem to use the LTGSource alias like in a select query :(
Update [MandS].[dbo].[Temp_LTGData] LTGSource
Set [CORP_REG_NO] = (SELECT [CORP_REG_NO]
FROM [MandS].[dbo].[Temp_LTGData]
WHERE ([CORP_REG_NO] IS NULL
AND [CUSTOMER_NUMBER] = LTGSource.[CUSTOMER_NUMBER] ))
where [CORP_REG_NO] IS NULL
Thanks for the feedback guys that's some really awesome stuff, I even learnt some different approaches to this problem (voted for you all).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试以下操作来准确了解您在做什么
但是,您的查询中有一个错误,我相信子查询应该是 IS NOT NULL。
Try the following to get exactly what you were doing
However, you have a mistake in your query I believe the subquery should be IS NOT NULL.
像这样的东西将允许您处理具有源行和目标行的许多列
如果您需要链接不同列的不同行,那么它会更复杂
如果我理解正确,过滤为
CORP_REG_NO IS NULL
当然,仅适用于 CORP_REG_NO,因此您不想进行过滤,除非根据您的问题独立于特定列过滤器来限制目标行和源行。Something like this which will allow you to deal with many columns with a source and target row
If you need to link different rows for different columns, then it's more complex
If I understand correctly, filtering to
CORP_REG_NO IS NULL
would only work for CORP_REG_NO of course so you don't want to filter except to restrict target and source rows independently of specific column filters as per you're question.试试这个:
这应该适合你。您将可更新表连接到其自身,以便可以访问旧行和新行以进行更新。这样,您可以一次更新多个列。
Try this:
That should do it for you. You're joining the updateable table to itself so you can have access to both the old row and the new row for the update. This way, you can update multiple columns at once.