Friday 14 April 2017

Correct Nodejs mssql parameterized LIKE query syntax

Using Nodejs and mssql package for MS SQL Server, I understand normal SELECT query with parameters:

aVar = "somestring"; // used as example, the real input is from a web form
request = new sql.Request(conn)
.input('aParam', sql.NVarChar(200), aVar);
q = "SELECT * FROM myTable WHERE theColumn = @aParam";
request.query(q, function(err, record) { ...

works just fine. Changing this to use LIKE, and I cannot get it to work. I've tried single quote before the % character, after, around it, just around the string, the whole thing, etc. Doing this:

q = "SELECT * FROM myTable WHERE theColumn LIKE " + "'%@aParam%'";

does not result in any errors, but returns no results. All other attempts with those single quotes results in query syntax errors. I cant seem to locate any documentation for parameterizing queries with wildcards. Where am I going wrong with this?



via K Jackson

No comments:

Post a Comment