Utilice la instrucción INSERT … VALUES o la cláusula VALUES TABLE para insertar pequeñas cantidades de datos en una tabla.
Para ejecutar una instrucción INSERT INTO, se requiere el permiso ALTER permission sobre la tabla de destino y el permiso DESCRIBE sobre la tabla de origen. Para obtener información sobre cómo otorgar permisos, consulte Permisos de MaxCompute.
Puede ejecutar los comandos de este tema en las siguientes plataformas:
Funciones
MaxCompute permite utilizar INSERT … VALUES o VALUES TABLE para insertar pequeñas cantidades de datos en una tabla.
Operación | Descripción |
| Durante las pruebas, puede usar
|
| Para cálculos sencillos sobre datos insertados, utilice la cláusula
|
Limitaciones
Cuando utiliza INSERT … VALUES o VALUES TABLE para insertar datos en una tabla, no puede usar INSERT OVERWRITE para especificar las columnas de destino. Debe utilizar INSERT INTO.
Sintaxis
--INSERT … VALUES
INSERT INTO TABLE <table_name>
[PARTITION (<pt_spec>)][(<col1_name> ,<col2_name>,...)]
VALUES (<col1_value>,<col2_value>,...),(<col1_value>,<col2_value>,...),...
--VALUES table
VALUES (<col1_value>,<col2_value>,...),(<col1_value>,<col2_value>,...),<table_name> (<col1_name> ,<col2_name>,...)...
|
Parámetro |
Obligatorio |
Descripción |
|
table_name |
Sí |
El nombre de la tabla en la que se insertarán los datos. La tabla debe existir previamente. |
|
pt_spec |
No |
La partición de destino. El formato es |
|
col_name |
No |
El nombre de una columna de destino. |
|
col_value |
No |
El valor correspondiente a la columna. Separe varios valores con comas (,). Puede ser una constante o una expresión no constante, como una función definida por el usuario o una función integrada. Si no se especifica un valor para una columna, esta tomará NULL como valor predeterminado. |
La cláusula
VALUESno admite literales para tipos de datos complejos, como ARRAY. La inserción de datos de tipo ARRAY requiere una sintaxis diferente. Para más detalles, consulte Example 3.Cuando utilice
VALUESpara escribir datos DATETIME o TIMESTAMP, debe especificar el nombre del tipo enVALUES. Consulte Example 4.
Ejemplos
-
Ejemplo 1: Uso de
INSERT … VALUESpara insertar datos en una partición específica.-- Create a partitioned table named srcp. CREATE TABLE IF NOT EXISTS srcp (key STRING,value BIGINT) PARTITIONED BY (p STRING); -- Add a partition to the srcp table. ALTER TABLE srcp ADD IF NOT EXISTS PARTITION (p='abc'); -- Insert data into the 'abc' partition of the srcp table. INSERT INTO TABLE srcp PARTITION (p='abc') VALUES ('a',1),('b',2),('c',3); -- Query the srcp table. SELECT * FROM srcp WHERE p='abc'; -- The following result is returned. +------------+------------+------------+ | key | value | p | +------------+------------+------------+ | a | 1 | abc | | b | 2 | abc | | c | 3 | abc | +------------+------------+------------+ -
Ejemplo 2: Uso de
INSERT … VALUESpara insertar datos sin especificar un valor de partición.-- Create a partitioned table named srcp. CREATE TABLE IF NOT EXISTS srcp (key STRING,value BIGINT) PARTITIONED BY (p STRING); -- Insert data into the srcp table without specifying a partition value. The partition column is included in the column list. INSERT INTO TABLE srcp PARTITION (p)(key,p) VALUES ('d','20170101'),('e','20170101'),('f','20170101'); -- Query the srcp table. SELECT * FROM srcp WHERE p='20170101'; -- The following result is returned. +------------+------------+------------+ | key | value | p | +------------+------------+------------+ | d | NULL | 20170101 | | e | NULL | 20170101 | | f | NULL | 20170101 | +------------+------------+------------+ -
Ejemplo 3: Uso de una instrucción
INSERTcon una cláusula SELECT para insertar datos de un tipo de dato complejo.-- Create a partitioned table named srcp with a complex data type. CREATE TABLE IF NOT EXISTS srcp (key STRING,value ARRAY<INT>) PARTITIONED BY (p STRING); -- Add a partition to the srcp table. ALTER TABLE srcp ADD IF NOT EXISTS PARTITION (p='abc'); -- Insert data into the 'abc' partition of the srcp table. INSERT INTO TABLE srcp PARTITION (p='abc') SELECT 'a', ARRAY(1, 2, 3); -- Query the srcp table. SELECT * FROM srcp WHERE p='abc'; -- The following result is returned. +------------+------------+------------+ | key | value | p | +------------+------------+------------+ | a | [1,2,3] | abc | +------------+------------+------------+ -
Ejemplo 4: Uso de
INSERT … VALUESpara escribir datos DATETIME o TIMESTAMP. Debe convertir explícitamente el literal anteponiendo el nombre del tipo en la cláusulaVALUES.-- Create a partitioned table named srcp. CREATE TABLE IF NOT EXISTS srcp (key STRING, value TIMESTAMP) PARTITIONED BY (p STRING); -- Add a partition to the srcp table. ALTER TABLE srcp ADD IF NOT EXISTS PARTITION (p='abc'); -- Insert data into the 'abc' partition of the srcp table. INSERT INTO TABLE srcp PARTITION (p='abc') VALUES (datetime'2017-11-11 00:00:00', TIMESTAMP'2017-11-11 00:00:00.123456789'); -- Query the srcp table. SELECT * FROM srcp WHERE p='abc'; -- The following result is returned. +------------+------------+------------+ | key | value | p | +------------+------------+------------+ | 2017-11-11 00:00:00 | 2017-11-11 00:00:00.123 | abc | +------------+------------+------------+ -
Ejemplo 5: Uso de una
VALUES tablepara insertar datos.-- Create a partitioned table named srcp. CREATE TABLE IF NOT EXISTS srcp (key STRING,value BIGINT) PARTITIONED BY (p STRING); -- Insert data into the srcp table. INSERT INTO TABLE srcp PARTITION (p) SELECT concat(a,b), length(a)+length(b),'20170102' FROM VALUES ('d',4),('e',5),('f',6) t(a,b); -- Query the srcp table. SELECT * FROM srcp WHERE p='20170102'; -- The following result is returned. +------------+------------+------------+ | key | value | p | +------------+------------+------------+ | d4 | 2 | 20170102 | | e5 | 2 | 20170102 | | f6 | 2 | 20170102 | +------------+------------+------------+La base de datos infiere los tipos de datos de las columnas (en este caso, STRING y BIGINT) a partir de los valores en la lista
VALUES.VALUES (…), (…) t(a, b)define una tabla temporal llamadatcon las columnasayb. -
Ejemplo 6: Uso de una tabla VALUES para construir una tabla de constantes como alternativa al uso de
SELECT * FROMconUNION ALL.SELECT 1 c UNION ALL SELECT 2 c; -- The preceding statement is equivalent to the following statement: SELECT * FROM VALUES (1), (2) t(c); -- The following result is returned. +------------+ | c | +------------+ | 1 | | 2 | +------------+ -
Ejemplo 7: Uso de una forma especial de
VALUES tablepara insertar datos sin una cláusulaFROM.-- Create a partitioned table named srcp. CREATE TABLE IF NOT EXISTS srcp (key STRING,value BIGINT) PARTITIONED BY (p STRING); -- Insert data into the srcp table. INSERT INTO TABLE srcp PARTITION (p) SELECT ABS(-1), LENGTH('abc'), GETDATE(); -- Query the srcp table. SET odps.sql.allow.fullscan=true; SELECT * FROM srcp; -- Sample result: +------------+------------+------------+ | key | value | p | +------------+------------+------------+ | 1 | 3 | 2024-12-10 16:58:56 | +------------+------------+------------+ -
Ejemplo 8: Uso de expresiones no constantes.
SELECT * FROM VALUES (GETDATE()),(TO_DATE('20190101', 'yyyyMMdd')),(LASTDAY(DATETIME '2019-01-01 01:10:00')) t(d);Se devuelve el siguiente resultado:
+------------+ | d | +------------+ | 2019-01-31 00:00:00 | | 2019-01-01 00:00:00 | | 2024-12-10 16:52:36 | +------------+