Em 2 de junho de 2016 01:10, Patrick Baker <[email protected]>
escreveu:

> Olá pessoal, tudo bem?
>
> estou escrevendo uma função para fazer algumas coisas:
>
> CREATE or REPLACE FUNCTION function_data_1(rows integer)
>
> RETURNS INTEGER AS $$
>
>
> declare
>
>   completed integer;
>
>   offset_num integer;
>
>   crtRow record;
>
>
> BEGIN
>
>   offset_num = 0;
>
>
> INSERT INTO table2_y_b (note_id, size, file_id, full_path)
>
>     (
>
>             SELECT
>
>                     t1.note_id,
>
>                     t1.size,
>
>                     t1.file_id,
>
>                     t1.full_path
>
>             FROM
>
>                     table1_n_b t1
>
>             JOIN
>
>                     table3_n_b t3 ON t3.file_id = t1.file_id
>
>     );
>
>
>
> -- Copying the BLOBS into the table above (BACKUP)
>
> UPDATE table2_y_b t2 SET segment_data =
>
>     (
>
>             SELECT
>
>                     o1.data
>
>             FROM
>
>                     original_table1_b o1
>
>             JOIN
>
>                     table3_n_b t3 ON t3.file_id = o1.file_id
>
>             WHERE
>
>                     t2.migrated = 0
>
>             AND
>
>                     t2.file_id = o1.file_id
>
>     );
>
>
> -- Update the migrated column from 0 to 1, for those rows that have been
> modified/copied.
>
> UPDATE table2_y_b SET migrated = 1 WHERE file_id = crtRow.file_id AND
> migrated = 0;
>
>
> UPDATE original_table1_b SET data = NULL WHERE file_id = crtRow.file_id;
>
>
> --RETURN file_id;
>
>
> END
>
>
> $$ language 'plpgsql';
>
>
> *Explicação:*
>
> CREATE or REPLACE FUNCTION function_data_1(rows integer)
>
> Quero poder chamar a função e especificar na hora quantas rows eu vou
> querer que sejam atingidas por ela. Por exemplo: select
> function_data_1(5000);
>
>
> INSERT INTO table2_y_b (note_id, size, file_id, full_path)
>>     (
>>             SELECT
>>                     t1.note_id,
>>                     t1.size,
>>                     t1.file_id,
>>                     t1.full_path
>>             FROM
>>                     table1_n_b t1
>>             JOIN
>>                     table3_n_b t3 ON t3.file_id = t1.file_id
>>     );
>
> Estou inserindo dados na tabela table2_y_b. Estes dados são dados básicos
> para que depois eu armazene os BLOBS também *(FUNCIONANDO)*
>
> UPDATE table2_y_b t2 SET segment_data =
>>     (
>>             SELECT
>>                     o1.data
>>             FROM
>>                     original_table1_b o1
>>             JOIN
>>                     table3_n_b t3 ON t3.file_id = o1.file_id
>>             WHERE
>>                     t2.migrated = 0
>>             AND
>>                     t2.file_id = o1.file_id
>>     );
>
> Aqui eu insiro os BLOBS - Sempre respeitando o file_id *(FUNCIONANDO)*
>
> UPDATE table2_y_b SET migrated = 1 WHERE file_id = crtRow.file_id AND
>> migrated = 0;
>
> Depois que os passos a cima estão completos, posso setar a column migrated
> como 1 - Isso quer dizer que aquela ROW foi manipulada com sucesso
>
> - Não sei como faço para que este update seja feito respeitando o update a
> cima. Como fazer a query saber que ela tem que atualizar somente as rows
> que foram alteradas pela query a cima?
>
>
>> UPDATE original_table1_b SET data = NULL WHERE file_id = crtRow.file_id;
>
> Como já tenho o backup dos dados ( table2_y_b ), posso agora setar o blob
> como NULL ( deletando ele )
>
> - Não sei como faço para que este update seja feito respeitando o update a
> cima. Como fazer a query saber que ela tem que atualizar somente as rows
> que foram alteradas pela query a cima?
>
>
>
>
> Não sei como faço para limitar a query, fazer com que ela só execute os
> procedimentos para as rows que eu quiser ( *select function_data_1(5000)*
> )
> Alguém pode dar uma mão?
>
> Obrigado!
> Patrick
>
> _______________________________________________
> pgbr-geral mailing list
> [email protected]
> https://listas.postgresql.org.br/cgi-bin/mailman/listinfo/pgbr-geral
>

Bom dia
não seria fácil você colocar um limit no select que gera os inserts,
utilizando o parâmetro que chama a funcion?

algo assim:

  SELECT
                    o1.data
            FROM
                    original_table1_b o1
            JOIN
                    table3_n_b t3 ON t3.file_id = o1.file_id
            WHERE
                    t2.migrated = 0
            AND
                    t2.file_id = o1.file_id
limit *rows *
-- 

Douglas Fabiano Specht
_______________________________________________
pgbr-geral mailing list
[email protected]
https://listas.postgresql.org.br/cgi-bin/mailman/listinfo/pgbr-geral

Responder a