我有一个SQL数据库和HTTP触发的Azure函数,该函数接收数据并将其写入数据库。问题是,当我尝试向该函数发送10个带有300条JSON行的块并将其写入数据库时。那里什么都没有。但是当我在一个信号中发送300条线时-函数将这些数据写入表中。有什么问题吗?
我是Azure函数和JavaScript的新手。我使用乏味的方法来连接数据库并执行请求。
if (req.body) {
// get id - if exist increment, else create new
const connection = new Connection(config);
connection.on('connect', function(err){
console.log("Connected");
var stemData_list =
["hovednummer","leverandoernummer","cvrnummer","chrnummer","first_name",
"last_name",
"alternative_name","street","street_num","city","postal_code","country"]
stemData_list = stemData_list.sort()
var stemData_list_con = stemData_list.join("")
var slaughterData_list =
["hovednummer","leverandoernummer","chrnummer", "slaughters"]
slaughterData_list = slaughterData_list.sort()
var slaughterData_list_con = slaughterData_list.join("")
var insertionData_list =
["hovednummer","insertions","chrnummer"]
insertionData_list = insertionData_list.sort()
var insertionData_list_con = insertionData_list.join("")
var statusData_list =
["hovednummer","chrnummer","status_count","status_date"]
statusData_list = statusData_list.sort()
var statusData_list_con = statusData_list.join("")
var keys_names = Object.keys(req.body[0])
keys_names = keys_names.sort()
var keys_names_con = keys_names.join("")
//NEW
var request_write_stem = [];
var request_write_status = [];
var FctInsertion_insert = [];
var FctSlaughter_insert = [];
// StemData (main data)
if (keys_names_con == stemData_list_con){
for (var b = 0; b < req.body.length; b++){
// COLLECT VALUES!!!
// NEW
request_write_stem[b] = "IF((Select unit_id From
DKUnitId where hovednummer='"+ req.body[b].hovednummer +"' \
and leverandoernummer ='"+ req.body[b].leverandoernummer +"' and chrnummer
='"+ req.body[b].chrnummer +"')=(Select unit_id from UnitInfo \
where UnitInfo.unit_id=(Select unit_id from DKUnitId where
hovednummer='"+req.body[b].hovednummer+"' and \
leverandoernummer ='"+req.body[b].leverandoernummer+"' and chrnummer ='"+
req.body[b].chrnummer +"'))) \
Update UnitInfo set first_name = '"+ req.body[b].first_name +"' ,
last_name
= '"+ req.body[b].last_name +"', \
street = '"+ req.body[b].street +"', street_num = '"+
req.body[b].street_num +"', city = '"+ req.body[b].city +"', \
postal_code = '"+ req.body[b].postal_code +"', country = '"+
req.body[b].country +"', \
alternative_name = '"+ req.body[b].alternative_name +"' where
UnitInfo.unit_id=(Select unit_id from DKUnitId \
where hovednummer='"+ req.body[b].hovednummer +"' and leverandoernummer
='"+req.body[b].leverandoernummer+"'and chrnummer
='"+req.body[b].chrnummer+"'); \
else begin INSERT INTO DimGroup (id, type, hovednummer) VALUES ((SELECT
IsNULL(max(id), 0) \
FROM DimGroup WHERE hovednummer!='"+req.body[b].hovednummer+"')+1,
'"+req.body[b].country+"', '"+req.body[b].hovednummer+"'); \
INSERT INTO DimUnit (group_id, type) VALUES ((SELECT id FROM DimGroup
WHERE hovednummer = '"+req.body[b].hovednummer+"'),
'"+req.body[b].country+"'); \
INSERT INTO DKUnitId (unit_id,
hovednummer,leverandoernummer,cvrnummer,chrnummer) VALUES ((SELECT max(id)
FROM DimUnit), \
'"+req.body[b].hovednummer+"',
INSERT INTO UnitInfo
VALUES ((SELECT IsNULL(max(id), 0) FROM DimUnit),
'"+req.body[b].first_name+"', '"+req.body[b].last_name+" END ;"}
request_write_stem = request_write_stem.join('\n');
var request = new Request(request_write_stem,
function(err, rowCount, rows){
console.log(rowCount + ' row(s) returned');
if (err) {
console.log(err)
connection.close();
}
});
request.on('row', function(columns) {
columns.forEach(function(column) {
// console.log("%s\t%s", column.metadata.colName,
column.value);
});
connection.close();
});
connection.execSql(request);
}
// StatusData
if (keys_names_con == statusData_list_con){
for (var b = 0; b < req.body.length; b++){
// COLLECT VALUES!!!
request_write_status[b] = "INSERT INTO FctStatus
(unit_id, group_id,status_count,status_date) \
VALUES ((SELECT max(unit_id) FROM DKUnitId WHERE hovednummer = '"+
req.body[b].hovednummer +"' AND chrnummer = '"+ req.body[b].chrnummer
+"'), (SELECT id FROM DimGroup WHERE hovednummer = '"+
req.body[b].hovednummer +"'), \
"+ req.body[b].status_count +","+ req.body[b].status_date +");" }
request_write_status = request_write_status.join('\n');
var request = new Request(request_write_status,
function(err, rowCount, rows){
console.log(rowCount + ' row(s) returned');
if (err) {
console.log("Error in FctStatus - no corresponding
data with such number(hovednummer) and upload it with StemData");
connection.close();
}
});
request.on('row', function(columns) {
columns.forEach(function(column) {
//console.log("%s\t%s", column.metadata.colName,
column.value);
});
connection.close();
});
connection.execSql(request);
}
//InsertionData
if (keys_names_con == insertionData_list_con){
d = 0;
for (var b = 0; b < req.body.length; b++){
// COLLECT VALUES!!!
for (var c = 0; c < req.body[b].insertions.length;
c++){
t = b + c + d + 1;
//DKUnitId_insert[t-1] = [("(SELECT unit_id FROM
DKUnitId WHERE DKUnitId.hovednummer = '"+req.body[b].hovednummer+"')"),
FctInsertion_insert[t-1] = [("(SELECT
max(unit_id) FROM DKUnitId WHERE DKUnitId.hovednummer =
'"+req.body[b].hovednummer+"' AND chrnummer = '"+ req.body[b].chrnummer
+"')"),
String(""+req.body[b].insertions[c].insertion_date+""),
String(""+req.body[b].insertions[c].delivery_weight+"")];
//DimUnit_insert[t-1] = "INSERT INTO DimUnit
(id,type) VALUES ((SELECT IsNULL(max(id), 0) FROM DimUnit)+1, (SELECT
IsNULL(max(type), 'DK') FROM DimUnit))"
}
d += req.body[b].insertions.length-1;
}
FctInsertion_insert = String("
("+FctInsertion_insert.join('),(')+")");
var request = new Request("INSERT INTO FctInsertion
(unit_id, insertion_date, delivery_weeks, inserted_count, avg_weight,
spread_weight, daily_gain, mortality,delivery_weight)\
VALUES "+ FctInsertion_insert +";", function(err, rowCount, rows){
console.log(rowCount + ' row(s) returned');
if (err) {
console.log(err);
connection.close();
}
});
request.on('row', function(columns) {
columns.forEach(function(column) {
//console.log("%s\t%s", column.metadata.colName,
column.value);
});
connection.close();
});
connection.execSql(request);
}
//SlaughterData
if (keys_names_con == slaughterData_list_con){
d = 0;
for (var b = 0; b < req.body.length; b++){
// COLLECT VALUES!!!
for (var c = 0; c < req.body[b].slaughters.length;
c++){
t = b + c + d + 1;
//DKUnitId_insert[t-1] = [("(SELECT
IsNULL(max(id), 0) FROM DimUnit)+1-"+ t),
String("'"+req.body[b].hovednummer+"'"),
String("'"+req.body[b].leverandoernummer+"'"),
String("'"+req.body[b].chrnummer+"'")];
FctSlaughter_insert[t-1] = [("(SELECT
max(unit_id) FROM DKUnitId WHERE DKUnitId.hovednummer =
'"+req.body[b].hovednummer+"' AND chrnummer = '"+ req.body[b].chrnummer
+"')"),
String(""+req.body[b].slaughters[c].slaughter_id+""),
String(""+req.body[b].slaughters[c].slaughter_date+""),
String(""+req.body[b].slaughters[c].slaughter_weight+""),
String(""+req.body[b].slaughters[c].meat_percent+"")];
// DimUnit_insert[t-1] = "INSERT INTO DimUnit
(id,type) VALUES ((SELECT IsNULL(max(id), 0) FROM DimUnit)+1, (SELECT
IsNULL(max(type), 'DK') FROM DimUnit))";
}
d += req.body[b].slaughters.length-1;
}
FctSlaughter_insert = String("
("+FctSlaughter_insert.join('),(')+")");
var request = new Request("INSERT INTO FctSlaughter
(unit_id,slaughter_id, slaughter_date, slaughter_weight,meat_percent)\
VALUES "+ FctSlaughter_insert +"; SELECT max(id) FROM FctSlaughter;",
function(err, rowCount, rows){
console.log(rowCount + ' row(s) returned');
if (err) {
console.log(err);
connection.close();
}
});
request.on('row', function(columns) {
columns.forEach(function(column) {
//console.log("%s\t%s", column.metadata.colName,
column.value);
});
connection.close();
});
connection.execSql(request);
}
});
}
else {
context.res = {
status: 400,
body: "Input Data is Missing.\n"
};
}
};
预期写入数据库,例如10000行。他们将完成100个任务(每个任务100行)。
代码中可以更改什么?谢谢您的帮助!