-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplsql.sql
More file actions
145 lines (123 loc) · 3.08 KB
/
plsql.sql
File metadata and controls
145 lines (123 loc) · 3.08 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
-- variable declaration and print value
set serveroutput on
declare
book_name books.title%type;
begin
select title into book_name from books where book_id = 3;
dbms_output.put_line(book_name);
end;
/
--Insert and set default value
set serveroutput on
declare
reqid NUMBER :=11;
bookid NUMBER :=9;
memberid NUMBER :=6;
begin
INSERT INTO BookRequests VALUES(reqid, bookid, memberid, TO_DATE('2022-03-25', 'YYYY-MM-DD'), 'Pending');
end;
/
--row type
set serveroutput on
declare
bookrow books%rowtype;
begin
select book_id,title,author into bookrow.book_id,bookrow.title,bookrow.author from books where book_id=7;
dbms_output.put_line(bookrow.book_id || bookrow.title||bookrow.author);
end;
/
--cursor and rowcount
set serveroutput on
declare
cursor book_cursor is select book_id, title, author , publish_year from books;
book_row books%rowtype;
begin
open book_cursor;
fetch book_cursor into
book_row.book_id,book_row.title,book_row.author,book_row.publish_year;
while book_cursor%found loop
dbms_output.put_line('book_id: '||book_row.book_id|| ' book_name:
'||book_row.title || ' author: ' ||book_row.author|| ' publish year:
'||book_row.publish_year);
dbms_output.put_line('Row count: '|| book_cursor%rowcount);
fetch book_cursor into
book_row.book_id,book_row.title,book_row.author,book_row.publish_year;
end loop;
close book_cursor;
end;
/
--loop if else
declare
id int :=1;
cursor c is select book_id, title, author , publish_year from books;
book_row books%rowtype;
begin
open c;
fetch c into book_row.book_id,book_row.title,book_row.author,book_row.publish_year;
for x in 1 .. 5 loop
if x = 3
then dbms_output.put_line('id number 3');
end if;
dbms_output.put_line('book_id: '||book_row.book_id|| ' book_name:
'||book_row.title || ' author: ' ||book_row.author|| ' publish year:
'||book_row.publish_year);
fetch c into book_row.book_id,book_row.title,book_row.author,book_row.publish_year;
end loop;
close c;
end;
/
--array and loop
set serveroutput on
declare
counter number;
book_name2 books.title%type;
TYPE NAMEARRAY IS VARRAY(5) OF books.title%type;
A_NAME NAMEARRAY:=NAMEARRAY();
begin
counter:=1;
for x in 1..5
loop
select title into book_name2 from books where book_id=x;
A_NAME.EXTEND();
A_NAME(counter):=book_name2;
counter:=counter+1;
end loop;
counter:=1;
WHILE counter<=A_NAME.COUNT
LOOP
DBMS_OUTPUT.PUT_LINE(A_NAME(counter));
counter:=counter+1;
END LOOP;
end;
/
--function
set SERVEROUTPUT on
create or replace function fun( id number) return varchar as
title varchar(30);
begin
select title into title from books where book_id = id;
return title;
end;
/
set SERVEROUTPUT on
declare
begin
dbms_output.put_line(fun(3));
end;
/
--procedure
set SERVEROUTPUT on
create or replace procedure proc( id number) is
title varchar(30);
begin
select title into title from books where book_id = id;
dbms_output.put_line(title);
end;
/
set SERVEROUTPUT on
declare
id number := 4;
begin
proc(id);
end;
/