Using the IF/THEN/ELSE statement : If « Postgre SQL « PostgreSQL

Home
PostgreSQL
1.Aggregate Functions
2.Analytical Functions
3.Array
4.Constraints
5.Cursor
6.Data Type
7.Database
8.Date Timezone
9.Index
10.Inheritance
11.Insert Delete Update
12.Math Functions
13.Postgre SQL
14.Select Query
15.Sequence
16.Store Procedure Function
17.String Functions
18.Subquery
19.Table
20.Table Joins
21.Transaction
22.User Previliege
23.View
PostgreSQL » Postgre SQL » If 
Using the IF/THEN/ELSE statement


postgres=#
postgres=# CREATE TABLE "editions" (
postgres(#      "isbn" text NOT NULL,
postgres(#      "book_id" integer,
postgres(#      "edition" integer,
postgres(#      "publisher_id" integer,
postgres(#      "publication" date,
postgres(#      "type" character(1)
postgres();
ERROR:  relation "editions" already exists
postgres=#
postgres=# insert into editions values('039480001X', 1608,  1,  59,  '1957-03-01', 'h');
ERROR:  duplicate key violates unique constraint "pkey"
postgres=# insert into editions values('0451160916', 7808,  1,  75,  '1981-08-01', 'p');
ERROR:  duplicate key violates unique constraint "pkey"
postgres=# insert into editions values('0394800753', 1590,  1,  59,  '1949-03-01', 'p');
ERROR:  duplicate key violates unique constraint "pkey"
postgres=# insert into editions values('0590445065', 259081,  150'1987-03-01', 'p');
ERROR:  duplicate key violates unique constraint "pkey"
postgres=# insert into editions values('0694003611', 1501,  1,  65,  '1947-03-04', 'p');
ERROR:  duplicate key violates unique constraint "pkey"
postgres=# insert into editions values('0679803335', 1234,  1,  102'1922-01-01', 'p');
ERROR:  duplicate key violates unique constraint "pkey"
postgres=# insert into editions values('0760720002', 190,   1,  91,  '1868-01-01', 'p');
ERROR:  duplicate key violates unique constraint "pkey"
postgres=# insert into editions values('0394900014', 1608,  1,  59,  '1957-01-01', 'p');
ERROR:  duplicate key violates unique constraint "pkey"
postgres=# insert into editions values('0385121679', 7808,  2,  75,  '1993-10-01', 'h');
ERROR:  duplicate key violates unique constraint "pkey"
postgres=# insert into editions values('1885418035', 156,   1,  163'1995-03-28', 'p');
ERROR:  duplicate key violates unique constraint "pkey"
postgres=# insert into editions values('0929605942', 156,   2,  171'1998-12-01', 'p');
ERROR:  duplicate key violates unique constraint "pkey"
postgres=# insert into editions values('0441172717', 4513,  2,  99,  '1998-09-01', 'p');
ERROR:  duplicate key violates unique constraint "pkey"
postgres=# insert into editions values('044100590X', 4513,  3,  99,  '1999-10-01', 'h');
ERROR:  duplicate key violates unique constraint "pkey"
postgres=# insert into editions values('0451457994', 4267,  3,  101'2000-09-12', 'p');
ERROR:  duplicate key violates unique constraint "pkey"
postgres=# insert into editions values('0451198492', 4267,  3,  101'1999-10-01', 'h');
ERROR:  duplicate key violates unique constraint "pkey"
postgres=# insert into editions values('0823015505', 2038,  1,  62,  '1958-01-01', 'p');
ERROR:  duplicate key violates unique constraint "pkey"
postgres=# insert into editions values('0596000855', 414732,  113'2001-03-01', 'p');
ERROR:  duplicate key violates unique constraint "pkey"
postgres=#
postgres=# select from editions;
    isbn    | book_id | edition | publisher_id | publication | type
------------+---------+---------+--------------+-------------+------
 039480001X |    1608 |       |           59 1957-03-01  | h
 0451160916 |    7808 |       |           75 1981-08-01  | p
 0394800753 |    1590 |       |           59 1949-03-01  | p
 0590445065 |   25908 |       |          150 1987-03-01  | p
 0694003611 |    1501 |       |           65 1947-03-04  | p
 0679803335 |    1234 |       |          102 1922-01-01  | p
 0760720002 |     190 |       |           91 1868-01-01  | p
 0394900014 |    1608 |       |           59 1957-01-01  | p
 0385121679 |    7808 |       |           75 1993-10-01  | h
 1885418035 |     156 |       |          163 1995-03-28  | p
 0929605942 |     156 |       |          171 1998-12-01  | p
 0441172717 |    4513 |       |           99 1998-09-01  | p
 044100590X |    4513 |       |           99 1999-10-01  | h
 0451457994 |    4267 |       |          101 2000-09-12  | p
 0451198492 |    4267 |       |          101 1999-10-01  | h
 0823015505 |    2038 |       |           62 1958-01-01  | p
 0596000855 |   41473 |       |          113 2001-03-01  | p
(17 rows)

postgres=#
postgres=#
postgres=#
postgres=# -- Using the IF/THEN/ELSE statement
postgres=#
postgres=# CREATE FUNCTION in_stock (integer,integerRETURNS boolean AS '
postgres'#   DECLARE
postgres'#      -- Declare aliases for function arguments.
postgres'#     b_id ALIAS FOR $1;
postgres'#     b_edition ALIAS FOR $2;
postgres'#
postgres'#      -- Declare a text variable to hold the ISBN of the book
postgres'#      -- once found.
postgres'#     b_isbn TEXT;
postgres'#
postgres'#      -- Declare an integer variable to hold the amount of stock.
postgres'#     stock_amount INTEGER;
postgres'#
postgres'#   BEGIN
postgres'#
postgres'#     SELECT INTO b_isbn isbn FROM editions WHERE
postgres'#       book_id = b_id AND edition = b_edition;
postgres'#
postgres'#     IF b_isbn IS NULL THEN
postgres'#       RETURN FALSE;
postgres'#     END IF;
postgres'#
postgres'#     SELECT INTO stock_amount stock FROM stock WHERE isbn = b_isbn;
postgres'#
postgres'#     IF stock_amount <= THEN
postgres'#       RETURN FALSE;
postgres'#     ELSE
postgres'#       RETURN TRUE;
postgres'#     END IF;
postgres'#
postgres'#   END;
postgres'# ' LANGUAGE 'plpgsql';
CREATE FUNCTION
postgres=#
postgres=#
postgres=# SELECT in_stock(4513,2);
 in_stock
----------
 t
(row)

postgres=#
postgres=#
postgres=# drop table editions;
DROP TABLE
postgres=#
           
       
Related examples in the same category
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.