DB2 12 - Preserving the order of a derived table


Volta a página anterior

Volta ao Menu Principal


Desenvolvido por DORNELLES, Carlos Alberto - Analista de Sistemas - Brasília DF. - cad_cobol@hotmail.com

DB2 12 - Preservando a ordem de uma tabela derivada - Preserving the order of a derived table

Ao especificar SELECT FROM INSERT, SELECT FROM UPDATE, SELECT FROM DELETE ou SELECT FROM MERGE, você pode preservar a ordem da tabela derivada.
Esta ação garante que as linhas de resultados de uma seleção completa sigam a mesma ordem que a tabela de resultados de uma subconsulta na seleção completa.

When you specify SELECT FROM INSERT, SELECT FROM UPDATE, SELECT FROM DELETE, or SELECT FROM MERGE, you can preserve the order of the derived table.
This action ensures that the result rows of a fullselect follow the same order as the result table of a subquery within the fullselect.

Sobre esta tarefa - About this task

Para preservar a ordem da tabela derivada, especifique a cláusula ORDER OF com a cláusula ORDER BY.
Essas duas cláusulas garantem que as linhas de resultados de uma seleção completa sigam a mesma ordem que a tabela de resultados de uma subconsulta na seleção completa.

To preserve the order of the derived table specify the ORDER OF clause with the ORDER BY clause.
These two clauses ensure that the result rows of a fullselect follow the same order as the result table of a subquery within the fullselect.

Você pode usar a cláusula ORDER OF em qualquer consulta que use uma cláusula ORDER BY, mas a cláusula ORDER OF é mais útil com consultas que contêm um operador de conjunto, como UNION.
You can use the ORDER OF clause in any query that uses an ORDER BY clause, but the ORDER OF clause is most useful with queries that contain a set operator, such as UNION.

Exemplo: - Example:

O exemplo a seguir recupera as seguintes linhas: The following example retrieves the following rows:

  • Linhas da mesa T1 em nenhuma ordem especificada
    Rows of table T1 in no specified order

  • Linhas da tabela T2 na ordem da primeira coluna na tabela T2
    Rows of table T2 in the order of the first column in table T2

A consulta de exemplo então executa uma operação UNION ALL nos resultados das duas subconsultas.
A cláusula ORDER BY ORDER OF UTABLE na consulta especifica que as linhas do resultado da seleção completa devem ser retornadas na mesma ordem que as linhas do resultado da instrução UNION ALL.

The example query then performs a UNION ALL operation on the results of the two subqueries.
The ORDER BY ORDER OF UTABLE clause in the query specifies that the fullselect result rows are to be returned in the same order as the result rows of the UNION ALL statement.

   SELECT * FROM
            (SELECT * FROM T1
             UNION ALL
            (SELECT * FROM T2 ORDER BY 1) 
            ) AS UTABLE
   ORDER  BY ORDER OF UTABLE; 

Exemplo: - Example:

O exemplo a seguir une dados da tabela T1 à tabela de resultados de uma expressão de tabela aninhada.
A expressão da tabela aninhada é ordenada pela segunda coluna na tabela T2.
A cláusula ORDER BY ORDER OF TEMP na consulta especifica que as linhas do resultado da seleção completa devem ser retornadas na mesma ordem que a expressão de tabela aninhada.

The following example joins data from table T1 to the result table of a nested table expression.
The nested table expression is ordered by the second column in table T2.
The ORDER BY ORDER OF TEMP clause in the query specifies that the fullselect result rows are to be returned in the same order as the nested table expression.

   SELECT T1.C1
   ,      T1.C2
   ,      TEMP.Cy
   ,      TEMP.Cx
   FROM   T1, 
         (SELECT T2.C1
          ,      T2.C2 
          FROM   T2 
          ORDER  BY 2) as TEMP(Cx, Cy)
   WHERE  Cy = T1.C1
   ORDER  BY ORDER OF TEMP;

Como alternativa, você pode produzir o mesmo resultado declarando explicitamente a coluna ORDER BY TEMP.Cy na seleção completa em vez de usar a sintaxe ORDER OF.
Alternatively, you can produce the same result by explicitly stating the ORDER BY column TEMP.Cy in the fullselect instead of using the ORDER OF syntax.

   SELECT T1.C1
   ,      T1.C2
   ,      TEMP.Cy
   ,      TEMP.Cx
   FROM   T1,
         (SELECT T2.C1
          ,      T2.C2 
          FROM   T2 
          ORDER  BY 2) as TEMP(Cx, Cy)
   WHERE  Cy = T1.C1
   ORDER  BY TEMP.Cy;

Parent topic: Selecting values while inserting data



© Copyright IBM Corp.