Rahmadnet

Develop the first user-defined function

Jul 21st, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. create table test(
  2. id int generated by default as identity primary key,
  3. nama char (50) not null,
  4. age int not null,
  5. address char(100) not null,
  6. salary int not null);
  7.  
  8. insert into test (nama, age, address, salary)
  9. values ('Rahmad', 24, 'Medan', 1000000);
  10.  
  11. insert into test (nama, age, address, salary)
  12. values ('Andari', 19, 'Suka Bumi', 2000000);
  13.  
  14. insert into test (nama, age, address, salary)
  15. values ('Rahmadi', 18, 'Jambi', 3000000);
  16.  
  17. insert into test (nama, age, address, salary)
  18. values ('Rafi', 20, 'Jakarta', 4000000);
  19.  
  20. insert into test (nama, age, address, salary)
  21. values ('Wandi', 20, 'Prabumuli', 5000000);
  22.  
  23. insert into test (nama, age, address, salary)
  24. values ('Wildan', 22, 'Bekasi', 6000000);
  25.  
  26. insert into test (nama, age, address, salary)
  27. values ('Rahmad', 24, 'Medan', 1000000);
  28.  
  29. insert into test (nama, age, address, salary)
  30. values ('Rahmad', 24, 'Medan', 1000000);
  31.  
  32.  
  33. create or replace function totalRecords ()
  34. returns integer as $total$
  35. declare
  36.     total integer;
  37. begin
  38.     select count(*) into total from test;
  39.     return total;
  40. end;
  41. $total$ language plpgsql;
  42. end
  43.  
  44.  
  45. select totalRecords();
  46.  
  47. select * from test;
Advertisement
Add Comment
Please, Sign In to add comment