Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

How To Create An Oracle Database?

Answer»

There are two WAYS to create a NEW database:

  • Use the Database Configuration ASSISTANT (DBCA) to create a database interactively.
  • Use the CREATE DATABASE statement to create a database MANUALLY.

There are two ways to create a new database:

2.

How To Start Oracle Sql Developer?

Answer»

To START ORACLE SQL Developer, go to sqldeveloper and click sqldeveloper.exe. The Oracle SQL Developer window shows up. Your copy of Oracle SQL Developer is running now. There will nothing under the Connections icon, because you haven't defined any connections YET.

You should visit different menu items to explore the user interface.

To start Oracle SQL Developer, go to sqldeveloper and click sqldeveloper.exe. The Oracle SQL Developer window shows up. Your copy of Oracle SQL Developer is running now. There will nothing under the Connections icon, because you haven't defined any connections yet.

You should visit different menu items to explore the user interface.

3.

Is Oracel Sql Developer Written In Java?

Answer»

Oracel SQL DEVELOPER is written in JAVA. It requires JDK 1.5, which is already included in your download file.

Oracel SQL Developer is written in Java. It requires JDK 1.5, which is already included in your download file.

4.

What Is The Reports View In Oracle Sql Developer?

Answer»

The Reports view lets you browse database information that organized by the server as special VIEWS. Information can be BROWSED include:

  • Database PARAMETERS
  • Storage information
  • Session information
  • Cursors
  • DATA objects
  • User ACCOUNTS
  • Security informaiton

The Reports view lets you browse database information that organized by the server as special views. Information can be browsed include:

5.

What Do You Think About Oracle Sql Developer?

Answer»

To conclude this introductory FAQ collection, you should think about ORACLE SQL Developer in comparison with other client tools like SQL*Plus and OracleWeb INTERFACE. SQL Developer is definitely better than the other tools, more functionality, and MUCH easier to use. The SCRIPT debugging MODE is very useful.

To conclude this introductory FAQ collection, you should think about Oracle SQL Developer in comparison with other client tools like SQL*Plus and OracleWeb interface. SQL Developer is definitely better than the other tools, more functionality, and much easier to use. The script debugging mode is very useful.

6.

What Is An Anonymous Block?

Answer»

An anonymous block is a PL/SQL code block with no name. It consists of three parts:

  • Declaration Part - Defining local variables and local procedures. Declaration part is optional.
  • EXECUTION Part - Defining execution logic with executable statements. Execution part is required.
  • Exception Part - Defining error handling logics. Exception part is optional.

Here how a complete anonymous block should LOOK like:

DECLARE // Declaration statements
BEGIN // Executable statements
EXCEPTION // Error handling statements
END;

An anonymous block is a PL/SQL code block with no name. It consists of three parts:

Here how a complete anonymous block should look like:

DECLARE // Declaration statements
BEGIN // Executable statements
EXCEPTION // Error handling statements
END;

7.

How To Declare A Local Variable?

Answer»

A local variable can be defined in the declaration part with a declaration statement, which is a variable name followed a data type identifier. Below are some examples of declaration STATEMENTS:

PROCEDURE proc_var_1 AS domain VARCHAR2(80); PRICE REAL; is_for_sale CHAR; BEGIN -- Executable statements END;

A local variable can be defined in the declaration part with a declaration statement, which is a variable name followed a data type identifier. Below are some examples of declaration statements:

8.

How To Initialize Variables With Default Values?

Answer»

There are TWO ways to assign default values to variables at the time of declaration:

  • Using key word DEFAULT - Appending "DEFAULT value" to the END of declaration statements.
  • Using assignment operator - Appending ":= value" to the end of declaration statements.

The SCRIPT below show you some examples of declaration statements with default values:

PROCEDURE proc_var_1 AS domain VARCHAR2(80) := 'fyicenter.com'; price REAL DEFAULT 999999.99; is_for_sale CHAR := 'N'; BEGIN -- EXECUTABLE statements END;

There are two ways to assign default values to variables at the time of declaration:

The script below show you some examples of declaration statements with default values:

9.

How To Assign Values To Variables?

Answer»

You can use assignment statements to ASSIGN values to VARIABLES. An assignment statement contains an assignment operator ":=", which takes the value specified on the right to the VARIABLE on LEFT. The script below show you some examples of assignment statements:

PROCEDURE proc_var_2 AS is_done BOOLEAN; counter NUMBER := 0; message VARCHAR2(80); BEGIN is_done := FASLE; counter := counter + 1; message := 'Hello world!'; END;

You can use assignment statements to assign values to variables. An assignment statement contains an assignment operator ":=", which takes the value specified on the right to the variable on left. The script below show you some examples of assignment statements:

10.

What Are The Arithmetic Operations?

Answer»

There are 4 basic arithmetic OPERATIONS on numeric values as shown in the following sample script:

PROCEDURE proc_arithmetic AS ADDITION NUMBER; subtraction NUMBER; multiplication NUMBER; DIVISION NUMBER; BEGIN addition := 7 + 8; subtraction := addition - 7; multiplication := subtraction * 5; division := multiplication / 8; -- division should contain 5 now END;

There are 4 basic arithmetic operations on numeric values as shown in the following sample script:

11.

What Are The Numeric Comparison Operations?

Answer»

PL/SQL supports 6 basic numeric comparison OPERATIONS as shown in the following SAMPLE SCRIPT:

PROCEDURE proc_comparison AS res BOOLEAN; BEGIN res := 1 = 2; res := 1 < 2; res := 1 > 2; res := 1 <= 2; res := 1 >= 2; res := 1 <> 2; -- more statements END;

PL/SQL supports 6 basic numeric comparison operations as shown in the following sample script:

12.

How Many Categories Of Data Types?

Answer»

PL/SQL data types are grouped into 4 categories:

  • SCALAR Data Types: A scalar data type holds a SINGLE value.
  • Composite Data Types: A composite data type has internal COMPONENTS, such as the elements of an array.
  • LOB Data Types: A LOB data type holds a lob locator that specify the LOCATION of a large object.
  • Reference Data Types: A reference data type holds a pointer that points to another data object.

PL/SQL data types are grouped into 4 categories:

13.

What Are The Logical Operations?

Answer»

PL/SQL supports 3 logical OPERATIONS as SHOWN in the FOLLOWING sample script:

PROCEDURE proc_comparison AS x BOOLEAN := TRUE; y BOOLEAN := FALSE; RES BOOLEAN; BEGIN res = x AND y; res = x OR y; res = NOT x; -- more statements END;

PL/SQL supports 3 logical operations as shown in the following sample script:

14.

How Many Scalar Data Types Are Supported In Pl/sql?

Answer»

PL/SQL supports many scalar data types divided into 4 GROUPS:

Numeric Types: BINARY_DOUBLE, BINARY_FLOAT, BINARY_INTEGER, DEC, DECIMAL, DOUBLE PRECISION, FLOAT, INT, INTEGER, NATURAL, NATURALN, NUMBER, NUMERIC, PLS_INTEGER, POSITIVE, POSITIVEN,REAL, SIGNTYPE, SMALLINT.

  • Character Types: CHAR, CHARACTER, LONG, LONG RAW, NCHAR, NVARCHAR2, RAW, ROWID, STRING,UROWID, VARCHAR, VARCHAR2.
  • Boolean Types: BOOLEAN.
  • Date Types: DATE, TIMESTAMP, TIMESTAMP WITH TIMEZONE, TIMESTAMP WITH LOCAL TIMEZONE,INTERVAL YEAR TO MONTH, INTERVAL DAY TO SECOND.

PL/SQL supports many scalar data types divided into 4 groups:

Numeric Types: BINARY_DOUBLE, BINARY_FLOAT, BINARY_INTEGER, DEC, DECIMAL, DOUBLE PRECISION, FLOAT, INT, INTEGER, NATURAL, NATURALN, NUMBER, NUMERIC, PLS_INTEGER, POSITIVE, POSITIVEN,REAL, SIGNTYPE, SMALLINT.

15.

How To Use "if" Statements On Multiple Conditions?

Answer»

If you have multiple blocks of codes to be EXECUTED based on different conditions, you can use the "IF ... ELSIF" statement. Here is a SAMPLE script on IF statements:

DECLARE DAY VARCHAR2; BEGIN day := 'SUNDAY'; IF day = 'THURSDAY' THEN DBMS_OUTPUT.PUT_LINE('Checking log files.'); ELSIF day = 'TUESDAY' THEN DBMS_OUTPUT.PUT_LINE('Helping developers.'); ELSIF day = 'FRIDAY' THEN DBMS_OUTPUT.PUT_LINE('Rebuild indexes.'); ELSE DBMS_OUTPUT.PUT_LINE('Reading some papers.'); END IF; END; This script should print this: Reading some papers.

If you have multiple blocks of codes to be executed based on different conditions, you can use the "IF ... ELSIF" statement. Here is a sample script on IF statements:

16.

How To Use "while" Statements?

Answer»

If you have a block of codes to be executed repeatedly based a condition, you can use the "WHILE ... LOOP" statement. Here is a SAMPLE script on WHILE STATEMENTS:

DECLARE TOTAL NUMBER; BEGIN total := 0; WHILE total &LT; 10 LOOP total := total+1; END LOOP; DBMS_OUTPUT.PUT_LINE('Total counts: ' || TO_CHAR(total)); END;

This script should print this: Total counts: 10

If you have a block of codes to be executed repeatedly based a condition, you can use the "WHILE ... LOOP" statement. Here is a sample script on WHILE statements:

This script should print this: Total counts: 10

17.

How To Use "for" Statements?

Answer»

If you have a block of CODES to be EXECUTED REPEATEDLY over a RANGE of values, you can use the "FOR ... LOOP" STATEMENT. Here is a sample script on FOR statements:

DECLARE total NUMBER := 0; BEGIN FOR i IN 1..10 LOOP total := total + i; END LOOP; DBMS_OUTPUT.PUT_LINE('Total: ' || TO_CHAR(total)); END;

If you have a block of codes to be executed repeatedly over a range of values, you can use the "FOR ... LOOP" statement. Here is a sample script on FOR statements:

18.

What Is A Procedure?

Answer»

A procedure is a named program UNIT. It consists of three parts:

  • Declaration PART - Defining the procedure name, CALLING parameters, local variables and local procedures. Declaration part is required.
  • Execution Part - Defining execution logic with executable statements. Execution part is required.
  • Exception Part - Defining error handling LOGICS. Exception part is OPTIONAL.

Here how a complete procedure should look like:

PROCEDURE name (parameter_1, parameter_2) AS -- Declaration statements BEGIN -- Executable statements EXCEPTION -- Error handling statements END;

A procedure is a named program unit. It consists of three parts:

Here how a complete procedure should look like:

19.

How To Create A Stored Procedure?

Answer»

A stored procedure is a procedure with a specified name and stored into the current database. If you want to create a stored procedure, you can use the CREATE PROCEDURE statement. The EXAMPLE script below CREATES a stored procedure:

SQL&GT; CREATE PROCEDURE GREETING AS BEGIN DBMS_OUTPUT.PUT_LINE('Welcome to WISDOM!'); END; /

Procedure created.

A stored procedure is a procedure with a specified name and stored into the current database. If you want to create a stored procedure, you can use the CREATE PROCEDURE statement. The example script below creates a stored procedure:

Procedure created.

20.

How To Execute A Stored Procedure?

Answer»

If you WANT to execute a STORED procedure, you can use the EXECUTE statement. The EXAMPLE script below shows how to executes a stored procedure:

SQL&GT; set serveroutput on; SQL> CREATE PROCEDURE Greeting AS BEGIN DBMS_OUTPUT.PUT_LINE('Welcome to wisdom!'); END; /

Procedure created. SQL> EXECUTE Greeting; Welcome to wisdom!

If you want to execute a stored procedure, you can use the EXECUTE statement. The example script below shows how to executes a stored procedure:

Procedure created. SQL> EXECUTE Greeting; Welcome to wisdom!

21.

How To Drop A Stored Procedure?

Answer»

If there is an existing stored procedure and you don't WANT it any more, you can remove it from the database by using the DROP PROCEDURE STATEMENT as shown in the following script example:

SQL&GT; CREATE PROCEDURE Greeting AS BEGIN DBMS_OUTPUT.PUT_LINE('Welcome to FYICenter!'); END; /

Procedure created. SQL> DROP PROCEDURE Greeting; Procedure dropped.

If there is an existing stored procedure and you don't want it any more, you can remove it from the database by using the DROP PROCEDURE statement as shown in the following script example:

Procedure created. SQL> DROP PROCEDURE Greeting; Procedure dropped.

22.

How To Create A Stored Function?

Answer»

A stored function is a function with a SPECIFIED NAME and stored into the current database. If you want to create a stored function, you can use the CREATE FUNCTION statement. The example script below creates a stored PROCEDURE:

SQL> CREATE OR REPLACE FUNCTION GET_SITE RETURN VARCHAR2 AS BEGIN RETURN 'FYICentere.com'; END; /

Function created.

A stored function is a function with a specified name and stored into the current database. If you want to create a stored function, you can use the CREATE FUNCTION statement. The example script below creates a stored procedure:

Function created.

23.

How To Drop A Stored Function?

Answer»

If there is an existing stored function and you don't WANT it any more, you can remove it from the DATABASE by using the DROP FUNCTION statement as shown in the following SCRIPT example:

SQL> CREATE OR REPLACE FUNCTION GET_SITE RETURN VARCHAR2 AS BEGIN RETURN 'FYICentere.com'; END; /

Function created. SQL> DROP FUNCTION GET_SITE; Function DROPPED.

If there is an existing stored function and you don't want it any more, you can remove it from the database by using the DROP FUNCTION statement as shown in the following script example:

Function created. SQL> DROP FUNCTION GET_SITE; Function dropped.

24.

How To Call A Stored Function With Parameters?

Answer»

You can define a FUNCTION that takes parameters, provide values to those parameters when calling the function. Here is a GOOD example of a function with a parameter:

SQL&GT; CREATE OR REPLACE FUNCTION GET_DOUBLE(X NUMBER) RETURN NUMBER AS BEGIN RETURN X * 2; END; /

Function created. SQL> SELECT GET_DOUBLE(36) FROM DUAL;

GET_DOUBLE(36)
--------------
72

You can define a function that takes parameters, provide values to those parameters when calling the function. Here is a good example of a function with a parameter:

Function created. SQL> SELECT GET_DOUBLE(36) FROM DUAL;

GET_DOUBLE(36)
--------------
72

25.

How To Define A Sub Procedure?

Answer»

A sub procedure is a named procedure defined and used inside another procedure or function. You need to define a sub procedure in the DECLARATION part of the enclosing procedure or function. Sub procedure definition starts with the PROCEDURE key WORD. Here is a sample script showing you how to define and use a sub procedure:

SQL> CREATE OR REPLACE PROCEDURE HR.DBA_WEEK AS PROCEDURE DBA_TASK (day VARCHAR2) AS BEGIN IF day = 'MONDAY' THEN DBMS_OUTPUT.PUT_LINE('Checking log files.'); ELSIF day = 'FRIDAY' THEN DBMS_OUTPUT.PUT_LINE('Rebuild indexes.'); ELSE DBMS_OUTPUT.PUT_LINE('Reading some papers.'); END IF; END; BEGIN DBA_TASK('MONDAY'); DBA_TASK('TUESDAY'); END; /

SQL> EXECUTE DBA_WEEK; Checking log files. Reading some papers.

A sub procedure is a named procedure defined and used inside another procedure or function. You need to define a sub procedure in the declaration part of the enclosing procedure or function. Sub procedure definition starts with the PROCEDURE key word. Here is a sample script showing you how to define and use a sub procedure:

SQL> EXECUTE DBA_WEEK; Checking log files. Reading some papers.

26.

What Happens If Recursive Calls Get Out Of Control?

Answer»

What happens if your code has bug on recursive procedure calls, which causes an infinite number nested procedure calls? The ANSWER is so GOOD. Oracle SERVER seems to offer no protection calling stack limit. The script below shows you a badly coded recursive procedure. If you run it on an Oracle 10g XE server on Windows, your server will out of control and keep using virtual memory to satisfy the growing calling stack. You have to reboot your server to control back.

SQL> CREATE OR REPLACE PROCEDURE STACK_TEST AS --Warning: do not run this procedure on your server PROCEDURE STACK AS BEGIN STACK; END; BEGIN STACK; END; /

SQL> EXECUTE STACK_TEST; (your server keep running with 100% CPU and memory usage)

What happens if your code has bug on recursive procedure calls, which causes an infinite number nested procedure calls? The answer is so good. Oracle server seems to offer no protection calling stack limit. The script below shows you a badly coded recursive procedure. If you run it on an Oracle 10g XE server on Windows, your server will out of control and keep using virtual memory to satisfy the growing calling stack. You have to reboot your server to control back.

SQL> EXECUTE STACK_TEST; (your server keep running with 100% CPU and memory usage)

27.

Can Ddl Statements Be Used In Pl/sql?

Answer»

No, you can not run any DDL STATEMENTS is PL/SQL DIRECTLY. If you try to use the DROP TABLE STATEMENT inside PL/SQL, you will get a compilation error as shown below:

(Connect to XE with SQL*PLUS) BEGIN DROP TABLE student; -- compilation error END; /

No, you can not run any DDL statements is PL/SQL directly. If you try to use the DROP TABLE statement inside PL/SQL, you will get a compilation error as shown below:

28.

How To Resolve Name Conflicts Between Variables And Columns?

Answer»

The best way to resolve name CONFLICTS is to avoid USING COLUMN names for variables.

The best way to resolve name conflicts is to avoid using column names for variables.

29.

How To Assign Query Results To Variables?

Answer»

If you want to ASSIGN RESULTS from SELECT statements to VARIABLES, you can use the INTO clause, which an extension of SELECT statements for PL/SQL. The sample code below shows some good example on INTO clause:

DECLARE total NUMBER; now DATE; fname VARCHAR2(10); LNAME VARCHAR2(10); BEGIN SELECT COUNT(*) INTO total FROM employees; DBMS_OUTPUT.PUT_LINE('Count = ' || TO_CHAR(total)); SELECT SYSDATE INTO now FROM DUAL; DBMS_OUTPUT.PUT_LINE('Now = ' || TO_CHAR(now, 'SSSSS')); SELECT first_name, last_name INTO fname, lname FROM employees WHERE employee_id = 100; DBMS_OUTPUT.PUT_LINE('Name = ' || fname || ' ' || lname); END; / Count = 107 Now = 82375 Name = STEVEN King

If you want to assign results from SELECT statements to variables, you can use the INTO clause, which an extension of SELECT statements for PL/SQL. The sample code below shows some good example on INTO clause:

30.

How To Retrieve The Count Of Updated Rows?

Answer»

After running an UPDATE STATEMENT, the database server returns a COUNT of updated rows. You can retrieve this count from a special predefined variable called SQL%ROWCOUT, as shown in the following TUTORIAL:

CREATE TABLE emp_temp AS SELECT * FROM employees; BEGIN UPDATE emp_temp SET salary = salary * 1.05 WHERE salary < 5000; DBMS_OUTPUT.PUT_LINE('# of rows updated: ' || SQL%ROWCOUNT); END; / # of rows updated: 49

After running an UPDATE statement, the database server returns a count of updated rows. You can retrieve this count from a special predefined variable called SQL%ROWCOUT, as shown in the following tutorial:

31.

How To Assign Data Of The Deleted Row To Variables?

Answer»

If a DELETE statement is deleting a SINGLE row, you can assign column values of the deleted row to variables by using the RETURNING clause, which an extension of DELETE statements for PL/SQL. The tutorial script SHOWS you how to do this:

CREATE TABLE emp_temp AS SELECT * FROM employees; DECLARE fname VARCHAR2(10); lname VARCHAR2(10); BEGIN DELETE FROM emp_temp WHERE employee_id = 100; RETURNING first_name, last_name INTO fname, lname; DBMS_OUTPUT.PUT_LINE('Name deleted = ' || fname || ' ' || lname); -- This will not work because multiple rows deleted -- DELETE FROM emp_temp WHERE employee_id > 100; -- RETURNING first_name, last_name INTO fname, lname; END; /

Name deleted = Steven King Similar to SELECT ... INTO, RETURNING ... INTO will not work if multiple rows are deleted.

If a DELETE statement is deleting a single row, you can assign column values of the deleted row to variables by using the RETURNING clause, which an extension of DELETE statements for PL/SQL. The tutorial script shows you how to do this:

Name deleted = Steven King Similar to SELECT ... INTO, RETURNING ... INTO will not work if multiple rows are deleted.

32.

What Is A Record In Pl/sql?

Answer»

RECORD is a composite data TYPE in PL/SQL. It can have MANY fields REPRESENTING data elements with different data types. Variables of RECORD type can be designed to hold data from database table ROWS. To use RECORD data type, you need to DEFINE a specific RECORD type with each field defined with a name and a data type. Then you can use your specific RECORD type to define your RECORD type variables.

RECORD is a composite data type in PL/SQL. It can have many fields representing data elements with different data types. Variables of RECORD type can be designed to hold data from database table rows. To use RECORD data type, you need to define a specific RECORD type with each field defined with a name and a data type. Then you can use your specific RECORD type to define your RECORD type variables.

33.

How To Define A Specific Record Type?

Answer»

If you WANT to define a specific RECORD type, you need to use the TYPE ... IS RECORD statement in the declaration PART of any procedure or FUNCTION. The following example script defines a RECORD type called STUDENT:

CREATE OR REPLACE PROCEDURE HELLO AS TYPE student IS RECORD ( id NUMBER(5), first_name VARCHAR(80), last_name VARCHAR(80) ); BEGIN NULL; END; /

If you want to define a specific RECORD type, you need to use the TYPE ... IS RECORD statement in the declaration part of any procedure or function. The following example script defines a RECORD type called STUDENT:

34.

How To Define A Variable Of A Specific Record Type?

Answer»

Once you have your specific RECORD type DEFINED, you can define NEW variables with this specific RECORD type like any other data type. In the sample script below, several variables are defined with a regular data type and a specific RECORD type:

CREATE OR REPLACE PROCEDURE HELLO AS TYPE student IS RECORD ( ID NUMBER(5), first_name VARCHAR(80), last_name VARCHAR(80) ); best_student student; another_student student; class_name VARCHAR2(80); BEGIN NULL; END; /

Once you have your specific RECORD type defined, you can define new variables with this specific RECORD type like any other data type. In the sample script below, several variables are defined with a regular data type and a specific RECORD type:

35.

How To Assign Values To Data Fields In Record Variables?

Answer»

If a variable is a RECORD variable, you can assign values to its data fields by using fields names prefixed with variable name as "variable.field_name". Here is a sample script assigning values to data fields of RECORD variables:

CREATE OR REPLACE PROCEDURE HELLO AS TYPE student IS RECORD ( id NUMBER(5), first_name VARCHAR(80), last_name VARCHAR(80) ); best_student student; another_student student; class_name VARCHAR2(80); BEGIN class_name := 'FYI Center 2006'; best_student.first_name := 'The'; best_student.last_name := 'Best'; END; /

If a variable is a RECORD variable, you can assign values to its data fields by using fields names prefixed with variable name as "variable.field_name". Here is a sample script assigning values to data fields of RECORD variables:

36.

How To Retrieve Values From Data Fields In Record Variables?

Answer»

If a variable is a RECORD variable with data fields assigned values, you can retrieve those values out of its data fields by using fields names PREFIXED with variable name as "variable.field_name". Here is a SAMPLE script showing you how to retrieve field values from RECORD variables:

CREATE OR REPLACE PROCEDURE HELLO AS TYPE student IS RECORD ( id NUMBER(5), first_name VARCHAR(80), last_name VARCHAR(80) ); best_student student; another_student student; class_name VARCHAR2(80); BEGIN class_name := 'FYI Center 2006'; best_student.first_name := 'The'; best_student.last_name := 'Best'; DBMS_OUTPUT.PUT_LINE('Best student = ' || best_student.first_name || ' ' || best_student.last_name); END; / Best student = The Best

If a variable is a RECORD variable with data fields assigned values, you can retrieve those values out of its data fields by using fields names prefixed with variable name as "variable.field_name". Here is a sample script showing you how to retrieve field values from RECORD variables:

37.

How To Define A Data Field As Not Null?

Answer»

When defining a specific RECORD type, you can define a data field as NOT NULL to make sure variables with this RECORD type to always have values in this field. A field defined as NOT NULL must have a default value. Here is a tutorial script showing you how to define a data field as NOT NULL:

CREATE OR REPLACE PROCEDURE HELLO AS TYPE student IS RECORD ( id NUMBER(5) NOT NULL DEFAULT 0, first_name VARCHAR(80), last_name VARCHAR(80) ); best_student student; another_student student; class_name VARCHAR2(80); BEGIN class_name := 'FYI Center 2006'; best_student.first_name := 'The'; best_student.last_name := 'Best'; DBMS_OUTPUT.PUT_LINE('Best student ID = ' || best_student.id); DBMS_OUTPUT.PUT_LINE('Best student = ' || best_student.first_name || ' ' || best_student.last_name); END; / Best student ID = 0 Best student = The Best

When defining a specific RECORD type, you can define a data field as NOT NULL to make sure variables with this RECORD type to always have values in this field. A field defined as NOT NULL must have a default value. Here is a tutorial script showing you how to define a data field as NOT NULL:

38.

How To Define A Record Variable To Store A Table Row?

Answer»

If you have a table, and want to define a RECORD variable to store all the data elements of a row from that table, you can use table_name%ROWTYPE to define the RECORD variable as shown in the following sample script:

CREATE TABLE student (id NUMBER(5) PRIMARY KEY, first_name VARCHAR(80) NOT NULL, last_name VARCHAR(80) NOT NULL); Table created. CREATE OR REPLACE PROCEDURE FYI_CENTER AS best_student student%ROWTYPE; another_student student%ROWTYPE; class_name VARCHAR2(80); BEGIN class_name := 'FYI Center 2006'; best_student.first_name := 'The'; best_student.last_name := 'Best'; DBMS_OUTPUT.PUT_LINE('Best student ID = ' || best_student.id); DBMS_OUTPUT.PUT_LINE('Best student = ' || best_student.first_name || ' ' || best_student.last_name); END; / Best student ID = Best student = The Best

If you have a table, and want to define a RECORD variable to store all the data elements of a row from that table, you can use table_name%ROWTYPE to define the RECORD variable as shown in the following sample script:

39.

How To Assign A Table Row To A Record Variable?

Answer»

If you have a table, and want to assign a data row of that table to a RECORD variable, you need to DEFINE this RECORD variable to MATCH the table column structure, then use the SELECT ... INTO statement to assign a data row that RECORD variable. The script below shows you how to do this:

CREATE OR REPLACE PROCEDURE FYI_CENTER AS MANAGER employees%ROWTYPE; BEGIN SELECT * INTO manager FROM employees WHERE employee_id = 100; DBMS_OUTPUT.PUT_LINE('My manager = ' || manager.first_name || ' ' || manager.last_name); END; / My manager = Steven KING

If you have a table, and want to assign a data row of that table to a RECORD variable, you need to define this RECORD variable to match the table column structure, then use the SELECT ... INTO statement to assign a data row that RECORD variable. The script below shows you how to do this:

40.

How To Update A Table Row With A Record?

Answer»

If you have a RECORD VARIABLE with data fields matching a table structure, you can update a row in this table with this RECORD variable using the UPDATE ... SET ROW statement as shown in the SAMPLE script below:

CREATE TABLE emp_temp AS SELECT * FROM employees; CREATE OR REPLACE PROCEDURE FYI_CENTER AS manager employees%ROWTYPE; BEGIN SELECT * INTO manager FROM employees WHERE employee_id = 100; manager.employee_id := 299; INSERT INTO emp_temp VALUES manager; manager.first_name := 'FYI'; manager.last_name := 'Center'; UPDATE emp_temp SET ROW = manager WHERE employee_id = 299; DBMS_OUTPUT.PUT_LINE('# rows updated = ' || SQL%ROWCOUNT); END; / # rows updated = 1

If you have a RECORD variable with data fields matching a table structure, you can update a row in this table with this RECORD variable using the UPDATE ... SET ROW statement as shown in the sample script below:

41.

How To Define A Variable To Match A Table Column Data Type?

Answer»

If you have a table, and want to define some variables to have exactly the same DATA types as some columns in that table, you can use table_name.column_name%TYPE as data types to define those variables. The TUTORIAL sample below SHOWS you how to do this:

CREATE OR REPLACE PROCEDURE FYI_CENTER AS my_email employees.email%TYPE; my_salary employees.salary%TYPE; BEGIN SELECT email, salary INTO my_email, my_salary FROM employees WHERE employee_id = 101; DBMS_OUTPUT.PUT_LINE('My email = ' || my_email); DBMS_OUTPUT.PUT_LINE('My salary = ' || my_salary); 73 END; / My email = NKOCHHAR My salary = 17000

If you have a table, and want to define some variables to have exactly the same data types as some columns in that table, you can use table_name.column_name%TYPE as data types to define those variables. The tutorial sample below shows you how to do this:

42.

How Many Types Of Cursors Supported In Pl/sql?

Answer»

PL/SQL SUPPORTS two types of CURSORS:

  • The IMPLICIT cursor - A SINGLE default cursor that automatically connects to the last DML statement executed.
  • Explicit cursors - User defined cursors with specific DML STATEMENTS and execution statuses.

PL/SQL supports two types of cursors:

43.

What Is The Implicit Cursor?

Answer»

There is only ONE implicitly CURSOR in a session. The implicit cursor is the cursor automatically defined by PL/SQL for you. Whenever a SQL statement is executed, this cursor will be assigned to represent the execution of this statement. This implicit cursor is called SQL. It has MANY attributes representing some GOOD INFORMATION about the execution like:

  • SQL%FOUND - True, if the SQL statement has changed any rows.
  • SQL%NOTFOUND - True, if the SQL statement has not changed any rows.
  • %ISOPEN - True, if there is a SQL statement being associated to the cursor.

There is only one implicitly cursor in a session. The implicit cursor is the cursor automatically defined by PL/SQL for you. Whenever a SQL statement is executed, this cursor will be assigned to represent the execution of this statement. This implicit cursor is called SQL. It has many attributes representing some good information about the execution like:

44.

How To Define An Explicit Cursor?

Answer»

An explicit CURSOR must be defined in the declaration part of a procedure or function with the CURSOR ... IS STATEMENT as shown in the following sample script:

DECLARE CURSOR c_list IS SELECT * FROM countries; CURSOR t_list IS SELECT * FROM employees WHERE employee_id = 100; BEGIN NULL; END; /

An explicit cursor must be defined in the declaration part of a procedure or function with the CURSOR ... IS statement as shown in the following sample script:

45.

How To Open And Close An Explicit Cursor?

Answer»

An EXISTING cursor can be opened or CLOSED by the OPEN or CLOSE STATEMENT as shown in the following SAMPLE script:

DECLARE CURSOR c_list IS SELECT * FROM countries; CURSOR t_list IS SELECT * FROM employees WHERE employee_id = 100; BEGIN OPEN c_list; OPEN t_list; CLOSE c_list; CLOSE t_list; END; /

An existing cursor can be opened or closed by the OPEN or CLOSE statement as shown in the following sample script:

46.

How To Retrieve Data From An Cursor To A Record?

Answer»

If you have a cursor opened ready to use, you can also use the FETCH statement to retrieve data from the cursor into a RECORD variable as shown in the tutorial exercise below:

CREATE OR REPLACE PROCEDURE FYI_CENTER AS CURSOR t_list IS SELECT first_name, last_name FROM employees; TYPE name_rec IS RECORD ( f_name VARCHAR2(10), l_name VARCHAR2(10) ); n name_rec; BEGIN OPEN t_list; FETCH t_list INTO n; DBMS_OUTPUT.PUT_LINE('Name = ' || n.f_name || ' ' || n.l_name); FETCH t_list INTO n; DBMS_OUTPUT.PUT_LINE('Name = ' || n.f_name || ' ' 75 || n.l_name); CLOSE t_list; END; / Name = Ellen ABEL Name = Sundar Ande

If you have a cursor opened ready to use, you can also use the FETCH statement to retrieve data from the cursor into a RECORD variable as shown in the tutorial exercise below:

47.

How To Use Fetch Statement In A Loop?

Answer»

If you have a cursor OPENED ready to use, you can also use the FETCH statement in a loop to retrieve DATA from the cursor more efficiently. But you NEED to remember to use an EXIT statement break the loop when the cursor pointer reaches the end. The script below gives you a good example:

CREATE OR REPLACE PROCEDURE FYI_CENTER AS CURSOR emp_cur IS SELECT * FROM EMPLOYEES WHERE manager_id = 101; emp_rec employees%ROWTYPE; BEGIN OPEN emp_cur; LOOP FETCH emp_cur INTO emp_rec; EXIT WHEN emp_cur%NOTFOUND; DBMS_OUTPUT.PUT_LINE('Name = ' || emp_rec.first_name || ' ' || emp_rec.last_name); END LOOP; CLOSE emp_cur; END; /

If you have a cursor opened ready to use, you can also use the FETCH statement in a loop to retrieve data from the cursor more efficiently. But you need to remember to use an EXIT statement break the loop when the cursor pointer reaches the end. The script below gives you a good example:

48.

How To Use An Explicit Cursor Without Open Statements?

Answer»

f you want to open a CURSOR and loop through its data rows in quick way, you can USE the FOR ... IN ... LOOP statement in the same way as the implicit cursor. The following TUTORIAL exercise gives you a good EXAMPLE:

CREATE OR REPLACE PROCEDURE FYI_CENTER AS CURSOR emp_cur IS SELECT * FROM employees WHERE manager_id = 101; BEGIN FOR row IN emp_cur LOOP DBMS_OUTPUT.PUT_LINE('Name = ' || row.first_name || ' ' || row.last_name); END LOOP; END; / Name = Nancy Greenberg Name = Jennifer Whalen Name = Susan Mavris Name = Hermann Baer Name = Shelley Higgins

f you want to open a cursor and loop through its data rows in quick way, you can use the FOR ... IN ... LOOP statement in the same way as the implicit cursor. The following tutorial exercise gives you a good example:

49.

Can Multiple Cursors Being Opened At The Same Time?

Answer»

Yes, multiple cursors can be opened at the same time. See the following EXAMPLE:

CREATE OR REPLACE PROCEDURE FYI_CENTER AS CURSOR emp_cur IS SELECT * FROM employees; emp_rec employees%ROWTYPE; CURSOR dpt_cur IS SELECT * FROM departments; dpt_rec departments%ROWTYPE; BEGIN OPEN emp_cur; OPEN dpt_cur; FETCH emp_cur INTO emp_rec; FETCH dpt_cur INTO dpt_rec; DBMS_OUTPUT.PUT_LINE('DEPARTMENT name = ' || dpt_rec.department_name); DBMS_OUTPUT.PUT_LINE('Employee name = ' || emp_rec.first_name || ' ' || emp_rec.last_name); CLOSE emp_cur; CLOSE dpt_cur; END; / Department name = Administration Employee name = Steven King

Yes, multiple cursors can be opened at the same time. See the following example:

50.

What Is A Cursor Variable?

Answer»

A cursor variable is a variable of a specific REF CURSOR data TYPE, which is a pointer to a data STRUCTURE resource CONNECTS to QUERY statement result, similar to the CURSOR data type.. The advantage of using cursor variables is that cursor variables can be used as variables to PASS between procedures and functions.

A cursor variable is a variable of a specific REF CURSOR data type, which is a pointer to a data structure resource connects to query statement result, similar to the CURSOR data type.. The advantage of using cursor variables is that cursor variables can be used as variables to pass between procedures and functions.