Created
September 20, 2021 18:52
-
-
Save OberdanBrito/f95e6578c970962720bd81008674d3f1 to your computer and use it in GitHub Desktop.
O exemplo utiliza o looping do banco de dados dentro de cada linha para gerar uma sequência de 365 dias do ano para cada registro da tabela de destino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Como obter datas retroativas usando o bloco de código, Looping | |
*/ | |
do | |
$$ | |
begin | |
for i in 1 .. 365 loop | |
raise notice '%', current_date - i || 'T00:00:00.000Z'; | |
end loop; | |
end | |
$$; | |
/** | |
* Exemplo de inserção em tabela usando datas retroativas | |
*/ | |
do | |
$$ | |
declare | |
_usuario record; | |
begin | |
for _usuario in select id from directoryservices.users where gr_download = true loop | |
for i in 1 .. 365 loop | |
insert into directoryservices.dates (userid, date_start, date_end) | |
values (_usuario.id, (current_date - i || 'T00:00:00.000Z')::timestamp, (current_date - i || 'T23:59:59.000Z')::timestamp); | |
end loop; | |
end loop; | |
end | |
$$; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment