0

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]);
};
yuro
  • 2,189
  • 6
  • 40
  • 76
  • I don't beleive that this error is thrown for this piece of the code. You don't have any `as` keyword here – smnbbrv Apr 04 '16 at 09:42
  • @smnbbrv sorry, I have edited my post. This error comes from the AS clause. Because as soon as I execute the code without AS clause, the function works. – yuro Apr 04 '16 at 10:11
  • @Jaco Do you know a better angular module for data exporting to excel? – yuro Apr 04 '16 at 10:24

2 Answers2

1

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]);
Rick
  • 572
  • 1
  • 7
  • 21
  • Many thanks rick! It's very helpful :) Do you know how can I define the width of individual cells? – yuro Apr 19 '16 at 06:57
0

I've found the solution for my problem. It was the space character after the AS clause.

It's just allowed to define one word and not two.

Example:

vm.btnExport = function () {
   alasql('SELECT shortcode AS Short_Code, fname AS fullname INTO XLSX("test.xlsx",{headers:true}) FROM ?', [vm.list]);
};
yuro
  • 2,189
  • 6
  • 40
  • 76