Search code examples
javascriptsqlangularjsexport-to-excelalasql

Using "AS" clause in SQL statement in alasql (AngularJS)


As the title describes, I have the problem, that I'm getting an error message as soon as I try to use the AS clause in the sql statement of the angular module alasql.

The following error message will displayed:

Error: Parse error on line 1: ... shortcode AS Short code, fname AS fullname -----------------------^ Expecting 'EOF', 'WITH', 'COMMA', 'RPAR', 'PIVOT', 'UNPIVOT', 'REMOVE', 'ORDER', 'WHERE', 'UNION', 'INTERSECT', 'EXCEPT', 'FROM', 'INTO', 'GROUP', 'LIMIT', 'OFFSET', 'END', 'ELSE', 'SEMICOLON', 'GO', got 'LITERAL'

I'm not sure, what I'm doing wrong. In the wiki, they also used the AS clause without problems.

My angular code is very simple as shown below:

vm.btnExport = function () {
   alasql('SELECT shortcode AS Short code, fname AS fullname INTO XLSX("test.xlsx",{headers:true}) FROM ?', [vm.list]);
};

Solution

  • Most if not all databases do not allow spaces in table, column names or alias names. In Alasql use square brackets []. So for your case you could rename Short Code to Short_Code or you could use [Short Code]. However... in Alasql there are a lot of keywords. So to be safe I put square brackets around everything that is not a keyword.

    alasql('SELECT [shortcode] AS [Short code], [fname] AS [fullname] INTO XLSX("test.xlsx",{headers:true}) FROM ?', [vm.list]);