-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4.insert_artwork_function.sql
More file actions
65 lines (48 loc) · 2.67 KB
/
Copy path4.insert_artwork_function.sql
File metadata and controls
65 lines (48 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*insert_artwork function enables you to enter
* the name of the art work,
* the artist,
* the creation year,
* event name and
* supplier's name
* all at once. As Always the function returns the inserted row id.
* There is only one example in the DML, so I'm gonna show you here as well.
* Here is an example of DML using this function.
* SELECT * FROM insert_artwork('Memory Lost', 'Nan Goldin', '2020', 'NAN GOLDIN: MEMORY LOST', 'Moderna Museet, Stockholm' );
*/
SET search_path TO ls_museum;
DROP FUNCTION IF EXISTS insert_artwork( "name" TEXT,
artist TEXT,
creation_year TEXT,
event_name TEXT,
supplier_name TEXT);
CREATE OR REPLACE FUNCTION insert_artwork( "name" TEXT,
artist TEXT,
creation_year TEXT,
event_name TEXT,
supplier_name TEXT)
RETURNS integer
AS $$
DECLARE i_event_id integer;
i_supplier_id integer;
i_artwork_id integer;
BEGIN
SELECT e.event_id INTO i_event_id --selecting necessary event id into i_event_id veriable
FROM ls_museum.event e
WHERE lower(e.event_name) = lower(insert_artwork.event_name :: text);
SELECT s.supplier_id INTO i_supplier_id --selecting necessary supplier id into i_supplier_id veriable
FROM ls_museum.supplier s
WHERE lower(s.name) = lower(insert_artwork.supplier_name:: text);
INSERT INTO ls_museum.artwork("name",
artist,
creation_year,
event_id,
supplier_id)
SELECT "name",
artist,
to_date(creation_year, 'YYYY'),
i_event_id,
i_supplier_id
RETURNING ls_museum.artwork.artwork_id INTO i_artwork_id ; --returning artwork_id into i_artwork_id TO CHECK IF the INSERT was successful
RETURN i_artwork_id LIMIT 1;
END;
$$ LANGUAGE plpgsql;