import.sql(((( ``
use `[D:\DBSD.CW2.14714.13786.12928\DBSD.CW2.14714.13786.12928\DBSD.CW2.14714.13786.12928\APPDATA\CAMBRIDGE.MDF]
--generating sample XML
select top 2 EmployeeID "@id", FirstName "@fn", LastName "@ln", Position "@position", HireDate "@hiredate", 1 as "@IsActive"
from Employee where Position is not null
for xml path('Employee'), root('Employees')
-------XML insert with id --------
declare @xml nvarchar(max) = '<Employees>
<Employee id="400" fn="Margaret" ln="Park" position="Sales Support Agent" hiredate="2024-03-28" IsActive="1"/>
<Employee id="500" fn="Steve" ln="Johnson" position="Sales Support Agent" hiredate="2024-03-28" IsActive="1"/>
</Employees>'
declare @xmlDocHandle int
exec sp_xml_preparedocument @xmlDocHandle out--, @xml
set IDENTITY_INSERT employee on
insert into Employee(EmployeeID, FirstName, lastname, Position, IsActive, HireDate)
output inserted.*
select EmployeeID, FirstName, lastname, Position, IsActive, HireDate
from openxml(@xmlDocHandle, N'/Employees/Employee')
with(
EmployeeID int '@id',
FirstName nvarchar(200) '@fn',
LastName nvarchar(200),
Position nvarchar(100),
IsActive bit '@IsActive',
HireDate DATE '@hiredate'
)
set IDENTITY_INSERT employee off
------ XML insert with id generated by identity -----------
go
declare @xml nvarchar(max) = '<Employees>
<Employee id="4" fn="Margaret" ln="Park" position="Sales Support Agent" hiredate="2024-03-28" IsActive="1"/>
<Employee id="5" fn="Steve" ln="Johnson" position="Sales Support Agent" hiredate="2024-03-28" IsActive="1"/>
</Employees>'
go
create or alter procedure udpEmployeeImportFromXml(@xml nvarchar(2000))
as
begin
declare @xmlDocHandle int
exec sp_xml_preparedocument @xmlDocHandle out, @xml
insert into Employee(firstname, lastname, Position, IsActive, HireDate)
output inserted.*
select firstname, ln, position, IsActive, HireDate
from openxml(@xmlDocHandle, N'/Employees/Employee')
with(
EmployeeID int '@id',
FirstName nvarchar(200) '@fn',
ln nvarchar(200),
position nvarchar(100),
IsActive BIT '@IsActive',
HireDate DATE '@hiredate'
)
end
---
go
exec udpEmployeeImportFromXml '<Employees>
<Employee id="4" fn="Margaret" ln="Park" position="Agent" hiredate="2024-03-28" IsActive="1"/>
<Employee id="5" fn="Steve" ln="Johnson" position="Agent" hiredate="2024-03-28" IsActive="1"/>
</Employees>'
select * from employee
-------------- JSON import ----------------------
--generating sample JSON
select top 2 EmployeeID "id", FirstName "fn", LastName "ln", Position "position", HireDate "@hiredate", 1 as "@IsActive"
from Employee where position is not null
for json path
go
---- json without root wrapper
declare @json nvarchar(max) = '[
{
"id": 9356,
"fn": "Margaret",
"ln": "Park",
"position": "Sales Support Agent",
"hiredate": "2023-01-15",
"IsActive": true
},
{
"id": 6985,
"fn": "Steve",
"ln": "Johnson",
"position": "Sales Support Agent"
"hiredate": "2023-04-15",
"IsActive": true
}
]';
--Test again
select *
from openjson(@json)
with(
id int,
fn nvarchar(200),
ln nvarchar(200),
position nvarchar(100),
hiredate DATE '$.hiredate',
IsActive BIT '$.IsActive'
);
------------- Json with root wrapper ----
select top 2 EmployeeID "id", FirstName "fn", LastName "ln", Position "position", HireDate "@hiredate", 1 A S "@IsActive"
from Employee where position is not null
for json path
go
---- json without root wrapper
DECLARE @json NVARCHAR(MAX) = '{
"Employee":[
{
"id": 4,
"fn": "Margaret",
"ln": "Park",
"position": "Sales Support Agent",
"hiredate": "2023-01-15",
"IsActive": true
},
{
"id": 5,
"fn": "Steve",
"ln": "Johnson",
"position": "Sales Support Agent",
"hiredate": "2023-01-15",
"IsActive": true
}
]
}'
go
create or alter procedure udpEmployeeImportFromJson(@json nvarchar(max))
as
begin
insert into Employee(firstname, lastname, Position, HireDate, IsActive)
output inserted.*
select fn, ln, position, HireDate, IsActive
from openjson(@json, '$.Employee')
with(
id int '$.id',
fn nvarchar(200),
ln nvarchar(200),
position nvarchar(100),
IsActive BIT '@IsActive',
HireDate DATE '@hiredate'
)
end
--- test
---test again
go
EXEC udpEmployeeImportFromJson '{"Employee":[{"id":4,"fn":"Margaret","ln":"Park","position":"Sales Support Agent","IsActive":true,"hiredate": "2023-01-15"},{"id":5,"fn":"Steve","ln":"Johnson","position":"Sales Support Agent","IsActive":true,"hiredate": "2023-01-15"}]}'
`
` ))))
Good Day. I am trying to import json and xml file to my database. so I can insert data from there but once I have choosen the nexessary file it is showing me the error that i showed above as screenshot. I dont know if it is in the import.sql or repository itself. thank you all for your reponse beforehand