首页 > 解决方案 > Nodejs mssql批量插入RequestError:无效的对象名称't​​able_name'

问题描述

我正在尝试使用 nodejs mssql 包批量插入现有表。即使我的表已经存在,它也会给出错误,因为无效对象名称“my_test”并且我已经尝试了“db.schema.tablename”或“schema.tablename”两者。请帮助我,并提前致谢。我的代码部分如下 - :

async function getPool(name) {
  if (!Object.prototype.hasOwnProperty.call(pools, name)) {
    const pool = process.env.NODE_ENV.trim() === 'local' ? new sql.ConnectionPool(configLocal) : new sql.ConnectionPool(config);
    const close = pool.close.bind(pool);
    pool.close = (...args) => {
      delete pools[name]
      return close(...args)
    }
    await pool.connect();
    pools[name] = pool;
  }
  return pools[name];
}

const pool = await getPool('default');
        const table = new sql.Table('my_test'); // or temporary table, e.g. #temptable
        table.create = false;
        table.columns.add('id', sql.Int, { nullable: false,primary:true,identity:true});
        table.columns.add('name', sql.VarChar(50), { nullable: false });
        table.rows.add(10, 'test');
        table.rows.add(11, 'test 2');
        const request = new sql.Request(pool);
        request.bulk(table, (err, result) => {
            console.log("Result  ", result, err);//return result;
        }); ```
 

标签: javascriptnode.jssql-serverbulkinsert

解决方案


推荐阅读