Closed SungKyuMM closed 2 years ago
This library is not a query builder, so this kind of thing is not really supported.
If you want to add a dynamic query element like that you should use a manual parameterised query:
const request = pool.request();
request.input('paramName', test);
let query = 'SELECT * FROM table WHERE C = @paramName';
if (condition) {
request.input('anotherParam', test);
query += 'and D = @anotherParam';
}
request.query(query).then((result) => console.log(result));
There are also some docs around the template
method and how that could be used here: #1060, which didn't get merged because I think it's bad practice and shouldn't be encouraged without a very strong understanding of what is going on.
According to the document, this method prevents SQL Injection.
but, sometimes I want to add 'where' conditions. I tried to add 'where conditions', but an error occurs because the prevention of SQL Injection
How do I dynamically add conditions in this way?