-
Notifications
You must be signed in to change notification settings - Fork 664
User Defined Functions
Mathias Rangel Wulff edited this page Nov 24, 2015
·
18 revisions
To define new functions for SQL simply add it to alasql.fn
variable, like below:
alasql.fn.cube = function(x) { return x*x*x; }
alasql(‘SELECT cube(x) FROM ?’,[data]);
alasql.fn.double = function(x){return x*2};
alasql.fn.sum10 = function(x,y) { return x+y*10; }
alasql('SELECT a, double(a) AS b, sum10(a,b) FROM test1');
You can use alasql inside alasql functions, like below:
alasql.fn.myfilter = function(phase) {
return alasql('SELECT VALUE COUNT(*) FROM ? WHERE Phase = ?',[data,phase]) == 2;
};
var res = alasql('SELECT * FROM ? WHERE myfilter(Phase)',[data]);
See the working example in jsFiddle
© 2014-2024, Andrey Gershun & Mathias Rangel Wulff
Please help improve the documentation by opening a PR on the wiki repo