循环遍历 JSON 数据并更新 SQL 服务器中的查询
LOOP Through JSON Data and update query in SQL Sever
我有一个 JSON 数组如下
[
{
"CRT_FIRST_NAME": "abc",
"CRT_EMAIL": "abc@gmail.com",
"CRT_ID": 60
},
{
"CRT_FIRST_NAME": "pqr",
"CRT_EMAIL": "pqr@gmail.com",
"CRT_ID": 61
},
{
"CRT_FIRST_NAME": "sample",
"CRT_EMAIL": "sample@gmail.com",
"CRT_ID": 62
}
]
我正在传递它并执行一个存储过程save_employee_data
如下
exec save_employee_data '[{"CRT_FIRST_NAME": "abc","CRT_EMAIL": "abc@gmail.com","CRT_ID": 60},{"CRT_FIRST_NAME": "pqr","CRT_EMAIL": "pqr@gmail.com","CRT_ID": 61},{"CRT_FIRST_NAME": "sample","CRT_EMAIL": "sample@gmail.com","CRT_ID": 62}]'
我的存储过程如下
CREATE PROCEDURE [dfed].[save_employee_data]
@jsondata nvarchar(max)
AS
DECLARE
@CRTID_FETCH INT,
@CRT_FIRST_NAME_FETCH VARCHAR(250),
@CRT_EMAIL_FETCH VARCHAR(250)
BEGIN
SELECT @CRT_FIRST_NAME_FETCH=CRT_FIRST_NAME, @CRT_EMAIL_FETCH=CRT_EMAIL, @CRTID_FETCH=CRT_ID FROM
OPENJSON ( @jsondata )
WITH (
CRT_FIRST_NAME varchar(200) '$.CRT_FIRST_NAME' ,
CRT_EMAIL varchar(200) '$.CRT_EMAIL',
CRT_ID varchar(200) '$.CRT_ID'
)
UPDATE employees SET email_staff = @CRT_EMAIL_FETCH WHERE CRT_ID = @CRTID_FETCH
END
此处更新查询不起作用。
我正在尝试循环我的 JSON 数据并从 json 数据中获取 CRT_EMAIL
、CRT_FIRST_NAME
、CRT_ID
值,并根据这些值我尝试更新 email_staff
列,其各自列的值为 CRT_EMAIL
,即主键 CRT_ID
但是更新查询不起作用。这是循环 JSON 数据的正确方法吗?
只需将 JSON 数据加入 employees
table 并更新结果。像这样
with n as
(
SELECT CRT_FIRST_NAME, CRT_EMAIL, CRT_ID
FROM OPENJSON ( @jsondata )
WITH (
CRT_FIRST_NAME varchar(200) '$.CRT_FIRST_NAME' ,
CRT_EMAIL varchar(200) '$.CRT_EMAIL',
CRT_ID varchar(200) '$.CRT_ID'
)
), q as
(
select n.CRT_EMAIL, e.email_staff , e.crt_id
from n
join employees e
on n.CRT_ID = e.CRT_ID
)
--select * from q;
update q set email_staff = CRT_EMAIL;
如果要遍历 json 数据,可以使用游标。 EG
DECLARE C CURSOR LOCAL FOR
SELECT CRT_FIRST_NAME, CRT_EMAIL, CRT_ID
FROM OPENJSON ( @jsondata )
WITH (
CRT_FIRST_NAME varchar(200) '$.CRT_FIRST_NAME' ,
CRT_EMAIL varchar(200) '$.CRT_EMAIL',
CRT_ID varchar(200) '$.CRT_ID'
);
OPEN C;
FETCH NEXT FROM C INTO @CRT_FIRST_NAME, @CRT_EMAIL, @CRT_ID;
WHILE @@FETCH_STATUS = 0
BEGIN
UPDATE employees SET email_staff = @CRT_EMAIL WHERE CRT_ID = @CRT_ID;
FETCH NEXT FROM C INTO @CRT_FIRST_NAME, @CRT_EMAIL, @CRT_ID;
END
CLOSE C;
DEALLOCATE C;
我有一个 JSON 数组如下
[
{
"CRT_FIRST_NAME": "abc",
"CRT_EMAIL": "abc@gmail.com",
"CRT_ID": 60
},
{
"CRT_FIRST_NAME": "pqr",
"CRT_EMAIL": "pqr@gmail.com",
"CRT_ID": 61
},
{
"CRT_FIRST_NAME": "sample",
"CRT_EMAIL": "sample@gmail.com",
"CRT_ID": 62
}
]
我正在传递它并执行一个存储过程save_employee_data
如下
exec save_employee_data '[{"CRT_FIRST_NAME": "abc","CRT_EMAIL": "abc@gmail.com","CRT_ID": 60},{"CRT_FIRST_NAME": "pqr","CRT_EMAIL": "pqr@gmail.com","CRT_ID": 61},{"CRT_FIRST_NAME": "sample","CRT_EMAIL": "sample@gmail.com","CRT_ID": 62}]'
我的存储过程如下
CREATE PROCEDURE [dfed].[save_employee_data]
@jsondata nvarchar(max)
AS
DECLARE
@CRTID_FETCH INT,
@CRT_FIRST_NAME_FETCH VARCHAR(250),
@CRT_EMAIL_FETCH VARCHAR(250)
BEGIN
SELECT @CRT_FIRST_NAME_FETCH=CRT_FIRST_NAME, @CRT_EMAIL_FETCH=CRT_EMAIL, @CRTID_FETCH=CRT_ID FROM
OPENJSON ( @jsondata )
WITH (
CRT_FIRST_NAME varchar(200) '$.CRT_FIRST_NAME' ,
CRT_EMAIL varchar(200) '$.CRT_EMAIL',
CRT_ID varchar(200) '$.CRT_ID'
)
UPDATE employees SET email_staff = @CRT_EMAIL_FETCH WHERE CRT_ID = @CRTID_FETCH
END
此处更新查询不起作用。
我正在尝试循环我的 JSON 数据并从 json 数据中获取 CRT_EMAIL
、CRT_FIRST_NAME
、CRT_ID
值,并根据这些值我尝试更新 email_staff
列,其各自列的值为 CRT_EMAIL
,即主键 CRT_ID
但是更新查询不起作用。这是循环 JSON 数据的正确方法吗?
只需将 JSON 数据加入 employees
table 并更新结果。像这样
with n as
(
SELECT CRT_FIRST_NAME, CRT_EMAIL, CRT_ID
FROM OPENJSON ( @jsondata )
WITH (
CRT_FIRST_NAME varchar(200) '$.CRT_FIRST_NAME' ,
CRT_EMAIL varchar(200) '$.CRT_EMAIL',
CRT_ID varchar(200) '$.CRT_ID'
)
), q as
(
select n.CRT_EMAIL, e.email_staff , e.crt_id
from n
join employees e
on n.CRT_ID = e.CRT_ID
)
--select * from q;
update q set email_staff = CRT_EMAIL;
如果要遍历 json 数据,可以使用游标。 EG
DECLARE C CURSOR LOCAL FOR
SELECT CRT_FIRST_NAME, CRT_EMAIL, CRT_ID
FROM OPENJSON ( @jsondata )
WITH (
CRT_FIRST_NAME varchar(200) '$.CRT_FIRST_NAME' ,
CRT_EMAIL varchar(200) '$.CRT_EMAIL',
CRT_ID varchar(200) '$.CRT_ID'
);
OPEN C;
FETCH NEXT FROM C INTO @CRT_FIRST_NAME, @CRT_EMAIL, @CRT_ID;
WHILE @@FETCH_STATUS = 0
BEGIN
UPDATE employees SET email_staff = @CRT_EMAIL WHERE CRT_ID = @CRT_ID;
FETCH NEXT FROM C INTO @CRT_FIRST_NAME, @CRT_EMAIL, @CRT_ID;
END
CLOSE C;
DEALLOCATE C;