Here I divide 3 parts
a) Generate JSON text Data
b) Read JSON Data
c) Pass JSON as a parameter to the procedure
Insert JSON data into a table
Update JSON parameter data
Delete JSON based data
Return JSON data
Eg:
create table employee
(
eid int,
empname varchar(250),
empadd1 varchar(250),
empadd2 varchar(250),
empadd3 varchar(250),
empphone char(10),
emppin char(6),
empdate datetime
)
go
insert into employee values(1,'Sample Name','Address Line1','Address Line2','Address Line3','1234567891','500012',getdate())
insert into employee values(2,'Sample Name2','Address Line1','Address Line2','Address Line3','1234567881','500013',getdate())
go
select *from employee -- Default select
Eid | empname | empadd1 | empadd2 | empadd3 | empphone | emppin | empdate |
1 | Sample Name | Address Line1 | Address Line2 | Address Line3 | 1234567891 | 500012 | 11-10-2018 |
2 | Sample Name2 | Address Line1 | Address Line2 | Address Line3 | 1234567881 | 500013 | 11-10-2018 |
a) Generate JSON Data
SELECT *FROM employee FOR JSON AUTO -- JSON
[
{ --row start
"eid":1,
"empname":"Sample Name",
"empadd1":"Address Line1",
"empadd2":"Address Line2",
"empadd3":"Address Line3",
"empphone":"1234567891",
"emppin":"500012",
"empdate":"2018-10-11T18:23:43.520"
}, -- row end
{ -- row start
"eid":2,
"empname":"Sample Name2",
"empadd1":"Address Line1",
"empadd2":"Address Line2",
"empadd3":"Address Line3",
"empphone":"1234567881",
"emppin":"500013",
"empdate":"2018-10-11T18:24:46.477"
} -- row end
]
b ) Read JSON text Data
create proc usp_myprocforJson
(
@peramjson NVARCHAR(MAX)
)
AS
BEGIN
SELECT eid,empname,empadd1,empadd2,empadd3,empphone,emppin,empdate
INTO #tblPersons
FROM OPENJSON (@peramjson, '$.root')
WITH (
eid int,
empname varchar(250),
empadd1 varchar(250),
empadd2 varchar(250),
empadd3 varchar(250),
empphone char(10),
emppin char(6),
empdate datetime
)
SELECT *
FROM #tblPersons tmp
DROP TABLE #tblPersons
END
GO
Declare @myparam varchar(max) = '{"root": [
{
"eid": 1,
"empname": "Sample Name",
"empadd1": "Address Line1",
"empadd2": "Address Line2",
"empadd3": "Address Line3",
"empphone": "1234567891",
"emppin": "500012",
"empdate": "2018-10-11T18:23:43.520"
},
{
"eid": 2,
"empname": "Sample Name2",
"empadd1": "Address Line1",
"empadd2": "Address Line2",
"empadd3": "Address Line3",
"empphone": "1234567881",
"emppin": "500013",
"empdate": "2018-10-11T18:24:46.477"
}]}'
Exec usp_myprocforJson @myparam
go
[
{ --row start
"eid":1,
"empname":"Sample Name",
"empadd1":"Address Line1",
"empadd2":"Address Line2",
"empadd3":"Address Line3",
"empphone":"1234567891",
"emppin":"500012",
"empdate":"2018-10-11T18:23:43.520"
}, -- row end
{ -- row start
"eid":2,
"empname":"Sample Name2",
"empadd1":"Address Line1",
"empadd2":"Address Line2",
"empadd3":"Address Line3",
"empphone":"1234567881",
"emppin":"500013",
"empdate":"2018-10-11T18:24:46.477"
} -- row end
]
b ) Read JSON text Data
create proc usp_myprocforJson
(
@peramjson NVARCHAR(MAX)
)
AS
BEGIN
SELECT eid,empname,empadd1,empadd2,empadd3,empphone,emppin,empdate
INTO #tblPersons
FROM OPENJSON (@peramjson, '$.root')
WITH (
eid int,
empname varchar(250),
empadd1 varchar(250),
empadd2 varchar(250),
empadd3 varchar(250),
empphone char(10),
emppin char(6),
empdate datetime
)
SELECT *
FROM #tblPersons tmp
DROP TABLE #tblPersons
END
GO
Declare @myparam varchar(max) = '{"root": [
{
"eid": 1,
"empname": "Sample Name",
"empadd1": "Address Line1",
"empadd2": "Address Line2",
"empadd3": "Address Line3",
"empphone": "1234567891",
"emppin": "500012",
"empdate": "2018-10-11T18:23:43.520"
},
{
"eid": 2,
"empname": "Sample Name2",
"empadd1": "Address Line1",
"empadd2": "Address Line2",
"empadd3": "Address Line3",
"empphone": "1234567881",
"emppin": "500013",
"empdate": "2018-10-11T18:24:46.477"
}]}'
Exec usp_myprocforJson @myparam
go
|
Working on next ...,
Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site.
ReplyDeletewebsite designing services in warangal
SEO Services in Warangal
Php Services in Warangal
Digital marketing services in warangal
kuşadası
ReplyDeleteşırnak
tekirdağ
van
sakarya
PME7