# CTE

Especifica um conjunto de resultados nomeado temporário, conhecido como uma CTE (expressão de tabela comum). Ela é derivada de uma consulta simples e definida no escopo de execução de uma única instrução SELECT, INSERT, UPDATE, DELETE ou MERGE.

Selecioando dados com CTE

```sql
WITH cte_clientes
as
(
SELECT ClienteCodigo 
FROM Clientes
),
cte_cartao
as
(
SELECT ClienteCodigo 
FROM CartaoCredito
)

select cte_cartao.ClienteCodigo, cte_clientes.ClienteCodigo 
from cte_clientes inner join cte_cartao
on cte_cartao.ClienteCodigo=cte_clientes.ClienteCodigo
```

Atualiznado dados usadno CTE

```sql
WITH cte_clientes
as
(
SELECT ClienteCodigo 
FROM Clientes
),
cte_cartao
as
(
SELECT ClienteCodigo 
FROM CartaoCredito
)

UPDATE cte_clientes set ClienteCodigo = cte_clientes.ClienteCodigo 
from cte_clientes inner join cte_cartao
on cte_cartao.ClienteCodigo=cte_clientes.ClienteCodigo
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://rdornel.gitbook.io/material/linguagem-sql/cte.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
