I'm writing in SQL and I want to copy some data into a new table. I want 3 variables from existingtable
to go into a new table called newtable
. I am running the following query:
SELECT var1, var2, v3
INTO `myproject.mydataset.newtable`
FROM `myproject.mydataset.existingtable`;
I get the error message:
Syntax error: Expected end of input but got keyword INTO at [2:1]
My code seems identically structured to some tutorial articles such as the one here. What am I doing wrong? I'm sorry I know there are a lot of similar posts with this basic error message but I have followed the advice from many different articles and haven't been able to solve it as the error message is so vague.
In GoogleSQL, the SELECT
statement is query only. It will not update a table. What you need is an INSERT
. For example:
INSERT INTO `myproject.mydataset.newtable` (var1, var2, var3)
SELECT var1, var2, var3 FROM `myproject.mydataset.existingtable`
If the table doesn't already exist, use:
CREATE TABLE `myproject.mydataset.newtable` (
var1 STRING,
var2 STRING,
var3 STRING) AS
SELECT var1, var2, var3 FROM `myproject.mydataset.existingtable`
Here is what I used to test:
CREATE TABLE test.table1 (col1 string, col2 string, col3 string);
INSERT INTO test.table1 VALUES ("A", "B", "C"), ("D", "E", "F");
CREATE TABLE test.table2 (col1 string, col2 string, col3 string);
INSERT INTO test.table2 (col1, col2,col3) SELECT col1, col2, col3 FROM test.table1
Looking at the link you pasted and reading the intro, that seems to use mySQL as the database. What you will find is that databases support different variants of the SQL language. BigQuery claims to be ANSI compliant. What this means is that if you restrict your usage of SQL to just that as defined to be standard ANSI SQL you will have portability.