|
Ao excluir linhas de uma tabela, você pode selecionar os valores dessas linhas ao mesmo tempo.
When you delete rows from a table, you can select the values from those rows at the same time.
Sobre esta tarefa - About this task
Você pode selecionar valores de linhas que estão sendo excluídas, especificando a instrução DELETE na cláusula FROM da instrução SELECT.
Ao excluir uma ou mais linhas de uma tabela, você pode recuperar:
You can select values from rows that are being deleted by specifying the DELETE statement in the FROM clause of the SELECT statement.
When you delete one or more rows in a table, you can retrieve:
- Quaisquer valores padrão para colunas
Any default values for columns
- Todos os valores de uma linha excluída, sem especificar nomes de colunas individuais
All values for a deleted row, without specifying individual column names
- Valores calculados com base em linhas excluídas
Calculated values based on deleted rows
Ao usar uma instrução SELECT FROM DELETE, você deve usar a cláusula FROM OLD TABLE para recuperar os valores excluídos.
A OLD TABLE consiste nas linhas da tabela ou visão antes de ocorrer a exclusão.
When you use a SELECT FROM DELETE statement, you must use the FROM OLD TABLE clause to retrieve deleted values.
The OLD TABLE consists of the rows of the table or view before the delete occurs.
Exemplo: - Example:
Suponha que uma empresa esteja eliminando todos os cargos de operador e que a empresa queira saber quanto dinheiro de salário ela economizará eliminando esses cargos.
Você pode usar a seguinte instrução SELECT FROM DELETE para excluir operadores da tabela EMP e recuperar a soma dos salários dos operadores.
Suppose that a company is eliminating all operator positions and that the company wants to know how much salary money it will save by eliminating these positions.
You can use the following SELECT FROM DELETE statement to delete operators from the EMP table and to retrieve the sum of operator salaries.
SELECT SUM(SALARY)
INTO :salary
FROM OLD TABLE
(DELETE FROM EMP
WHERE JOB = 'OPERATOR');
Para recuperar a saída linha por linha de dados excluídos, use um cursor com uma instrução SELECT FROM DELETE.
To retrieve row-by-row output of deleted data, use a cursor with a SELECT FROM DELETE statement.
Exemplo: - Example:
Suponha que uma empresa esteja eliminando todas as posições de analista e que a empresa queira saber quantos anos de experiência cada analista teve com a empresa.
Você pode usar a seguinte instrução SELECT FROM DELETE para excluir analistas da tabela EMP e recuperar a experiência de cada analista.
Suppose that a company is eliminating all analyst positions and that the company wants to know how many years of experience each analyst had with the company.
You can use the following SELECT FROM DELETE statement to delete analysts from the EMP table and to retrieve the experience of each analyst.
DECLARE CS1 CURSOR FOR
SELECT YEAR(CURRENT DATE - HIREDATE)
FROM OLD TABLE
(DELETE FROM EMP
WHERE JOB = 'ANALYST');
FETCH CS1 INTO :years_of_service;
Se você precisar recuperar dados calculados, com base nos dados que você excluiu, mas não adicionou essa coluna à tabela de destino.
If you needto retrieve calculated data, based on the data that you delete but not add that column to the target table.
Exemplo: - Example:
Suponha que você precise excluir gerentes da tabela EMP e recuperar o salário e os anos de emprego de cada gerente.
Você pode usar a seguinte instrução SELECT FROM DELETE para executar a operação de exclusão e recuperar os dados necessários.
Suppose that you need to delete managers from the EMP table and that you need to retrieve the salary and the years of employment for each manager.
You can use the following SELECT FROM DELETE statement to perform the delete operation and to retrieve the required data.
DECLARE CS2 CURSOR FOR
SELECT LASTNAME
, SALARY
, years_employed
FROM OLD TABLE
(DELETE FROM EMP INCLUDE(years_employed INTEGER)
SET years_employed = YEAR(CURRENT DATE - HIREDATE)
WHERE JOB = 'MANAGER');
Tópico pai: Deleting data from tables
© Copyright IBM Corp.
|