|
Ao atualizar linhas em uma tabela, você pode selecionar os valores atualizados dessas linhas ao mesmo tempo.
When you update rows in a table, you can select the updated values from those rows at the same time.
Sobre esta tarefa - About this task
Você pode selecionar valores de linhas que estão sendo atualizadas, especificando a instrução UPDATE na cláusula FROM da instrução SELECT.
Ao atualizar uma ou mais linhas em uma tabela, você pode recuperar:
You can select values from rows that are being updated by specifying the UPDATE statement in the FROM clause of the SELECT statement.
When you update one or more rows in a table, you can retrieve:
- O valor de uma coluna gerada automaticamente, como um ROWID ou coluna de identidade
The value of an automatically generated column such as a ROWID or identity column
- Quaisquer valores padrão para colunas
Any default values for columns
- Todos os valores para uma linha atualizada, sem especificar nomes de colunas individuais
All values for an updated row, without specifying individual column names
Na maioria dos casos, você deseja usar a cláusula FINAL TABLE com as instruções SELECT FROM UPDATE.
A TABELA FINAL consiste nas linhas da tabela ou visão após a atualização ocorrer.
In most cases, you want to use the FINAL TABLE clause with SELECT FROM UPDATE statements.
The FINAL TABLE consists of the rows of the table or view after the update occurs.
Exemplo: - Example:
Suponha que todos os funcionários de uma empresa estejam recebendo aumentos de 5%.
Você pode usar a seguinte instrução SELECT FROM UPDATE para aumentar o salário de cada designer em 5 por cento e para recuperar o aumento total no salário da
empresa.
Suppose that all clerks for a company are receiving 5 percent raises.
You can use the following SELECT FROM UPDATE statement to increase the salary of each designer by 5 percent and to retrieve the total increase in salary for the
company.
SELECT SUM(SALARY)
INTO :salary
FROM FINAL TABLE
(UPDATE EMP SET SALARY = SALARY * 1.05
WHERE JOB = 'DESIGNER');
Para recuperar a saída linha por linha de dados atualizados, use um cursor com uma instrução SELECT FROM UPDATE.
To retrieve row-by-row output of updated data, use a cursor with a SELECT FROM UPDATE statement.
Exemplo: - Example:
Suponha que todos os designers de uma empresa estejam recebendo um aumento de 30% em seu bônus.
Você pode usar a seguinte instrução SELECT FROM UPDATE para aumentar o bônus de cada funcionário em 30 por cento e recuperar o bônus de cada funcionário.
Suppose that all designers for a company are receiving a 30 percent increase in their bonus.
You can use the following SELECT FROM UPDATE statement to increase the bonus of each clerk by 30 percent and to retrieve the bonus for each clerk.
DECLARE CS1 CURSOR FOR
SELECT LASTNAME
, BONUS
FROM FINAL TABLE
(UPDATE EMP SET BONUS = BONUS * 1.3
WHERE JOB = 'CLERK');
FETCH CS1 INTO :lastname, :bonus;
Você pode usar a cláusula INCLUDE para introduzir uma nova coluna na tabela de resultados, mas não adicionar a coluna à tabela de destino.
You can use the INCLUDE clause to introduce a new column to the result table but not add the column to the target table.
Exemplo: - Example:
Suponha que os representantes de vendas recebam um aumento de 20% em sua comissão.
Você precisa atualizar a comissão (COMM) dos representantes de vendas (SALESREP) na tabela EMP e que você precisa recuperar a comissão antiga e a nova comissão
para cada representante de vendas.
Você pode usar a seguinte instrução SELECT FROM UPDATE para realizar a atualização e recuperar os dados necessários.
Suppose that sales representatives received a 20 percent increase in their commission.
You need to update the commission (COMM) of sales representatives (SALESREP) in the EMP table and that you need to retrieve the old commission and the new
commission for each sales representative.
You can use the following SELECT FROM UPDATE statement to perform the update and to retrieve the required data.
DECLARE CS2 CURSOR FOR
SELECT LASTNAME
, COMM
, old_comm
FROM FINAL TABLE
(UPDATE EMP INCLUDE(old_comm DECIMAL (7,2))
SET COMM = COMM * 1.2
, old_comm = COMM
WHERE JOB = 'SALESREP');
Parent topic: Updating table data
© Copyright IBM Corp.
|