I have ten thousand records in the postObj object and need to insert into a SQL Server database.
The object structure :
{
postObj : [
{
start_coordinates": {
"latitude": 1.373211,
"longitude": 103.955124
},
"end_coordinates": {
"latitude": 1.31974,
"longitude": 103.889633
},
longitude: 114.063042,
latitude: 22.366266,
event_name : '社交聚會'
event_country: 'New Yotk',
event_date: '2021-06-15 00:02:18-15',
},
{......
}
]
SQL Server database table structure:
event_name , nvarchar(500)
event_country, nvarchar(500)
event_date, datetimeoffset(7)
start_coordinates_latitude, varchar(100)
start_coordinates_longitude, varchar(100)
end_coordinates_latitude, varchar(100)
end_coordinates_longitude, varchar(100)
insert js code
sql.connect(dbConfig, err => {
const request = new sql.Request();
let cols = [];
let inputs = [];
for (let k in postObj) {
request.input(k, postObj[k]);
cols.push(k);
inputs.push('@' + k);
}
let query = `insert into test (${cols.toString()}) values (${inputs.toString()})`;
request.query(query, (err, result) => {
console.log(err);
//stuff here
});
I have following issue:
- filed [event_date] cause the error:
Conversion failed when converting date and/or time from character string.
filed [event_name ] , do I need to put the "N" (like SET @something = N'sometext') for Chinese characters, if need, how to do it?
how to split the sub-object of start_coordinates into start_coordinates_latitude and start_coordinates_longitude?
Thanks for the help