PostgreSQL/Postgre SQL/If — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 10:13, 26 мая 2010
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", 25908, 1, 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", 41473, 2, 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 | 1 | 59 | 1957-03-01 | h
0451160916 | 7808 | 1 | 75 | 1981-08-01 | p
0394800753 | 1590 | 1 | 59 | 1949-03-01 | p
0590445065 | 25908 | 1 | 150 | 1987-03-01 | p
0694003611 | 1501 | 1 | 65 | 1947-03-04 | p
0679803335 | 1234 | 1 | 102 | 1922-01-01 | p
0760720002 | 190 | 1 | 91 | 1868-01-01 | p
0394900014 | 1608 | 1 | 59 | 1957-01-01 | p
0385121679 | 7808 | 2 | 75 | 1993-10-01 | h
1885418035 | 156 | 1 | 163 | 1995-03-28 | p
0929605942 | 156 | 2 | 171 | 1998-12-01 | p
0441172717 | 4513 | 2 | 99 | 1998-09-01 | p
044100590X | 4513 | 3 | 99 | 1999-10-01 | h
0451457994 | 4267 | 3 | 101 | 2000-09-12 | p
0451198492 | 4267 | 3 | 101 | 1999-10-01 | h
0823015505 | 2038 | 1 | 62 | 1958-01-01 | p
0596000855 | 41473 | 2 | 113 | 2001-03-01 | p
(17 rows)
postgres=#
postgres=#
postgres=#
postgres=# -- Using the IF/THEN/ELSE statement
postgres=#
postgres=# CREATE FUNCTION in_stock (integer,integer) RETURNS 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 <= 0 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
(1 row)
postgres=#
postgres=#
postgres=# drop table editions;
DROP TABLE
postgres=#