nodejs mmsql bulk insert to temp table

懵懂的女人 提交于 2019-12-07 15:49:32

I switched over to a regular sql statement to create a temporary table, and then tried to query it. What I realized was the new sql.Request was most of my problem. The temporary table doesn't exist on another connection/request/thread.

So this was my solution:

var table = new sql.Table('#atable');
    table.create = true;
    table.columns.add('a', sql.Int, { nullable: false});
    table.rows.add(1);
    table.rows.add(2);
    table.rows.add(3);
    var request = new sql.Request();
    request.bulk(table, function(err, rowCount){
       if(err){
           console.log('bulk insert error');
           console.log(err);
           return;
       } 

        request.query('select * from #atable', function(err, recordset){
       if(err){
           console.log('taco error:' + err);
           return;
       } 
       console.log('taco recordset:');
       console.log(recordset);
    });
    });

It's a subtle change. Inside the request.bulk call I used the same request object (smartly named request) to query the table. I wasn't creating a new connection object so I didn't realize I was using a connection where the table didn't exist.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!