|
Você pode copiar uma ou mais linhas de uma tabela para outra.
You can copy one or more rows from one table into another table.
Sobre esta tarefa - About this task
Use uma seleção completa em uma instrução INSERT para selecionar linhas de uma tabela para inserir em outra tabela.
Use a fullselect within an INSERT statement to select rows from one table to insert into another table.
Exemplo: - Example:
A seguinte instrução SQL cria uma tabela chamada TELE:
The following SQL statement creates a table named TELE:
CREATE TABLE TELE
(NAME2 VARCHAR(15) NOT NULL
, NAME1 VARCHAR(12) NOT NULL
, PHONE CHAR(4));
A seguinte instrução copia dados de DSN8C10.EMP para a tabela recém-criada:
The following statement copies data from DSN8C10.EMP into the newly created table:
INSERT INTO TELE
SELECT LASTNAME
, FIRSTNME
, PHONENO
FROM DSN8C10.EMP
WHERE WORKDEPT = 'D21';
As duas instruções anteriores criam e preenchem uma tabela, TELE, que se parece com a seguinte tabela:
The two previous statements create and fill a table, TELE, that looks similar to the following table:
NAME2 NAME1 PHONE
=============== ============ =====
PULASKI EVA 7831
JEFFERSON JAMES 2094
MARINO SALVATORE 3780
SMITH DANIEL 0961
JOHNSON SYBIL 8953
PEREZ MARIA 9001
MONTEVERDE ROBERT 3780
O exemplo da instrução CREATE TABLE cria uma tabela que, a princípio, está vazia.
A tabela possui colunas para sobrenomes, nomes e números de telefone, mas não possui nenhuma linha.
The CREATE TABLE statement example creates a table which, at first, is empty.
The table has columns for last names, first names, and phone numbers, but does not have any rows.
A instrução INSERT preenche a tabela recém-criada com dados selecionados na tabela DSN8C10.EMP: os nomes e números de telefone dos funcionários no departamento D21.
The INSERT statement fills the newly created table with data that is selected from the DSN8C10.EMP table: the names and phone numbers of employees in department
D21.
Exemplo: - Example:
A seguinte instrução CREATE cria uma tabela que contém o nome do departamento e o número de telefone de um funcionário.
A seleção completa dentro da instrução INSERT preenche a tabela DLIST com dados de linhas selecionadas de duas tabelas existentes, DSN8C10.DEPT e DSN8C10.EMP.
The following CREATE statement creates a table that contains an employee's department name and phone number.
The fullselect within the INSERT statement fills the DLIST table with data from rows that are selected from two existing tables, DSN8C10.DEPT and DSN8C10.EMP.
CREATE TABLE DLIST
(DEPT CHAR(3) NOT NULL
, DNAME VARCHAR(36)
, LNAME VARCHAR(15) NOT NULL
, FNAME VARCHAR(12) NOT NULL
, INIT CHAR
, PHONE CHAR(4) );
INSERT INTO DLIST
SELECT DEPTNO
, DEPTNAME
, LASTNAME
, FIRSTNME
, MIDINIT
, PHONENO
FROM DSN8C10.DEPT
, DSN8C10.EMP
WHERE DEPTNO = WORKDEPT;
Parent topic: Inserting rows by using the INSERT statement
© Copyright IBM Corp.
|