There are many ways of doing this and many packages that can help. I would recommend the Knex.js package. Once you've set that up and made a connection, you can then use the knex.raw function to execute arbitrary SQL and have it returned as a knex object. I'm not sure of the specific SQL syntax for MSSQL, but it should be very similar to Postgres where you would do something like:
knex.raw('select * from my_func(?, ?)', [valOne, valTwo]);
In the above example I am running a select query against a stored procedure called my_func
. I am them passing in a question mark for each parameter, and then matching those up in an array after the string. This will result in the SQL being executed.
select * from my_funct(valOne, valTwo);
This includes escaping values to help defend against things such as SQL injection.
Your execution syntax may be slightly different in MSSQL, but you can still use knex.raw
and the question mark + array syntax to inject values into a prepared statement like this.