2

I want to use Google App Scripts to trigger a Google Big Query query as per the docs here:

https://developers.google.com/apps-script/advanced/bigquery#reference

Since my query uses the new DML functionality it needs to use non-legacy SQL.

Is this possible using Google App Script? How do I specify non-legacy SQL?

3
  • I'll have to come back to this - I'm not sure which answer to select as I've encountered a different problem. Posting elsewhere as I suspect it's un-related. Commented Oct 5, 2016 at 15:10
  • Ah, got there sooner than expected. Truth be told I couldn't get Mijhail's and Pentium10's solutions to worked, but not 100% sure I implemented as intended. Elliott's answer has now worked for me. Thanks for all your input. Commented Oct 5, 2016 at 15:15
  • Can anyone volunteer whether this is an advisable way to automate GBQ queries? I've come across Google's CLI, but this so far seems easier. Are there any drawbacks? Commented Oct 5, 2016 at 15:24

4 Answers 4

3

I don't believe that you need to use #StandardSQL in the query text. Using the sample code on https://developers.google.com/apps-script/advanced/bigquery#reference as a reference, it should be sufficient to change:

var request = {
  query: 'SELECT TOP(word, 300) AS word, COUNT(*) AS word_count ' +
    'FROM publicdata:samples.shakespeare WHERE LENGTH(word) > 10;'
};

to:

var request = {
  query: 'SELECT APPROX_TOP_COUNT(word, 300) AS word, COUNT(*) AS word_count ' +
    'FROM `publicdata.samples.shakespeare` WHERE LENGTH(word) > 10;',
  useLegacySql = false
};

Specifically, you need to add the useLegacySql key to the request.

Sign up to request clarification or add additional context in comments.

1 Comment

it's actually useLegacySql : false instead of useLegacySql = false but many thanks it saved me a lot of times
3

Very Minor correction in the accepted answer. (Semicolon needed, not equals symbol)

It should be

useLegacySql : false

And not

useLegacySql = false

2 Comments

this can be a comment to that answer actually
I had tried that initially but I don't have enough points of reputation to comment.
2

It is possible to enforce Standard SQL without code change. Just make sure that the first line of your query is

#StandardSQL

and BigQuery will treat the rest of the query as Standard SQL

Comments

2

Make sure that your first line of your query is

#StandardSQL
select 1;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.