how to use the SQL 'IN' operator with an array?

Find out how to get things done with Servoy. Post how YOU get things done with Servoy

how to use the SQL 'IN' operator with an array?

Postby mboegem » Mon Dec 08, 2008 3:22 pm

I hope anyone can help me out on this:

I have some values in an array and want to use them in a query, using the 'IN' operator.
I've been struggling around and ended up converting the array to literal text and add this to my query string:
Code: Select all
function getRecordsWorking()
{
var $array = new Array(globals.idA ,globals.idB)
var $param = '(\'' + $array.join('\',\'') + '\')';
var $query = '';
$query += 'SELECT *' + ' ';
$query += 'FROM dummy' + ' ';
$query += 'WHERE k_owner_id IN ' + $param;
var $server = 'myserver';
var $max = -1;

var $dataset = databaseManager.getDataSetByQuery($server, $query, null, $max);
var $countRec = $dataset.getMaxRowIndex();
}


Isn't there anything like this that can be used????:
Code: Select all
function getRecordsNotWorking()
{
var $array = new Array(globals.idA ,globals.idB)

var $query = '';
$query += 'SELECT *' + ' ';
$query += 'FROM dummy' + ' ';
$query += 'WHERE k_owner_id IN (?)';
var $server = 'myserver';
var $max = -1;
var $args = new Array();
$args[0] = $array;

var $dataset = databaseManager.getDataSetByQuery($server, $query, $args, $max);
var $countRec = $dataset.getMaxRowIndex();
}


Maybe another easier syntax than the one I use now???

Thnkx a lot!
Marc Boegem
Solutiative / JBS Group, Partner
Servoy Specialist
• Servoy Certified Developer
• Servoy Valued Professional
• Freelance Developer

Image
User avatar
mboegem
 
Posts: 1750
Joined: Sun Oct 14, 2007 1:34 pm
Location: Amsterdam

Re: how to use the SQL 'IN' operator with an array?

Postby pbakker » Mon Dec 08, 2008 4:00 pm

Best is to supply the values in your array as an arguments array.

But, then you need to insert the proper amount of questionmarks into your SQL statement to match the number of arguments.

Quickest way I found to get a string with the right number of question marks is this:
Code: Select all
$array.map(function() {return "?"}).join(",")


Source: http://www.javascriptkit.com/jsref/arrays.shtml
Requires Servoy 4.0.0 or higher


Paul
pbakker
 
Posts: 2822
Joined: Wed Oct 01, 2003 8:12 pm
Location: Amsterdam, the Netherlands

Re: how to use the SQL 'IN' operator with an array?

Postby mboegem » Tue Dec 09, 2008 12:24 pm

Thnkx Paul! This is definitely shorter /more readable than what I figured out...
Marc Boegem
Solutiative / JBS Group, Partner
Servoy Specialist
• Servoy Certified Developer
• Servoy Valued Professional
• Freelance Developer

Image
User avatar
mboegem
 
Posts: 1750
Joined: Sun Oct 14, 2007 1:34 pm
Location: Amsterdam

Re: how to use the SQL 'IN' operator with an array?

Postby pbakker » Tue Dec 09, 2008 12:36 pm

I dare people to come up with an even shorter notation to get the same thing done :D

Paul
pbakker
 
Posts: 2822
Joined: Wed Oct 01, 2003 8:12 pm
Location: Amsterdam, the Netherlands

Re: how to use the SQL 'IN' operator with an array?

Postby david » Tue Feb 10, 2009 9:23 pm

pbakker wrote:I dare people to come up with an even shorter notation to get the same thing done :D


I'm just starting to get my head around some of the JavaScript contortions Servoy 4 allows us to utilize. Thanks for a REALLY cool example.
David Workman, Kabootit

Image
Everything you need to build great apps with Servoy
User avatar
david
 
Posts: 1727
Joined: Thu Apr 24, 2003 4:18 pm
Location: Washington, D.C.


Return to How To

Who is online

Users browsing this forum: No registered users and 3 guests