-
Notifications
You must be signed in to change notification settings - Fork 0
/
PL-SQL Anonymous Block.sql
86 lines (78 loc) · 1.64 KB
/
PL-SQL Anonymous Block.sql
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
-- PL-SQL Anonymous Block Examples
SET SERVEROUTPUT ON;
-- Hello World
DECLARE
v_line VARCHAR2(40);
BEGIN
v_line := 'Hello World';
DBMS_OUTPUT.PUT_LINE(v_line);
END;
/
-- Value Concatenation
DECLARE
v_line1 VARCHAR2(20);
v_line2 VARCHAR2(20);
BEGIN
v_line1 := 'Hello ...';
v_line2 := '+... World';
DBMS_OUTPUT.PUT_LINE(v_line1 || v_line2);
END;
/
-- Declaring Types
DECLARE
v_line1 VARCHAR2(20);
v_line2 v_line1%TYPE;
BEGIN
v_line1 := 'Hello ...';
v_line2 := '+... World';
DBMS_OUTPUT.PUT_LINE(v_line1 || v_line2);
END;
/
-- Default Variable Settings
DECLARE
v_line1 VARCHAR2(20) := 'Default A ';
v_line2 VARCHAR2(20) := 'Default B';
BEGIN
DBMS_OUTPUT.PUT_LINE('Before: ' || v_line1 || v_line2);
v_line1 := 'Hello ...';
v_line2 := '+... World';
DBMS_OUTPUT.PUT_LINE(v_line1 || v_line2);
END;
/
-- Assign values of SQL statements
DECLARE
first_started NUMBER;
BEGIN
SELECT MIN(started) INTO first_started
FROM Student2;
DBMS_OUTPUT.PUT_LINE(first_started);
END;
/
-- Errors and Exceptions
DECLARE
first_started NUMBER;
sid NUMBER;
BEGIN
SELECT MIN(started) INTO first_started
FROM Student2;
SELECT SID INTO sid
FROM Student2
WHERE started = first_started;
DBMS_OUTPUT.PUT_LINE(sid);
END;
/
DECLARE
first_started NUMBER;
sid NUMBER;
BEGIN
SELECT MIN(started) INTO first_started
FROM Student2;
SELECT SID INTO sid
FROM Student2
WHERE started = first_started;
DBMS_OUTPUT.PUT_LINE(sid);
EXCEPTION
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE('Several Students in First Year');
END;
/