My first PostgreSQL stored function...
Goal
------
Have the following table:
ld_operatingsystem
Column | Type | Modifiers
----------------------+-----------------------------+-----------
computer_idn | numeric |
operating_system_idn | numeric |
ostype | character varying(255) |
version | character varying(255) |
laststartuptime | timestamp without time zone |
facility_id | numeric |
suitename | character varying(255) |
I need to update the suitename column so that it is composed of the concatenation of the ostype and version columns.
Start a psql session and paste the function:
create or replace function u_ld_tbl() returns void as $$
declare
userRecord record;
begin
for userRecord IN
select computer_idn, operating_system_idn, ostype, version from ld_operatingsystem
loop
update ld_operatingsystem
set suitename = userRecord.ostype||' '||userRecord.version
where computer_idn = userRecord.computer_idn
and operating_system_idn = userRecord.operating_system_idn;
raise notice 'Updated %, % ', userRecord.computer_idn, userRecord.operating_system_idn;
end loop;
return;
end;
$$ language plpgsql;
The function can be invoked from psql as follows:
select u_ld_tbl();
NOTICE: Updated 31, 19
NOTICE: Updated 1, 1
NOTICE: Updated 4, 7
...
...
...
NOTICE: Updated 53, 37
NOTICE: Updated 54, 38
NOTICE: Updated 55, 39
u_ld_tbl
----------
(1 row)
No comments:
Post a Comment