Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- create table student
- (
- id integer,
- name varchar(50),
- gender char(1),
- address varchar(100),
- height_student integer,
- weight_student integer
- )
- select * from student
- insert into student (id, name, gender, address, height_student, weight_student)
- values
- (2, 'sarah', 'W', 'brazil', 140, 40),
- (3, 'reza', 'M', 'england', 155, 55),
- (4, 'george', 'M', 'scothland', 160, 60),
- (5, 'rani', 'W', 'borneo', 120, 35),
- (6, 'raisa', 'W', 'argentina', 146, 46),
- (7, 'fahmi', 'M', 'indonesia', 167, 60),
- (8, 'kana', 'M', 'hindia', 170, 55),
- (9, 'panji', 'M', 'prancis', 180, 67),
- (10, 'rambo', 'M', 'belgia', 178, 87);
- create or replace function get_students_address(p_id integer)
- returns text as $$
- declare
- addresss text default '';
- rec_student record;
- cur_student cursor (p_id integer)
- for select name, id
- from student
- where id = p_id;
- begin
- -- open the cursor
- open cur_student(p_id);
- loop
- -- fetch row into the student
- fetch cur_student into rec_student;
- -- exit when no more row to fetch
- exit when not found;
- --build the output
- if rec_student.address like '%' then
- addresss := addresss || ',' || rec_student.address || ':' || rec_student.id;
- end if;
- end loop;
- --close the cursor
- close cur_student;
- return addresss;
- end; $$
- language plpgsql;
- select get_students_address(1);
Advertisement
Add Comment
Please, Sign In to add comment