Hi @dinesh ,
set = @jsonInfo = '{ "@jsonInfo ": {' +jsonInfo + '}}'
Could you please provide the purpose of adding above row inside the procedure?
I tried from my side as below and you could have a check whether it is helpful.
create procedure dbo.sp_jsontest
@jsonInfo varchar(max)
as
BEGIN
--set @jsonInfo = '{ "@jsonInfo ": {' +jsonInfo + '}}'
INSERT INTO dbo.test
SELECT
JSON_VALUE(@jsonInfo,'$.Easy.Quarter') AS Quarter,
JSON_VALUE(@jsonInfo,'$.Easy.Entity') AS Entity,
JSON_VALUE(@jsonInfo,'$.Easy.Schooldetails') AS Schooldetails,
JSON_VALUE(@jsonInfo,'$.Easy.Teachername') AS Teachername,
JSON_VALUE(@jsonInfo,'$.Easy.Teachid') AS Teachid,
JSON_VALUE(@jsonInfo,'$.Easy.Observation') AS Observation
;with cte as
(SELECT *,ROW_NUMBER() over (order by (select null)) rn
FROM OPENJSON(@jsonInfo, '$.Easy.Schooldetails') WITH (
Username VARCHAR(10) '$.Username',
Age INT '$.Age',
Id INT '$.Id'))
,cte1 as
(SELECT *,ROW_NUMBER() over (order by (select null)) rn
FROM OPENJSON(@jsonInfo, '$.Easy.Additionalinfo') WITH (
CtrName VARCHAR(10) '$.CtrName',
Rank INT '$.Rank',
Levelid VARCHAR(10) '$.Levelid',
business VARCHAR(10) '$.business'))
INSERT INTO dbo.test1
select a.Username,a.Age,a.Id,b.CtrName,b.Rank,b.Levelid,b.business
from cte a
full join cte1 b on a.rn=b.rn
end
Then call this function as below:
exec sp_jsontest '{
"Easy" : {
"Quarter": "2nd Q",
"Entity": "ABC",
"Schooldetails": [{
"Username": "Dinesh,K",
"Age": "45",
"Id": "1"
}, {
"Username": "Dinesh,K",
"Age": "45",
"Id": "1"}
],
"Teachername": "Suni",
"Teachid" : "123",
"Additionalinfo":[{
"CtrName": "test1",
"Rank": "1",
"Levelid": "lvl1",
"business": "forest"
},{
"CtrName": "test2",
"Rank": "2",
"Levelid": "lvl2",
"business": "air"}
],
"Observation": "1"
}'
Best regards,
Melissa
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.