Sie sind auf Seite 1von 9

PL/SQL Fundamentals: Exercise-14 with Solution

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.

Here is the procedure:

PL/SQL Code:

CREATE OR REPLACE PROCEDURE pri_bool(


boo_name VARCHAR2,
boo_val BOOLEAN
) IS
BEGIN
IF boo_val IS NULL THEN
DBMS_OUTPUT.PUT_LINE( boo_name || ' = NULL');
ELSIF boo_val = TRUE THEN
DBMS_OUTPUT.PUT_LINE( boo_name || ' = TRUE');
ELSE
DBMS_OUTPUT.PUT_LINE( boo_name || ' = FALSE');
END IF;
END;
/

Copy

Now call the procedure pri_bool:

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:

------------- FOR m TRUE ---------------------


m = TRUE
NOT m = FALSE
------------- FOR m FALSE ---------------------
m = FALSE
NOT m = TRUE
------------- FOR m NULL ---------------------
m = NULL
NOT m = NULL

Flowchart:

Procedure

Now call the procedure pri_bool:


PL/SQL Fundamentals: Exercise-15 with Solution
Write a PL/SQL block to describe the usage of NULL values in equal comparison, unequal
comparison and NOT NULL equals NULL comparison.

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;

IF NOT (q > r) -- If q or r is NULL, then NOT (q > r) is NULL


THEN large := r; -- run if NOT (q > r) is TRUE
ELSE large := q; -- run if NOT (q > r) is FALSE or NULL
DBMS_OUTPUT.PUT_LINE('The value of large : '||large);
END IF;
END;
/

Copy

Sample Output:

Can not say whether m and n are equal or not.


Can not say whether two NULLs are equal
The value of large : 9

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

Das könnte Ihnen auch gefallen