SQL 我如何像这样更新?

发布于 2024-09-08 03:59:33 字数 582 浏览 1 评论 0原文

我正在尝试更新 [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 技术交流群。

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

发布评论

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

评论(3

涫野音 2024-09-15 03:59:33

尝试以下操作来准确了解您在做什么

UDPATE [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] = ToUpdate.[CUSTOMER_NUMBER] )) 
FROM {MandS].[dbo].[Temp_LTGData] ToUpdate
where [CORP_REG_NO] IS NULL

但是,您的查询中有一个错误,我相信子查询应该是 IS NOT NULL。

Try the following to get exactly what you were doing

UDPATE [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] = ToUpdate.[CUSTOMER_NUMBER] )) 
FROM {MandS].[dbo].[Temp_LTGData] ToUpdate
where [CORP_REG_NO] IS NULL

However, you have a mistake in your query I believe the subquery should be IS NOT NULL.

萌无敌 2024-09-15 03:59:33

像这样的东西将允许您处理具有源行和目标行的许多列

如果您需要链接不同列的不同行,那么它会更复杂

如果我理解正确,过滤为CORP_REG_NO IS NULL当然,仅适用于 CORP_REG_NO,因此您不想进行过滤,除非根据您的问题独立于特定列过滤器来限制目标行和源行。

Update
   target
Set
   [CORP_REG_NO] = CASE WHEN target.[CORP_REG_NO] IS NULL THEN source.[CORP_REG_NO] ELSE target.[CORP_REG_NO] END,
    ...and again...
FROM
    [MandS].[dbo].[Temp_LTGData] target
    JOIN
    [MandS].[dbo].[Temp_LTGData] source ON target.[CUSTOMER_NUMBER] = source.[CUSTOMER_NUMBER]
WHERE
    a filter to restrict rows perhaps

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.

Update
   target
Set
   [CORP_REG_NO] = CASE WHEN target.[CORP_REG_NO] IS NULL THEN source.[CORP_REG_NO] ELSE target.[CORP_REG_NO] END,
    ...and again...
FROM
    [MandS].[dbo].[Temp_LTGData] target
    JOIN
    [MandS].[dbo].[Temp_LTGData] source ON target.[CUSTOMER_NUMBER] = source.[CUSTOMER_NUMBER]
WHERE
    a filter to restrict rows perhaps
方觉久 2024-09-15 03:59:33

试试这个:

UPDATE Temp_LTGData LTGSource
  SET Col1 = L2.Col1, Col2 = L2.Col2, Col3 = L2.Col3
FROM LTGSource L1
JOIN LTGSource L2 ON L2.CORP_REG_NO IS NOT NULL AND L1.CUSTOMER_NUMBER = L2.CUSTOMER_NUMBER
WHERE L1.CORP_REG_NO IS NULL 

这应该适合你。您将可更新表连接到其自身,以便可以访问旧行和新行以进行更新。这样,您可以一次更新多个列。

Try this:

UPDATE Temp_LTGData LTGSource
  SET Col1 = L2.Col1, Col2 = L2.Col2, Col3 = L2.Col3
FROM LTGSource L1
JOIN LTGSource L2 ON L2.CORP_REG_NO IS NOT NULL AND L1.CUSTOMER_NUMBER = L2.CUSTOMER_NUMBER
WHERE L1.CORP_REG_NO IS NULL 

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.

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