Sql 子查询中主更新查询的引用列
我有一个表Sql 子查询中主更新查询的引用列,sql,sql-server,sql-server-2008,tsql,sql-update,Sql,Sql Server,Sql Server 2008,Tsql,Sql Update,我有一个表UserLOG(>5M行),其中有一列LOG\u PARAMETERS,其中包含Json字符串和其他列(我在这里将其命名为LOG\u param1到LOG\u param9)。我使用从Json中提取参数并更新我的表 以下是我的表创建脚本: CREATE TABLE User_LOG( [LOG_Id] [int] IDENTITY(1,1) NOT NULL, [LOG_PARAMETERS] [nvarchar](500) NULL, [LOG_DATE] [
UserLOG
(>5M行),其中有一列LOG\u PARAMETERS
,其中包含Json
字符串和其他列(我在这里将其命名为LOG\u param1
到LOG\u param9
)。我使用从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参数的子查询中更新所有param列。我需要将LOG\u参数
列传递给子查询中的函数,但出现以下错误:
Msg 4104,16级,状态1,第1行
无法绑定多部分标识符“User\u LOG.LOG\u PARAMETERS”
我甚至不确定这是否可能。有没有办法实现此更新?使用“应用”之类的
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;