You are on page 1of 6

Fundamental of PL/SQL

Q1.
ans.

PL/SQL Expressions and Comparisons


I. Pl/SQL Expressions 1. Expressions are a combination of function, values (operands) and operators that evaluate a value. 2. Operator is either unary or binary, operating on either one or two operands w.r.t. 3. E.g.:- unary operator is negation (-) , Binary operator is addition (+). 4. Simple arithmetic expression is: E.g. :- 2 + 3 5. An expression always returns a single value. 6. As above e.g. , PL/SQL automatically finds the data type of the value. 7. TYPES OF OPERATOR ; Concatenation Operator ; The concatenation operator denoted by || It appends one string operand to another. Syntax :- string1 || string2 || string_n E.g.:SQL> DECLARE 2 x VARCHAR2(4) := 'suit'; 3 y VARCHAR2(4) := 'case'; 4 BEGIN 5 DBMS_OUTPUT.PUT_LINE (x || y); 6 END; 7 /suitcase PL/SQL procedure successfully completed. SQL>

A. a. b. c. d.

B. a.

b.

Operator Precedence The operations within an expression are evaluated in order of precedence. Operator precedence from highest to lowest order.

c. d.

You can use parentheses to control the order of evaluation and to improve readability. E.g.

Fundamental of PL/SQL
II. Pl/SQL Comparison; 1. Comparison operators compare one expression to another. 2. The result is always either TRUE, FALSE, OR NULL. 3. You use comparison operators in conditional control statements and in the WHERE clauses of SQL data manipulation statements. 4. The comparison operators :
RELATIONAL operators IS NULL Operator LIKE Operator BETWEEN Operator IN Operator

5.

RELATIONAL operators

6.

E.g. Relational Operators;

7.

IS NULL Operator : a. The IS NULL operator returns the BOOLEAN value ; TRUE; if its operand is NULL or FALSE; if it is not NULL. b. In Comparisons, NULL values always produce NULL. LIKE Operator :a. The LIKE operator compares a ; character, string, or CLOB value to a pattern and returns; TRUE if the value matches the pattern FALSE if it does not. b. E.g. Like operator;

8.

c.
d.

To search the percent sign or underscore, define an escape character and put it Before the percent sign or underscore.
note:- uses the backslash as the escape character, so that the percent sign in the string does not act as a wildcard.

E.g. Escape Character in Pattern

Fundamental of PL/SQL
9. BETWEEN Operator :a. The BETWEEN operator tests whether a value lies in a specified range. b. x BETWEEN a AND b means that x >= a and x <= b. c. E.g. BETWEEN Operator;- invokes the print_boolean procedure of Q2.e(AND operator) to print values of some expressions that include the BETWEEN operator.

10. IN Operator:a. The IN operator tests set membership. b. x IN (set) means that x is equal to any member of set. c. E.g.; IN operator invokes the print_boolean procedure created in Q2.e(AND operator) to print values of some expressions that include the IN operator.

d.

Below E.g. shows what happens when set contains a NULL value. (This e.g. invokes the print_boolean procedure created in Q2.e(AND operator) )

Q2. Logical Operators


a. b. c. The logical operators AND, OR, and NOT follow the tri-state logic. AND & OR are binary operators; NOT is a unary operator. Logical operator ;

d.

e.

E.g. AND Operator;

Fundamental of PL/SQL
f. E.g. OR Operator

g.

Eg NOT Operator

Q3. BOOLEAN Expressions


Ans.
i. ii. iii. iv. v. vi. vii. viii. PL/SQL allows you to compare variables and constants in both Sql and Procedural statements. These comparisons, called BOOLEAN expressions. In a SQL statement, BOOLEAN expressions specify the rows that are affected by the statement. In a procedural statement, BOOLEAN expressions are the used with conditional control. It consists of simple or complex expressions, separated by relational operators. Often, BOOLEAN expressions are connected by the logical operators AND, OR, and NOT. A BOOLEAN expression always produces TRUE, FALSE, or NULL. Types of BOOLEAN Expressions ; BOOLEAN Arithmetic Expressions BOOLEAN Character Expressions BOOLEAN Date Expressions BOOLEAN Arithmetic Expressions :1. Allow you compare number, using relational operators 2. Compare to find equality or inequality, greater than, less thanso on. 3. E.g.; number1 := 75; number2 := 70 , Therefore, number1 > number2 is true 4. Does not allow you to compare real no. BOOLEAN Character Expressions 1. Allow you to compare character. 2. Compare to find equality or inequality. 3. Comparisons are based on the binary values of each byte in the string. 4. E.g.; string1 := 'Kathy'; string2 := 'Kathleen'; Therefore, string1 > string2 is true BOOLEAN Date Expressions 1. Allow you to compare date. 2. Comparisons are chronological; that is, one date is greater than another. 3. E.g. date1:= '01-JAN-91'; date2:= '31-DEC-90'; Therefore, date1 > date2 is true. 4. E.g.

a.

b.

c.

Fundamental of PL/SQL
Q4. CASE Expressions
Ans. 1. 2. There are two types of expressions used in CASE statements: Simple CASE Expression Searched CASE Expression Simple CASE Expression:a. In simple CASE expression, we create one or more alternative using WHEN clause. b. The CASE uses a Selector, that allow you to select a desired alternative and then result is return. c. If no match are found, then the ELSE clause is performed. d. If ELSE clause is not provided then expression returns null. e. E.g. ; WHEN Clause and Selector(grade) with a CASE Statement

3.

Searched CASE Expression:a. In searched CASE expression different conditions is tested, instead of comparing a single expression to various values. b. A searched CASE expression has no selector. c. Each WHEN clause contains a search condition that produces a BOOLEAN value. The BOOLEAN value of each search condition determines which WHEN clause is executed. d. If a search condition is; TRUE; its WHEN clause is executed. Executed WHEN clause search conditions are not evaluated. NOT TRUE; ELSE clause is executed. e. If ELSE clause is not used, then expression return NULL f. E.g. Using a Search Condition with a CASE Statement

g.

The search conditions are evaluated sequentially.

Q6. Data Type in PL/SQL


Ans.

I.

Number Data Type;


a. b. c. The NUMBER data type stores fixed-point or floating-point numbers.
(Note ; with absolute values in the range 1E-130 up to (but not including) 1.0E126.)

d.

e. f. g. h. i. j. k.

A NUMBER variable can also represent 0. Oracle recommends using NUMBER literals and results of NUMBER computations That is within the specified range. Otherwise, the following happen: too small value become zero. too large literal value causes a compilation error. too large computation result that is undefined, cause unreliable results and run-time errors. A NUMBER value has both ; precision (total number of digits at left) scale (the number of digits to the right of the decimal point). Both precision and scale must be integer literals, not constants or variables. For precision, the maximum value is 38. The default value is 39 or 40. For scale, the minimum and maximum values are -84 and 127 respectively. The Default value is zero. The syntax for specifying a fixed-point NUMBER is: NUMBER (precision, scale) E.g.; NUMBER (8, 2) The syntax for specifying an integer NUMBER is: NUMBER(precision) E.g.; NUMBER (2) The syntax for specifying a floating-point NUMBER is: NUMBER Number Data Type In Short;

Fundamental of PL/SQL

II.

Character Data Type;


A.

You might also like