How to use IN with a query parameter? - javascript

I have a query like this.
SELECT * FROM player_details where name in ('messi','neymar','suarez','alves')
I want to execute this query in titanium.
I tried like this
var in = ['messi','neymar','suarez','alves'];
db.execute('SELECT * FROM player_details where name in ?',in);
But above code producing error.
How can i add IN and NOT IN condition in sqlite in titanium ?

A single parameter ? replaces a single expression.
When you have four values, you need four parameters:
db.execute('SELECT * FROM player_details where name in (?,?,?,?)', in);

If the length of the array is dynamic try something like this:
var params = ["messi", "neymar", "suarez", "alves"],
qMarks = new Array(params.length).join("?,") + "?";
db.execute("SELECT * FROM player_details WHERE name in (" + qMarks + ");", params);

Related

SQL assign infinite params

Usually we will use SQL queries like below, we will pass params with predifined number $1
queryRunner.query('SELECT * FROM sample_data WHERE code IN ($1)', ['1'])
But I want to pass multiple params without predifined $1. Any way to resolve this?
queryRunner.query('SELECT * FROM sample_data WHERE code IN ($$)', ['1','2','3'])
One approach dynamically builds the IN clause based on the expected number of parameters. Consider:
var params = ['1','2','3'];
var inClause = '?' + ', ?'.repeat(params.length - 1);
var sql = 'SELECT * FROM sample_data WHERE code IN (' + inClause + ')';
console.log(sql);
Once we have a statement with the right number of placeholders, we can simply bind the collection or array with no trouble.

Ignore Case sql query

I am trying to basically ignore the case sensitivity for my db2 sql select * query, so that I can populate the products to my catalogue page. Ex. If I type in my search bar 'squeege', I want the item 'Squeege' to populate, even if there is a difference in Upper/lower case. What is the best way to do this, based on the code I have below?
var searchProduct = "select * from LISTOFPRODUCTS where ITEM LIKE '%" + searchValue + "%'"
Thanks in advance for the help :)
I think this could work:
var searchProduct = "select * from LISTOFPRODUCTS where UPPER(ITEM) LIKE UPPER('%" + searchValue + "%'")
Also the same with LOWER()
Note that the trick is parse both values to UPPER() or LOWER() to match them.
You can use the function LOWER().
For example:
var searchProduct = "select * from LISTOFPRODUCTS where LOWER(ITEM) LIKE '%" + searchValue + "%'"
You can also consider using REGEXP_LIKE which has a case-insensitive option i

Array in sql statement not working with placeholder ?nodejs

sql query does not accept array as value when it is used in a placeholder, It only returns one result even though result is greater than 1. Not using a placeholder and withouting escaping it works perfectly returns the right amount of results.
//works
SELECT * FROM users WHERE userId IN (" + followerIds.join() + ");";
//does not work
SELECT * FROM users WHERE userId IN (?);";
con.query(queryFollowerstTable, [followeringIsd.join()],function(err,result)..
All I had to do was parse followerIds.join() to an int and It worked.
followerIdsParsed = followerIds.join().split(',').map(Number);
followingIdsParsed = followingIds.join().split(',').map(Number);
var queryFollowerstTable = "SELECT * FROM users WHERE userId IN (?); SELECT *
FROM users WHERE userId IN (?);";
con.query(queryFollowerstTable, [followerIdsParsed, followingIdsParsed],
function(err, result) {..
Change
con.query(queryFollowerstTable, [followeringIds.join()],function(err,result)
to
con.query(queryFollowerstTable, followeringIds.join(),function(err,result)
In your original example:
SELECT * FROM users WHERE userId IN (" + followerIds.join() + ");";
You are passing in a string not an array

BIRT report optional parameter script

I am using SpagoBI, and I am trying to create report with optional parameter. I have problem with beforeOpen() script. Here is the query.
select C."CUSTOMERNAME", C."CITY", D."YEAR", P."NAME"
from "CUSTOMER" C, "DAY" D, "PRODUCT" P, "TRANSACTIONS" T
where C."CUSTOMERID" = T."CUSTOMERID"
and D."DAYID" = T."DAYID"
and P."PRODUCTID" = T."PRODUCTID"
and C."REGION" in (?)
and the script
if (params["cityparam"].value != null){
this.queryText = this.queryText + "and C.\"CITY\" in ( ?,'" +params["cityparam"].value + "')";
}
else{
var str = reportContext.getParameterValue("regionparam");
q3 = this.queryText + "and C.\"CITY\" in (?,( select \"CUSTOMER\".\"CITY\" from \"CUSTOMER\" where \"CUSTOMER\".\"REGION\" in ('"+ str +"')))";
this.queryText =q3;
}
I have 2 parameters, regionparam and cityparam the second one is optional. I am trying to modify the query in this way, that when cityparam isn't set, I am comparing C."CITY" to all it's possible values in selected region. The generated query works in my PGadmin. But there are problems in SpagoBI studio. It's says:
Subquery returned more than 1 value. This is not permitted
Is there any BIRT master? I would be greatfull for help. Thanks.
Here is a technique I'm using for handling optional parameters for BIRT reports in SpagoBI. By rewriting the query, we can utilize a single query without modifying it based on parameter values.
Steps
Rewrite the query so that optional parameters may be null OR the database field is equal to some value. For every optional parameter, you'll have two '?' in the query. The first test is against null and the second test is a test for a value to match a field. For required parameters, you'll still only have one '?' in the query.
In the BIRT Data Set's parameters, for optional parameters, define two matching named parameters to correspond to the first and second '?' in the query for that parameter. Required parameters will only have 1 named parameter mapping to them.
Below are simplified samples from an existing report.
Example SQL query (SQLServer) with three optional parameters for user status, last login, and role
SELECT
ar.role_name,
au.user_id,
au.Lname,
au.FName,
au.Email,
au.Last_Login,
au.status,
au.Creation_Date
FROM account_user au WITH (NOLOCK)
INNER JOIN account_role ar WITH (NOLOCK)
ON ar.account_id = au.account_id
AND ar.role_id = au.role_id
WHERE au.account_id = 9999
AND ( (? IS NULL) OR (AU.status = ?) )
AND ( (? IS NULL) OR (AU.last_login <= ?) )
AND ( (? is null) OR (ar.role_id = ?))
ORDER BY role_name, Lname, Fname
Here is what the BIRT Data Set's Parameters look like, for three optional parameters.
Once again I managed to solve my problem. :)
First of all delete You'r optional parameter from data set. We' ll be setting it inside our beforeOpen() script.
Here's my query
select C."CUSTOMERNAME", C."CITY", D."YEAR", P."NAME"
from "CUSTOMER" C, "DAY" D, "PRODUCT" P, "TRANSACTIONS" T
where C."CUSTOMERID" = T."CUSTOMERID"
and D."DAYID" = T."DAYID"
and P."PRODUCTID" = T."PRODUCTID"
and script
if (params["cityparam"].value != null){
this.queryText = this.queryText +"and C.\"REGION\" in (?, '" + params["regionparam"].value + "' ) "
+ "and C.\"CITY\" in ( '" + params["cityparam"].value + "')";
}
else{
var str = reportContext.getParameterValue("regionparam");
this.queryText = this.queryText +"and C.\"REGION\" in (?, '" + params["regionparam"].value + "' ) "
+ "and C.\"CITY\" in (select \"CUSTOMER\".\"CITY\" from \"CUSTOMER\" where \"CUSTOMER\".\"REGION\" in ('"+ str +"'))";
}
As You can see the "?" is necessary only when parameter is declared in data set.
Without it We can compare single row to more than 1 values returned from subquery.

Passing sort order as a parameter

Referring to the sample SQL statement below. I'm able to pass parameter values to the placeholder '?' in the statement. However I'm wondering whether it is possible to pass in the sort order in the same way?
So instead of this:
//Create SQL query
var getAccountsTransactionsStatement = WL.Server.createSQLStatement(
"SELECT transactionId, fromAccount, toAccount, transactionDate, transactionAmount, transactionType " +
"FROM accounttransactions " +
"WHERE accounttransactions.fromAccount = ? OR accounttransactions.toAccount = ? " +
"ORDER BY transactionDate DESC " +
"LIMIT 20;"
);
Can I have this:
//Create SQL query
var getAccountsTransactionsStatement = WL.Server.createSQLStatement(
"SELECT transactionId, fromAccount, toAccount, transactionDate, transactionAmount, transactionType " +
"FROM accounttransactions " +
"WHERE accounttransactions.fromAccount = ? OR accounttransactions.toAccount = ? " +
"ORDER BY ? DESC " +
"LIMIT 20;"
);
And to invoke it:
//Invoke prepared SQL query and return invocation result
function getAccountTransactions1(accountId){
return WL.Server.invokeSQLStatement({
preparedStatement : getAccountsTransactionsStatement,
parameters : [accountId, accountId, transactionDate]
});
}
Two things:
This query piece:
WHERE accounttransactions.fromAccount = ? OR accounttransactions.toAccount = ?
Could be replaced with this:
WHERE ? in (accounttransactions.fromAccount, accounttransactions.toAccount)
No you can't. Parameters are values - kind of static stuff - while column names are not. You could probably work around the issue somehow in limited way by using s.t. like this:
ORDER BY
CASE ?
WHEN 'transactionDate' THEN transactionDate
WHEN 'someotherdate' THEN someotherdate
ELSE DATE '2010-01-01'
END
Note however that's a messy construction. Also depending on the type of the database you're using you might want to cast all the columns into one data type i.e. string. so to_char(transactionDate,'yyyy-mm-dd hh24:mm:ss') might be in order but you need to ensure that the sorting is proper in your case (as number tend to mess stuff up like '2' > '13').

Categories