Sqlserver
 sql >> Database >  >> RDS >> Sqlserver

NodeJS MSSQL WHERE IN Istruzione SQL preparata

Sembra che sql oggetto (cioè il mssql module) non ha alcun attributo per gestire array di qualsiasi cosa. Inoltre, specificando un tipo scalare nella chiamata a ps.input allo stesso modo non funziona.

La prossima cosa migliore è creare chiavi per la tua matrice di parametri nella tua stessa istruzione sql:

var connection = new sql.Connection(config, function(err) {
        var ps = new sql.PreparedStatement(connection);
        // Construct an object of parameters, using arbitrary keys
        var paramsObj = params.reduce((obj, val, idx) => {
            obj[`id${idx}`] = val;
            ps.input(`id${idx}`, sql.VarChar(200));
            return obj;
        }, {});
        // Manually insert the params' arbitrary keys into the statement
        var stmt = 'select * from table where id in (' + Object.keys(paramsObj).map((o) => {return '@'+o}).join(',') + ')';
        ps.prepare(stmt, function(err) {
            ps.execute(paramsObj, function(err, data) {
                callback(null, data);
                ps.unprepare(function(err) {
                });
            });
        });
    });
}