Loopback "params" for running native SQL queries
Asked Answered
S

1

5

Anybody know what "params" is/are for the Loopback docs here:

https://docs.strongloop.com/display/public/LB/Executing+native+SQL

it says:

Executing native SQL

To execute SQL directly against your data-connected model, use the following:

dataSource.connector.execute(sql, params, cb); 

or

dataSource.connector.query(sql, params, cb); // For 1.x connectors

Where: sql - The SQL string. params - parameters to the SQL statement. cb - callback function

Schwenk answered 21/10, 2016 at 19:18 Comment(0)
A
11

That is an array of values of your SQL string params. For example if you have postgresql database and parametrized query like this:

select * from table where id = $1 or name = $2

then you have to provide parameter values to your function, so you will do something like this:

var query = "select * from table where id = $1 or name = $2";
var params = [82, "My name"];
ds.connector.execute(query, params, function(err, data){
  if(err){
    console.log( err);
  }else{
    console.log(data);
  }
});
Artery answered 24/10, 2016 at 8:5 Comment(3)
AZ is one of my fav rappers btw :)Schwenk
How about MS SQL ? i tried both ? and $1 also doesn't work.Timotheus
MS SQL uses @param1 instead of $1Undemonstrative

© 2022 - 2024 — McMap. All rights reserved.