Oracle Database

  Home  Oracle  Oracle Database


“Learn Oracle database programming with hundreds of Interview Questions and Answers and examples and get a better job as an Oracle DBA with this basic and advance Oracle Database Interview Questions and Answers guide”



430 Oracle Database Questions And Answers

361⟩ How To Run SQL Functions in PL/SQL?

Of course, you can run SQL functions in SQL statements. But many SQL functions can also be executed in regular PL/SQL statements, as shown in the following sample script:

DECLARE

now DATE;

id NUMBER;

str VARCHAR2(40);

BEGIN

now := SYSDATE;

DBMS_OUTPUT.PUT_LINE('Time #1 = ' ||

TO_CHAR(now,'HH24:MI:SS'));

SELECT SYSDATE INTO now FROM DUAL;

DBMS_OUTPUT.PUT_LINE('Time #2 = ' ||

TO_CHAR(now,'HH24:MI:SS'));

id := UID;

DBMS_OUTPUT.PUT_LINE('User id #2 = ' || TO_CHAR(id));

SELECT UID INTO id FROM DUAL;

DBMS_OUTPUT.PUT_LINE('User id #2 = ' || TO_CHAR(id));

str := CHR(70)||CHR(89)||CHR(73);

DBMS_OUTPUT.PUT_LINE('String #1 = ' || str);

SELECT CHR(70)||CHR(89)||CHR(73) INTO str FROM DUAL;

DBMS_OUTPUT.PUT_LINE('String #2 = ' || str);

END;

/

Time #1 = 21:41:16

Time #2 = 21:41:16

User id #2 = 33

User id #2 = 33

String #1 = ggl

String #2 = ggl

 161 views

362⟩ How To Retrieve the Count of Updated Rows?

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

 141 views

363⟩ What Is the Implicit Cursor in Oracle?

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.

► SQL%ROWCOUNT - The number of rows affected by the SQL statement.

See the example below on how to use the implicit cursor:

CREATE TABLE student (id NUMBER(5) PRIMARY KEY,

first_name VARCHAR(80) NOT NULL,

last_name VARCHAR(80) NOT NULL);

Table created.

 141 views

364⟩ How To Assign Data of the Deleted Row to Variables?

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.

 150 views

365⟩ What Is a RECORD in PL/SQL?

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.

 125 views

366⟩ How To Define a Specific RECORD Type?

f 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;

/

 167 views

367⟩ How To Define a Variable of a Specific RECORD Type?

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;

/

 155 views

368⟩ How To Assign Values to Data Fields in RECORD Variables?

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 := 'ggl Center 2006';

best_student.first_name := 'The';

best_student.last_name := 'Best';

END;

/

 163 views

369⟩ How To Retrieve Values from Data Fields in RECORD Variables?

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 := 'ggl 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

 136 views

370⟩ How To Define a Data Field as NOT NULL?

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 := 'ggl 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

 133 views

371⟩ How To Define a RECORD Variable to Store a Table Row?

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 ggl_CENTER AS

best_student student%ROWTYPE;

another_student student%ROWTYPE;

class_name VARCHAR2(80);

BEGIN

class_name := 'ggl 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

 140 views

372⟩ How To Assign a Table Row to a RECORD Variable?

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 ggl_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

 138 views

373⟩ How To Insert a Record into a Table?

If you have a RECORD variable with data fields matching a table structure, you can insert a row to this table with this RECORD variable using the INSERT statement as shown in the example below:

CREATE TABLE emp_temp AS SELECT * FROM employees;

CREATE OR REPLACE PROCEDURE ggl_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;

DBMS_OUTPUT.PUT_LINE('# rows inserted = '

|| SQL%ROWCOUNT);

END;

/

# rows inserted = 1

 139 views

374⟩ How To Update a Table Row with a Record?

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 ggl_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 := 'ggl';

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

 143 views

375⟩ How To Define a Variable to Match a Table Column Data Type?

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 ggl_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);

END;

/

My email = NKOCHHAR

My salary = 17000

 129 views

376⟩ What Is a Cursor in Oracle?

A cursor looks like a variable, but it is not a variable. A cursor looks like a procedure, but it is not a procedure. A cursor is a cursor. It is a logical representation of a resource connects to a set of data rows related to a DML statement. A cursor is consists of:

► A DML statement with or without parameters.

► A status to indicate whether it is connected or not.

► A pointer to indicate the current row in the resource set.

► A number of attributes about the cursor, like FOUND, NOTFOUND and ROWCOUNT.

 156 views

377⟩ How Many Types of Cursors Supported in PL/SQL?

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.

 138 views

378⟩ What Is the Oracle Implicit 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:

► SQL%FOUND - True, if the SQL statement has changed any rows.

► SQL%NOTFOUND - True, if the SQL statement has not changed any rows.

► SQL%ROWCOUNT - The number of rows affected by the SQL statement.

► %ISOPEN - True, if there is a SQL statement being associated to the cursor.

 128 views

379⟩ How To Use Attributes of the Implicit Cursor in Oracle?

Right after executing a DML statement, you retrieve any attribute of the implicit cursor by using SQL%attribute_name, as shown in the following tutorial exercise:

CREATE TABLE student (id NUMBER(5) PRIMARY KEY,

first_name VARCHAR(80) NOT NULL,

last_name VARCHAR(80) NOT NULL);

Table created.

DECLARE

id NUMBER;

first_name CHAR(10);

BEGIN

id := 29;

first_name := 'Bob';

INSERT INTO student VALUES(id, first_name, 'Henry');

first_name := 'Joe';

INSERT INTO student VALUES(id+1, first_name, 'Bush');

DBMS_OUTPUT.PUT_LINE('# of rows inserted: '

|| SQL%ROWCOUNT);

first_name := 'ggl';

UPDATE student SET first_name = first_name WHERE id = 29;

IF SQL%FOUND THEN

DBMS_OUTPUT.PUT_LINE('# of rows updated: '

|| SQL%ROWCOUNT);

END IF;

UPDATE student SET first_name = first_name

WHERE id = id+1;

IF SQL%NOTFOUND THEN

DBMS_OUTPUT.PUT_LINE('No records updated.');

END IF;

DELETE FROM student WHERE id = id;

DBMS_OUTPUT.PUT_LINE('# of rows deleted: '

|| SQL%ROWCOUNT);

END;

/

# of rows inserted: 1

# of rows updated: 1

No records updated.

# of r

 143 views

380⟩ How To Loop through Data Rows in the Implicit Cursor?

You use the FOR ... IN ... LOOP statement to loop through data rows in the implicit cursor as the following syntax:

FOR row IN dml_statement LOOP

(statement block with row.field)

END LOOP;

Here "row" is a local RECORD type variable with fields automatically defined to match the fields in the data rows resulted from the DML statement. Here is a good tutorial exercise on loop through data rows with the implicit cursor:

BEGIN

FOR row IN (SELECT * FROM employees

WHERE manager_id = 101) LOOP

DBMS_OUTPUT.PUT_LINE('Name = ' || row.last_name);

END LOOP;

END;

/

Name = Greenberg

Name = Whalen

Name = Mavris

Name = Baer

Name = Higgins

 140 views