

 O Amazon Redshift não permitirá mais a criação de UDFs do Python a partir do Patch 198. As UDFs do Python existentes continuarão a funcionar normalmente até 30 de junho de 2026. Para ter mais informações, consulte a [publicação de blog ](https://aws.amazon.com/blogs/big-data/amazon-redshift-python-user-defined-functions-will-reach-end-of-support-after-june-30-2026/). 

# Exemplos de INSERT
<a name="c_Examples_of_INSERT_30"></a>

A tabela CATEGORY no banco de dados TICKIT contém as seguintes linhas: 

```
 catid | catgroup |  catname  |                  catdesc
-------+----------+-----------+--------------------------------------------
     1 | Sports   | MLB       | Major League Baseball
     2 | Sports   | NHL       | National Hockey League
     3 | Sports   | NFL       | National Football League
     4 | Sports   | NBA       | National Basketball Association
     5 | Sports   | MLS       | Major League Soccer
     6 | Shows    | Musicals  | Musical theatre
     7 | Shows    | Plays     | All non-musical theatre
     8 | Shows    | Opera     | All opera and light opera
     9 | Concerts | Pop       | All rock and pop music concerts
    10 | Concerts | Jazz      | All jazz singers and bands
    11 | Concerts | Classical | All symphony, concerto, and choir concerts
(11 rows)
```

 Crie uma tabela CATEGORY\$1STAGE com um esquema semelhante ao da tabela CATEGORY, mas defina os valores padrão para as colunas: 

```
create table category_stage
(catid smallint default 0,
catgroup varchar(10) default 'General',
catname varchar(10) default 'General',
catdesc varchar(50) default 'General');
```

A instrução INSERT a seguir seleciona todas as linhas da tabela CATEGORY e as insere na tabela CATEGORY\$1STAGE. 

```
insert into category_stage
(select * from category);
```

Os parênteses em volta da consulta são opcionais.

Este comando insere uma nova linha na tabela CATEGORY\$1STAGE com um valor especificado para cada coluna em ordem: 

```
insert into category_stage values
(12, 'Concerts', 'Comedy', 'All stand-up comedy performances');
```

Você também pode inserir uma nova linha que combina valores específicos e valores padrão: 

```
insert into category_stage values
(13, 'Concerts', 'Other', default);
```

Execute a consulta a seguir para obter as linhas inseridas: 

```
select * from category_stage
where catid in(12,13) order by 1;

 catid | catgroup | catname |             catdesc
-------+----------+---------+----------------------------------
    12 | Concerts | Comedy  | All stand-up comedy performances
    13 | Concerts | Other   | General
(2 rows)
```

Os exemplos a seguir mostram algumas instruções INSERT VALUES de várias linhas. O primeiro exemplo insere valores CATID específicos para duas linhas e valores padrão para outras colunas em ambas as linhas. 

```
insert into category_stage values
(14, default, default, default),
(15, default, default, default);

select * from category_stage where catid in(14,15) order by 1;
 catid | catgroup | catname | catdesc
-------+----------+---------+---------
    14 | General  | General | General
    15 | General  | General | General
(2 rows)
```

O próximo exemplo insere três linhas com várias combinações de valores específicos e valores padrão: 

```
insert into category_stage values
(default, default, default, default),
(20, default, 'Country', default),
(21, 'Concerts', 'Rock', default);

select * from category_stage where catid in(0,20,21) order by 1;
 catid | catgroup | catname | catdesc
-------+----------+---------+---------
     0 | General  | General | General
    20 | General  | Country | General
    21 | Concerts | Rock    | General
(3 rows)
```

Neste exemplo, o primeiro conjunto de VALUES produz os mesmos resultados que especificar DEFAULT VALUES para uma instrução INSERT de linha única.

Os exemplos a seguir mostram o comportamento de INSERT quando uma tabela tem uma coluna IDENTITY. Primeiro, crie uma nova versão de tabela CATEGORY, depois insira linhas de CATEGORY nela: 

```
create table category_ident
(catid int identity not null,
catgroup varchar(10) default 'General',
catname varchar(10) default 'General',
catdesc varchar(50) default 'General');


insert into category_ident(catgroup,catname,catdesc)
select catgroup,catname,catdesc from category;
```

Observe que você não pode inserir valores inteiros específicos na coluna CATID IDENTITY. Os valores da coluna IDENTITY são gerados automaticamente.

O exemplo a seguir demonstra que subconsultas não podem ser usadas como expressões em instruções INSERT VALUES de várias linhas: 

```
insert into category(catid) values
((select max(catid)+1 from category)),
((select max(catid)+2 from category));

ERROR: can't use subqueries in multi-row VALUES
```

O exemplo a seguir mostra uma inserção em uma tabela temporária preenchida com dados da tabela `venue` usando a cláusula `WITH SELECT`. Para obter mais informações sobre a tabela `venue`, consulte [Banco de dados de exemplo](c_sampledb.md).

Primeiro, crie a tabela temporária `#venuetemp`.

```
CREATE TABLE #venuetemp AS SELECT * FROM venue;
```

Liste as linhas na tabela `#venuetemp`.

```
SELECT * FROM #venuetemp ORDER BY venueid;
         
venueid | venuename                | venuecity  | venuestate| venueseats
--------+--------------------------+------------+-----------+------------
1        Toyota Park                Bridgeview   IL          0	
2        Columbus Crew Stadium      Columbus     OH          0	
3        RFK Stadium                Washington   DC          0	
4        CommunityAmerica Ballpark  Kansas City  KS          0	
5        Gillette Stadium           Foxborough   MA          68756	
...
```

Insira dez linhas duplicadas na tabela `#venuetemp` usando a cláusula `WITH SELECT`.

```
INSERT INTO #venuetemp (WITH venuecopy AS (SELECT * FROM venue) SELECT * FROM venuecopy ORDER BY 1 LIMIT 10);
```

Liste as linhas na tabela `#venuetemp`.

```
SELECT * FROM #venuetemp ORDER BY venueid;
         
venueid | venuename                | venuecity  | venuestate| venueseats
--------+--------------------------+------------+-----------+------------
1        Toyota Park                Bridgeview   IL          0	
1        Toyota Park                Bridgeview   IL          0	
2        Columbus Crew Stadium      Columbus     OH          0	
2        Columbus Crew Stadium      Columbus     OH          0	
3        RFK Stadium                Washington   DC          0
3        RFK Stadium                Washington   DC          0	
4        CommunityAmerica Ballpark  Kansas City  KS          0	
4        CommunityAmerica Ballpark  Kansas City  KS          0	
5        Gillette Stadium           Foxborough   MA          68756
5        Gillette Stadium           Foxborough   MA          68756
...
```