使用节点js将多个列和行插入SQL Server [英] Insert multiple columns and rows into SQL Server with node js

查看:60
本文介绍了使用节点js将多个列和行插入SQL Server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个从nosql读取并插入到SQL Server数据库中的脚本.

I'm trying to to make a script that reads from a nosql and inserts into a SQL Server database.

那表示我正在动态地阅读馆藏,所以我需要做一些事情来做

That said I'm reading collections dynamically, so I need something to do things like

var columns = [ 1, 2, 3, 4 ...]
var values = [a, b, c ,4 ...]
request.query("INSERT INTO TABLE (" + [columns] + ") VALUES ( " [values] ");"

我有一些集合最多有27列,我无法通过插入每个值来占用数据库,因为我有20.000.000个寄存器要执行...在事务内找不到能做到这一点的任何东西,所以我将不胜感激

I have some collections with up to like 27 columns and I can't hog the database by inserting each value as I have like 20.000.000 registers to do... can't find anything that can do that inside a transaction, so I would appreciate any suggestions

推荐答案

首先从npm下载sql服务器

first download sql server from npm

npm安装mssql

npm install mssql

然后尝试这样:

var sql = require("mssql");

    // config for your database
    var config = {
        user: 'sa',
        password: 'mypassword',
        server: 'localhost', 
        database: 'SchoolDB' 
    };

    // connect to your database
    sql.connect(config, function (err) {

        if (err) throw err;
          console.log("Connected!");
          var sql = "INSERT INTO customers (name, address) VALUES ?";
          var values = [
                    ['John', 'Highway 71'],
                        ['Peter', 'Lowstreet 4'],
                        ['Amy', 'Apple st 652'],
                        ['Hannah', 'Mountain 21'],
                        ['Michael', 'Valley 345'],
                        ['Sandy', 'Ocean blvd 2'],
                        ['Betty', 'Green Grass 1'],
                        ['Richard', 'Sky st 331'],
                        ['Susan', 'One way 98'],
                        ['Vicky', 'Yellow Garden 2'],
                        ['Ben', 'Park Lane 38'],
                        ['William', 'Central st 954'],
                        ['Chuck', 'Main Road 989'],
                        ['Viola', 'Sideway 1633']
                      ];
              con.query(sql, [values], function (err, result) {
                if (err) throw err;
                console.log("Number of records inserted: " + result.affectedRows);   }); });

这篇关于使用节点js将多个列和行插入SQL Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆