insert into

Insert new records to a table.

Note: To insert values into DFS tables, please configure enableInsertStatementForDFSTable=true.

Syntax

insert into
  table_name1
  values (X, [Y, ...]) | select col_name(s) from table_name2

Here, colName specifies the column name in the target table, which can be in one of the following three forms:

  • Unquoted column name colName

  • Column name enclosed in double quotes "colName"

  • Double-quoted column name prefixed with an underscore _"colName"

Examples

Insert using VALUE clause

t=table(`XOM`GS`FB as ticker, 100 80 120 as volume);
t;
ticker volume
XOM 100
GS 80
FB 120
insert into t values(`GOOG, 200);
t;
ticker volume
XOM 100
GS 80
FB 120
GOOG 200
insert into t values(`AMZN`NFLX, 300 250);
t;
ticker volume
XOM 100
GS 80
FB 120
GOOG 200
AMZN 300
NFLX 250
insert into t values(('AMD','NVDA'), (60 400));
t;
ticker volume
XOM 100
GS 80
FB 120
GOOG 200
AMZN 300
NFLX 250
AMD 60
NVDA 400

The above code example can also be written in a way that follows the ANSI SQL standard for inserting multiple rows into the table directly. This alternative approach yields the same result.

insert into t values ('AMD', 60), ('NVDA', 400);
t;
ticker volume
XOM 100
GS 80
FB 120
GOOG 200
AMZN 300
NFLX 250
AMD 60
NVDA 400

To only insert values for a subset of columns:

insert into t(ticker, volume) values(`UBER`LYFT, 0 0);
t;
ticker price volume
XOM 98.5 100
GS 12.3 80
FB 40.6 120
GOOG 100.6 200
AMZN 120 300
NFLX 56.6 250
AMD 78.6 60
NVDA 33.1 400
UBER 0
LYFT 0

Insert from query results using SELECT clause

t1 = table(`XOM`GS`FB as ticker, 100 80 120 as volume)
t1

ticker

volume

XOM 100
GS 80
FB 120
t2 = table(`GOOG`AMZN`NFLX as ticker, 100 80 120 as volume);
t2

ticker

volume

GOOG 110
AMZN 90
NFLX 150
insert into t1(ticker,volume) select ticker,volume from t2

ticker

volume

XOM 100
GS 80
FB 120
GOOG 110
AMZN 90
NFLX 150

Inserting all columns in the table is equivalent to the following two expressions

insert into t1 select * from t2
insert into t1 t2