0

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.

1
  • SELECT TOP(81)... will give you up to 81 rows. I'm honestly not 100% sure what you are trying to do. Are you trying to concatenate the variables (they don't seem to appear in your code anywhere)? Can you edit the post and add the expected results? Commented Nov 9, 2018 at 13:44

2 Answers 2

1

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();
    });
Sign up to request clarification or add additional context in comments.

6 Comments

Having introduced those changes, console displays " RequestError: Syntax error, permission violation, or other nonspecific error at StreamEvents.req.once.err"
Undeleted... I updated my answer with more code. I'm assuming you're using the mssql npm package to query SQL Server. Can you confirm that's the case?
Exactly, I'm using mssql npm package
Argh, sorry... I put single quotes in instead of backticks. I've updated the answer. Give it another try.
Ok. Not sure what the issue is, it works on my side.
|
0
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();
    });

1 Comment

While this works, don't do this. You open yourself up to SQL injection attacks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.