Insert data into BigQuery from a Google Script : Encountered “”

↘锁芯ラ 提交于 2019-12-04 06:06:36

问题


I am trying to import data from a Google Spreadsheet to BigQuery, via Google App Script. I can download data, but I have an error when I try to do INSERT INTO. The error message is

Encountered "" at line 1, column 31. [Try using standard SQL (https://cloud.google.com/bigquery/docs/reference/standard-sql/enabling-standard-sql)]

Here is my code :

function insertRowsInBigQuery(){
var projectId = 'xxx';
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getSheetByName("EPC");

var data = sheet.getRange(2, 1, sheet.getLastRow() - 1, 2).getValues();
var sqlData = "INSERT INTO ean.eanToAnalyze (EPC, EAN) VALUES (";

for (i = 0; i < data.length; i++){
  if (i > 0) {
    sqlData += ",(";
  }
  for (j = 0; j < data[0].length; j++){
    sqlData += "'" + data[i][j] + "'";
    if (j < data[0].length -1){
      sqlData += ",";
    } else {
      sqlData +=")";
    }
  }     
}

var request = {
  query: sqlData,
  writeDisposition: 'WRITE_TRUNCATE'
};

var queryResults = BigQuery.Jobs.query(request, projectId);
var jobId = queryResults.jobReference.jobId;

// Check on status of the Query Job.
var sleepTimeMs = 500;
while (!queryResults.jobComplete) {
  Utilities.sleep(sleepTimeMs);
  sleepTimeMs *= 2;
  queryResults = BigQuery.Jobs.getQueryResults(projectId, jobId);
}

Logger.log(queryResults);

}

With almost the same function, only changing the INSERT INTO by a SELECT, I get data.


回答1:


As the error states, you need to enable standardSQL to use DML. The easiest way seems to be by prefixing your query:

var sqlData = "#standardSQL\nINSERT INTO ean.eanToAnalyze (EPC, EAN) VALUES (";


来源:https://stackoverflow.com/questions/56652895/insert-data-into-bigquery-from-a-google-script-encountered

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!