简体   繁体   中英

How to input declared variable in js into mssql query in node.js?

I have introduced some variables a,b and c as follows in my code

var a = 10; var b = 90; var c = b - a + 1;

req2.query('SELECT TOP(81) [Numbers], [Square_Root] FROM Kiso_task_table WHERE Numbers   >=  10  AND Numbers <= 90', function (err, data) {
        if (err) {
            console.log(err);
            return;
        }
        else {
            console.log(data);
        }
        conn.close();
    });

I don't want to enter my data into query by "typing them from my finger". To be more precise, instead of req2.query('SELECT TOP(81) I want to have req2.query('SELECT TOP(c) , where variable c is already defined with assigned value.

You need to make a parameterized query.

req2.query(`SELECT TOP(${c}) [Numbers], [Square_Root] FROM Kiso_task_table WHERE Numbers >=  ${a}  AND Numbers <= ${b}`, function (err, data) {
        if (err) {
            console.log(err);
            return;
        }
        else {
            console.log(data);
        }
        conn.close();
    });
req2.query('SELECT TOP('+ c +')[Numbers], [Square_Root] FROM Kiso_task_table WHERE Numbers >=' + a +  'AND Numbers <=' + b, function (err, data) {
        if (err) {
            console.log(err);
            return;
        }
        else {
            console.log(data);
        }
        conn.close();
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM