[英]oracle node executeMany with dynamic sql query
I am trying to do UPDATE query with executeMany statement.我正在尝试使用 executeMany 语句进行 UPDATE 查询。 I have tried following with execute in async forEach loop like this:我尝试过在异步 forEach 循环中执行如下操作:
export const asyncForEach = async (array, callback) => {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}
then some dynamicDefinitions然后是一些动态定义
export const buildDynamicDefs = (data,enumRef) => {
const defs = {...data};
Object.keys(defs).forEach(key => defs[key] = enums.typeDefs[enumRef][key])
return defs;
};
then finally execute call然后最后执行调用
... ...
await asyncForEach(dataArray, async (device) => {
response[device.deviceId] = await connection.execute(
queryPut(device),
{
bind1,
...device,
},
{
autoCommit: false,
bindDefs: {
bind1,
...buildDynamicDefs(device,'enumName'),
},
},
)
})
and query looks following:查询如下:
export const queryPut = data => `
UPDATE status
SET MANDATORY_PROP1=:bind4
${data.bind2 && ',OPT_PROP1=:bind2' || ''}
${data.bind3 && ',OPT_PROP2=:bind3' || ''}
WHERE MANDATORY_PROP2=:bind1 AND MANDATORY_PROP3=:bind5`;
Problem is that when i use executeMany, i do not know how to tell query how should it looks like问题是当我使用 executeMany 时,我不知道如何告诉查询它应该是什么样子
.executeMany(query,arrayOfObject-binds,options)
There is only one query.只有一个查询。 And my array of objects may looks following:我的对象数组可能如下所示:
[
{
bind1: 1,
bind4: 4,
bind5: 5,
},
{
bind1: 1,
bind2: 2,
bind4: 4,
bind5: 5,
},
]
Is there some possibility to optionally add or include that set OPT_PROP1=:bind2 in sql?是否有可能在 sql 中选择性地添加或包含该设置 OPT_PROP1=:bind2? is there some check for null value of binds?是否对绑定的 null 值进行了一些检查?
With execute it is working, but i dont think it is efficient for big numbers of data.通过执行它可以工作,但我认为它对大量数据无效。
Thanks,谢谢,
After some investigation all i needed to do it was change SQL a bit:经过一番调查,我需要做的就是稍微改变 SQL:
UPDATE status
SET MANDATORY_PROP1=:bind4,
OPT_PROP1=COALESCE(:bind2,OPT_PROP1),
OPT_PROP2=COALESCE(:bind3,OPT_PROP2),
WHERE MANDATORY_PROP2=:bind1 AND MANDATORY_PROP3=:bind5`;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.