PL/SQL Flashcards
What is the basic syntax for a block?
DECLARE
BEGIN
EXCEPTION
END
What are ‘Anonymous Blocks’?
When you click the run button you get ‘compile runs’
What are ‘Named Blocks’
When you click the run button you get ‘compiles’
P code is generated
Declare variable v_foo and assign it the value 7
v_foo NUMBER := 7
Assign the number of teachers in the COSC department into the v_cosc variable from the teachers table
SELECT COUNT(*)
INTO v_cosc
FROM teachers
WHERE dept = ‘COSC’;
What are the 2 errors you may get when using a SELECT … INTO statement?
- NO_DATA_FOUND
Get when 0 rows are returned - TOO_MANY_ROWS
Get when more than 1 row is returned
Write this as PL/SQL
If(grade > 50) { System.out.println("Pass"); } else if (grade > 0) { System.out.println("Fail"); } else { System.out.printlnt("W"); }
IF grade > 50 THEN
dbms_output.put_line(‘Pass’);
ELSIF grade > 0 THEN
dbms_output.put_line(‘Fail’);
ELSE
dbms_output.put_line(‘W’);
END IF;
Write this as PL/SQL
while (i < 5) {
System.out.println(i);
i++;
}
WHILE i < 5 LOOP
System.out.println(i)
i := i + 1;
END LOOP;
Declare a varaible v_name to be the same data type as name from the Employees table
v_name Employees.name%TYPE;
Write an exception when theres SELECT … INTO returns no rows to print out Oops!
EXCEPTION
WHEN NO_DATA_FOUND THEN dbms_output.put_line(‘Oops!’);
END;
What are bind variables?
Variables created in SQL*Plus and that can be referenced in PL/SQL
Declare a bind foo variable to equal 7
:foo := 7;
Print out ‘Hello World’ in PL/SQL. Include line that enables printing
SET SERVEROUTPUT ON
dbms_output.put_line(‘Hello World’);