【问题标题】:Reference column from main update query in subquery子查询中主更新查询的引用列
【发布时间】:2016-10-13 15:04:30
【问题描述】:

我有一个表 UserLOG(>5M 行),其中有一列 LOG_PARAMETERS,其中包含一个 Json 字符串和其他列(我在这里将它们命名为 LOG_param1LOG_param9)。我正在使用Phil Factor TSQL Function 从 Json 中提取参数并更新我的表。

这是我的表创建脚本:

CREATE TABLE User_LOG(
    [LOG_Id] [int] IDENTITY(1,1) NOT NULL,
    [LOG_PARAMETERS] [nvarchar](500) NULL,
    [LOG_DATE] [varchar](25) NULL,
    [LOG_param1] [uniqueidentifier] NULL,
    [LOG_param2] [uniqueidentifier] NULL,
    [LOG_param3] [uniqueidentifier] NULL,
    [LOG_param4] [uniqueidentifier] NULL,
    [LOG_param5] [uniqueidentifier] NULL,
    [LOG_param6] [uniqueidentifier] NULL,
    [LOG_param7] [varchar](25) NULL,
    [LOG_param8] [int] NULL,
    [LOG_param9] [smallint] NULL,
    CONSTRAINT [PK_UserLOG] PRIMARY KEY CLUSTERED ([LOG_Id] ASC)
)

一些数据:

INSERT INTO dbo.User_LOG ([LOG_PARAMETERS]) 
   values ('{"LOG_param7": "2015-06-06T17:24:06.000Z", "LOG_param1": "04162673-90df-495f-9691-32c063b78e80", "LOG_param2": "2230f23e-83b6-46b8-aa81-fefce20efdf0", "LOG_param3": "7aa411a0-c265-4d8f-896a-4b2707c4086e"}'),
   ('{"LOG_param7": "2015-06-06T17:24:06.000Z", "LOG_param1": "04462673-90df-495f-9691-32c063b88e80", "LOG_param2": "2230f23e-85b6-46b8-aa81-fefce20efdf0", "LOG_param3": "7aa419a0-c265-4d8f-896a-4b2707c4086e"}'),
   ('{"LOG_param7": "2015-06-06T17:24:06.000Z", "LOG_param1": "04162673-90df-495f-9691-32c063b68e80", "LOG_param2": "2260f23e-83b6-46b8-ba81-fefce20efdf0", "LOG_param3": "7aa511a0-c265-4d8f-896a-4b2707c4086e"}'),
   ('{"LOG_param7": "2015-06-06T17:24:06.000Z", "LOG_param1": "04152673-90df-495f-9691-32c063b98e80", "LOG_param2": "2230f23e-82b6-46b8-va81-fefce20efdf0", "LOG_param3": "7aa411a0-c265-4d8f-893a-4b2707c4086e"}')

我正在尝试使用以下查询更新此表:

UPDATE  User_LOG 
SET  LOG_param1 = CONVERT(UNIQUEIDENTIFIER, j.[LOG_param1])
    ,LOG_param2 = CONVERT(UNIQUEIDENTIFIER, j.[LOG_param2])
    ,LOG_param3 = CONVERT(UNIQUEIDENTIFIER, j.[LOG_param3])
    ,LOG_param4 = CONVERT(UNIQUEIDENTIFIER, j.[LOG_param4])
    ,LOG_param5 = CONVERT(UNIQUEIDENTIFIER, j.[LOG_param5])
    ,LOG_param6 = CONVERT(UNIQUEIDENTIFIER, j.[LOG_param6])
    ,LOG_param7 = j.[LOG_param7]
    ,LOG_param8 = CAST(j.[LOG_param8] AS INT)
    ,LOG_param9 = CAST(j.[LOG_param9] AS SMALLINT)
FROM (SELECT 
         max(case when name='LOG_param1' then value else null end) as [LOG_param1],
         max(case when name='LOG_param2' then value else null end) as [LOG_param2],
         max(case when name='LOG_param3' then value else null end) as [LOG_param3],
         max(case when name='LOG_param4' then value else null end) as [LOG_param4],
         max(case when name='LOG_param5' then value else null end) as [LOG_param5],
         max(case when name='LOG_param6' then value else null end) as [LOG_param6],
         max(case when name='LOG_param7' then value else null end) as [LOG_param7],
         max(case when name='LOG_param8' then value else null end) as [LOG_param8],
         max(case when name='LOG_param9' then value else null end) as [LOG_param9]
     FROM temp.parseJSON(User_LOG.LOG_PARAMETERS) /* I want to reference the LOG_PARAMETERS column here */
     ) j

我从提取 Json 参数的子查询中更新所有参数列。我需要将LOG_PARAMETERS 列传递给子查询中的函数,但出现此错误:

消息 4104,第 16 级,状态 1,第 1 行 无法绑定多部分标识符“User_LOG.LOG_PARAMETERS”。

我什至不确定这是否可能。有没有办法实现这个更新?

【问题讨论】:

    标签: sql sql-server sql-server-2008 tsql sql-update


    【解决方案1】:

    使用 APPLY ,一种

    UPDATE  User_LOG 
    SET  LOG_param1 = CONVERT(UNIQUEIDENTIFIER, j.[LOG_param1])
        ,LOG_param2 = CONVERT(UNIQUEIDENTIFIER, j.[LOG_param2])   
        ,LOG_param3 = CONVERT(UNIQUEIDENTIFIER, j.[LOG_param3])
        ,LOG_param4 = CONVERT(UNIQUEIDENTIFIER, j.[LOG_param4])
        ,LOG_param5 = CONVERT(UNIQUEIDENTIFIER, j.[LOG_param5])
        ,LOG_param6 = CONVERT(UNIQUEIDENTIFIER, j.[LOG_param6])
        ,LOG_param7 = j.[LOG_param7]
    
        ,LOG_param8 = CAST(j.[LOG_param8] AS INT)
        ,LOG_param9 = CAST(j.[LOG_param9] AS SMALLINT)
    FROM User_LOG
    CROSS APPLY (SELECT 
             max(case when name='LOG_param1' then Stringvalue else null end) as [LOG_param1],
             max(case when name='LOG_param2' then Stringvalue else null end) as [LOG_param2],
             max(case when name='LOG_param3' then Stringvalue else null end) as [LOG_param3],
             max(case when name='LOG_param4' then Stringvalue else null end) as [LOG_param4],
             max(case when name='LOG_param5' then Stringvalue else null end) as [LOG_param5],
             max(case when name='LOG_param6' then Stringvalue else null end) as [LOG_param6],
             max(case when name='LOG_param7' then Stringvalue else null end) as [LOG_param7],
             max(case when name='LOG_param8' then Stringvalue else null end) as [LOG_param8],
             max(case when name='LOG_param9' then Stringvalue else null end) as [LOG_param9]
         FROM parseJSON(LOG_PARAMETERS) /* I want to reference the LOG_PARAMETERS column here */
         ) j;
    

    【讨论】:

      猜你喜欢
      • 2021-12-08
      • 1970-01-01
      • 1970-01-01
      • 2019-07-18
      • 1970-01-01
      • 2022-01-26
      • 1970-01-01
      • 2012-04-17
      相关资源
      最近更新 更多