我想将“未读”列“ 1”更新为“ -1”,反之亦然,“未读”是json字符串中的对象,json字符串存储在SQL SERVER中的“ info”列中( 2017)数据库表“通知”
我的表如下所示:
我的Json String看起来像这样:
{
"Notifications":
[
{
"count": 1,
"notification": "Upload Your Profile Picture",
"Timestamp":"2019-08-12T10:00:00.000Z",
"unread": -1, "click": "empProfile.html"
},
{
"count": 2,
"notification": "Complete Your Profile",
"Timestamp":"2019-08-16T10:00:00.000Z",
"unread": 1,
"click": "empProfile.html"
}
...
...
]
}
我尝试了以下操作:
DECLARE @JSON NVARCHAR(MAX);
SET @JSON = (SELECT info FROM Enotification WHERE EID=1000);
DECLARE @JSONTAB TABLE( count int, notification nvarchar(300), Timestamp DateTime, unread int, click nvarchar(300));
INSERT INTO @JSONTAB(count,notification,timestamp,unread,click)
SELECT * FROM OPENJSON(@JSON, '$.Notifications')
WITH
(
count int,
notification nvarchar(300),
Timestamp DateTime,
unread int,
click nvarchar(300)
);
SELECT * FROM @JSONTAB;
UPDATE @JSONTAB
SET unread=1
WHERE count=1;
UPDATE Enotification
SET info=(SELECT * FROM @JSONTAB FOR JSON)
where EID=1000;
我遇到这样的错误:
Msg 102, Level 15, State 1, Line 25
Incorrect syntax near ')'.
这是这行:
...
SET info=(SELECT * FROM @JSONTAB FOR JSON)
....
它实际上将尝试替换“信息”列中的整个json字符串,但我只希望替换“未读”值。 可能吗??