First, the control statement
1. If statement
Grammar:
IF < Boolean expressions > Then
PL/SQL and statements
END IF;
Example:
DECLARE Number: = & n; BEGIN IF > 0 Then dbms_output.put_line (' positive number ') ; END IF ; END;
Determine if it is a positive integer!
2. If...else ...
Grammar
IF < Boolean expressions > Then
PL/SQL and statements
ELSE
Other statements
END IF;
Example:
DECLAREV_count Number:= &N;BEGIN IFV_count> 0 ThenDbms_output.put_line ('Positive'); ELSEDbms_output.put_line ('Negative number'); END IF;END;
Determine if it is a positive or negative number!
3, multi-if...else ... Structure
Grammar:
IF < Boolean expressions > Then
PL/SQL and statements
Elsif < Other Boolean expressions > Then
PL/SQL and statements
Elsif < Other Boolean expressions > Then
PL/SQL and statements
ELSE
PL/SQL and statements
END IF;
Example:
DECLAREV_count Number:= &N;BEGIN IFV_count> 0 ThenDbms_output.put_line ('Positive'); elsif V_count< 0 ThenDbms_output.put_line ('Negative number'); ELSEDbms_output.put_line ('0'); END IF;END;
4. Case statement Syntax:
Format one:
Case conditional expression
When conditional expression result 1 Then
Statement Segment 1
When conditional expression Result 2 Then
Statement segment
......
When conditional expression Results n Then
Statement Segment N
[ELSE statement Segment]
END case;
Example: Show week based on input 1-7
DECLAREV_week Number:= &S;BEGIN CaseV_week when 1 ThenDbms_output. Put_Line ('Monday'); when 2 ThenDbms_output. Put_Line ('Tuesday'); when 3 ThenDbms_output. Put_Line ('Wednesday'); when 4 ThenDbms_output. Put_Line ('Thursday'); when 5 ThenDbms_output. Put_Line ('Friday'); when 6 ThenDbms_output. Put_Line ('Saturday'); when 7 ThenDbms_output. Put_Line ('Sunday'); ELSEDbms_output. Put_Line ('Input Error'); END Case;END;
Format two:
Case
When conditional expression 1 then
Statement Segment 1
When conditional expression 2 Then
Statement segment
......
When conditional expression n Then
Statement Segment N
[ELSE statement Segment]
END case;
Example: Based on fractional output a,b,c,d,e
DECLAREV_score Number:= &N;BEGIN Case whenV_score>= - ThenDbms_output. Put_Line ('A'); whenV_score>= the ThenDbms_output. Put_Line ('B'); whenV_score>= - ThenDbms_output. Put_Line ('C'); whenV_score>= - ThenDbms_output. Put_Line ('D'); ELSEDbms_output. Put_Line ('E'); END Case;END;
Pl-sql Programming Basics (2)