Beruflich Dokumente
Kultur Dokumente
Write a PL/SQL block to create a procedure using the "IS [NOT] NULL Operator"
and show NOT operator returns the opposite of its operand, unless the operand
is NULL.
PL/SQL Code:
Copy
PL/SQL Code:
DECLARE
PROCEDURE pri_not_m (
m BOOLEAN
) IS
BEGIN
pri_bool ('m', m);
pri_bool ('NOT m', NOT m);
END pri_not_m;
BEGIN
DBMS_OUTPUT.PUT_LINE('------------- FOR m TRUE ---------------------');
pri_not_m (TRUE);
DBMS_OUTPUT.PUT_LINE('------------- FOR m FALSE ---------------------');
pri_not_m (FALSE);
DBMS_OUTPUT.PUT_LINE('------------- FOR m NULL ---------------------');
pri_not_m (NULL);
END;
/
Copy
Sample Output:
Flowchart:
Procedure
In the following example the m and n seem unequal. But, NULL values are indeterminate.
Whether m equals n is unknown. Therefore, the IF condition yields NULL and the sequence
of statements is bypassed, similarly o and p seem equal. But, again, that is unknown, so
the IF condition yields NULL and the sequence of statements is bypassed and however, if
either q or r is NULL, then the first IF statement assigns the value of r to large and the
second IF statement assigns the value of q to large.
PL/SQL Code:
DECLARE
m NUMBER := 7;
n NUMBER := NULL;
o NUMBER := NULL;
p NUMBER := NULL;
q INTEGER := 4;
r INTEGER := 9;
large INTEGER;
----------------------------------
BEGIN
IF m != n THEN -- yields NULL, not TRUE
DBMS_OUTPUT.PUT_LINE('m != n'); -- not run
ELSIF m = n THEN -- also yields NULL
DBMS_OUTPUT.PUT_LINE('m = n');
ELSE
DBMS_OUTPUT.PUT_LINE
('Can not say whether m and n are equal or not.');
END IF;
-----------------------------------
IF o = p THEN -- yields NULL, not TRUE
DBMS_OUTPUT.PUT_LINE('o = p'); -- not run
ELSIF o != p THEN -- yields NULL, not TRUE
DBMS_OUTPUT.PUT_LINE('o != p'); -- not run
ELSE
DBMS_OUTPUT.PUT_LINE('Can not say whether two NULLs are equal');
END IF;
--------------------------------------
IF (q > r) -- If q or r is NULL, then (q > r) is NULL
THEN large := q; -- run if (q > r) is TRUE
ELSE large := r; -- run if (q > r) is FALSE or NULL
DBMS_OUTPUT.PUT_LINE('The value of large : '||large);
END IF;
Copy
Sample Output:
Statement processed.
0.00 seconds
PL/SQL Fundamentals: Exercise-16 with Solution
Write a PL/SQL block to describe the usage of LIKE operator including wildcard
characters and escape character.
In the following example a procedure pat_match with two arguments test_string and pattern
compares whether the test_string matching with the pattern and returns TRUE or FALSE
according to the matching.
PL/SQL Code:
DECLARE
PROCEDURE pat_match (
test_string VARCHAR2,
pattern VARCHAR2
) IS
BEGIN
IF test_string LIKE pattern THEN
DBMS_OUTPUT.PUT_LINE ('TRUE');
ELSE
DBMS_OUTPUT.PUT_LINE ('FALSE');
END IF;
END;
BEGIN
pat_match('Blweate', 'B%a_e');
pat_match('Blweate', 'B%A_E');
END;
/
Copy
Sample Output:
TRUE
FALSE
Statement processed.
0.00 seconds
Flowchart:
In the following example a procedure pat_escape with the arguments mar_achiv compares
whether the mar_achiv containing any percent ( % ) sign or underscore ( _ ) and returns
TRUE or FALSE according to the matching. To search for the percent sign or underscore,
an escape character ( backslash i.e. '\' ) have to define and put it before the percent sign or
underscore. Uses the backslash as the escape character the percent sign in the string does not
act as a wildcard.
PL/SQL Code:
DECLARE
PROCEDURE pat_escape (mar_achiv VARCHAR2) IS
BEGIN
IF mar_achiv LIKE '70\% out of 100!' ESCAPE '\' THEN
DBMS_OUTPUT.PUT_LINE ('TRUE');
ELSE
DBMS_OUTPUT.PUT_LINE ('FALSE');
END IF;
END;
BEGIN
pat_escape('Go and try your best');
pat_escape('70% out of 100!');
END;
/
Copy
Sample Output:
FALSE
TRUE
Statement processed.
0.00 seconds