Just figured out how to work this. request.input()
accepts three parameters described below from mssql docs:
input (name, [type], value) Add an input parameter to the request.
Arguments
name - Name of the input parameter without @ char.
type - SQL data type of input parameter. If you omit type, module
automatically decide which SQL data type should be used based on JS
data type.
value - Input parameter value. undefined ans NaN values are
automatically converted to null values.
So the way you'd use this is as follows;
let id= 123456
const stmt = 'SELECT * FROM Users WHRE User=@userId'
request.input('userId', sql.Int, id);
request.query(stmt)
Note that:
id
is the variable that holds my value and is used as the third parameter in request.input(name, type, value)
.
userId
is just a name I've specified for the variable that will be used in the sql statement using @userId
. In the request.input()
, id
value is assigned to userId
in the sql statement.
- Lastly
sql.Int
is just a data type that will be used to validate the date coming into userId
. You use this by importing sql in the same file: const sql = require('mssql')