我有一个如下的 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 数据的正确方法吗?