Mega Code Archive

 
Categories / PostgreSQL / Insert Delete Update
 

Using user defined function to update table data

postgres=# CREATE TABLE emp ( postgres(#    name        text, postgres(#    salary      numeric, postgres(#    age         integer, postgres(#    cubicle     point postgres(# ); CREATE TABLE postgres=# postgres=# insert into emp (salary) values(100); INSERT 0 1 postgres=# insert into emp (salary) values(200); INSERT 0 1 postgres=# insert into emp (salary) values(300); INSERT 0 1 postgres=# postgres=# CREATE FUNCTION double_salary(emp) RETURNS numeric AS $$ postgres$#    SELECT $1.salary * 2 AS salary; postgres$# $$ LANGUAGE SQL; CREATE FUNCTION postgres=# postgres=# SELECT name, double_salary(ROW(name, salary*1.1, age, cubicle)) AS dream postgres-#    FROM emp;  name | dream ------+-------       | 220.0       | 440.0       | 660.0 (3 rows) postgres=# postgres=# drop function double_salary(emp); DROP FUNCTION postgres=# drop table emp; DROP TABLE postgres=# postgres=#