子查询中主要更新查询的引用列

时间:2016-06-13 09:03:02

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

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

这是我的表创建脚本:

LOG_param9

一些数据:

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"}')

我从子查询中更新所有param列,在该子查询中我提取Json参数。我需要将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 列传递给子查询中的函数,但我收到此错误:

  

Msg 4104,Level 16,State 1,Line 1   多部分标识符" User_LOG.LOG_PARAMETERS"无法受约束。

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

1 个答案:

答案 0 :(得分: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;