Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- create table new_book
- (
- id serial,
- title text,
- author text
- );
- select * from new_book
- --function untuk mengisi table new_book (ADD)
- create or replace function ins_book(p_title varchar, p_author text)
- returns integer as
- $$
- insert into new_book(title, author) values ($1,$2)
- returning id;
- $$
- language 'sql' volatile;
- select ins_book ('Qowaid Arba', 'Darul Haq') as book_id;
- select ins_book ('Aqua', 'Rahad') as new_id;
- --function untuk mengUpdate new_book (UPDATE)
- create or replace function upd_book(log_id integer, p_title text, p_author text)
- returns void as
- $$
- update new_book
- set title = $2, author = $3 where id = $1;
- $$
- language 'sql' volatile;
- select upd_book(4, 'Aqua', 'Imam');
- --READ
- create or replace function red_book(p_title varchar)
- returns table (id int, title text, author text) as
- $$
- select id, title, author from new_book
- where title like $1;
- $$
- language 'sql' stable;
- select * from red_book ('sirah nabawi');
- --OUT
- create or replace function red_books_out (p_title varchar, out p_id int, out title text, out p_author text)
- returns setof record as
- $$
- select * from new_book
- where title like $1;
- $$
- language 'sql' stable;
- select * from red_books_out ('Aqua');
- --using composite type
- create or replace function red_book_com(p_title text)
- returns setof new_book as
- $$
- select * from new_book
- where title like $1;
- $$
- language 'sql' stable;
- select * from red_book_com ('Aqua');
- --writing
- create or replace function red_book_psql()
- returns table (id int, title text, author text)as
- $$
- begin
- return query select * from new_book;
- end;
- $$
- language 'plpgsql' stable;
- select * from red_book_psql();
- --trigger function
- create or replace function f_trig_books() returns trigger as
- $$
- begin
- new.title := upper(new.title);
- return new;
- end;
- $$
- language plpgsql volatile;
- create trigger trigger_books
- before insert or update of title, author
- on new_book
- for each row
- execute procedure f_trig_books();
- select ins_book('bahasa arab', 'darul haq') as new_id;
- drop function ins_book
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement