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

321⟩ How To Convert Character Types to Numeric Types?

You can convert character types to numeric types in two ways:

► Explicitly by using TO_NUMBER() function.

► Implicitly by putting character data in a numeric operation.

The sample script below shows you how to convert character types to numeric types:

PROCEDURE proc_convert_1 AS

start_time CHAR(5);

finish_time CHAR(5);

elapsed_time NUMBER(5);

BEGIN

start_time := '12052';

finish_time := '15314';

elapsed_time := TO_NUMBER(finish_time)

- TO_NUMBER(start_time);

elapsed_time := finish_time - start_time; -- same as above

END;

 132 views

322⟩ What Are the Execution Control Statements in Oracle?

PL/SQL supports three groups of execution control statements:

► IF Statements - Conditionally executes a block of statements.

► CASE Statements - Selectively executes a block of statements.

► LOOP Statements - Repeatedly executes a block of statements.

► GOTO Statements - Unconditional changes the execution flow to a specified statement.

The script below shows some execution control statements:

DECLARE

total NUMBER;

BEGIN

total := 0;

LOOP

total := total+1;

IF total >= 10 THEN

GOTO print;

END IF;

END LOOP;

<>

DBMS_OUTPUT.PUT_LINE('Total counts: ' || TO_CHAR(total));

END;

This script should print this:

Total counts: 10

 155 views

323⟩ How To Use "WHILE" Statements in Oracle?

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

 121 views

324⟩ How To Use "FOR" Statements in Oracle?

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;

Note that temporary variable "i" used in the FOR loop needs no declaration. This script should print this:

Total: 55

 137 views

325⟩ What Is NULL in PL/SQL?

NULL is a reserved key word and it stands for two things in PL/SQL:

► NULL is an executable statement, and means doing nothing.

► NULL is a data value, and means no value.

The following sample script shows you examples of using NULL keyword:

DECLARE

next_task CHAR(80);

BEGIN

next_task := NULL; -- NULL value

IF next_task IS NOT NULL THEN

DBMS_OUTPUT.PUT_LINE('I am busy.');

ELSE

DBMS_OUTPUT.PUT_LINE('I am free.');

END IF;

IF next_task IS NULL THEN

NULL; -- NULL statement

ELSE

DBMS_OUTPUT.PUT_LINE('... working on ' || next_task);

END IF;

END;

This script should print this:

I am free.

 126 views

326⟩ How To Use "IF" Statements on Multiple Conditions?

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.

 121 views

327⟩ How To Test NULL Values?

There ate two special comparison operators you can use on NULL values:

► "variable IS NULL" - Returns TRUE if the variable value is NULL.

► "variable IS NOT NULL" - Return TRUE if the variable value is not NULL.

The following sample script shows you examples of comparing NULL values:

DECLARE

next_task CHAR(80);

BEGIN

next_task := NULL;

IF next_task IS NOT NULL THEN

DBMS_OUTPUT.PUT_LINE('I am busy.');

ELSE

DBMS_OUTPUT.PUT_LINE('I am free.');

END IF;

IF next_task IS NULL THEN

NULL;

ELSE

DBMS_OUTPUT.PUT_LINE('... working on ' || next_task);

END IF;

END;

Note that "variable = NULL" is not a valid operation. This script should print this:

I am free.

 137 views

328⟩ What Is a Procedure in Oracle?

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;

 143 views

329⟩ What Is an Oracle Function?

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

► Declaration Part - Defining the function name, calling parameters, return value type, 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 logic. Exception part is optional.

Here how a complete procedure should look like:

FUNCTION name (parameter_1, parameter_2) RETURN type AS

-- Declaration statements

BEGIN

-- Executable statements

RETURN value;

EXCEPTION

-- Error handling statements

END;

 137 views

330⟩ How To Define an Anonymous Procedure without Variables?

Anonymous procedure is a procedure without any name. If you don't have any variables to declare, you can define an anonymous procedure by using the BEGIN keyword directly in SQL*Plus as shown in the following tutorial script:

SQL> set serveroutput on;

SQL> begin

2 dbms_output.put_line('Welcome to globalguideline!');

3 end;

4 /

Welcome to globalguideline!

PL/SQL procedure successfully completed.

"set serveroutput on;" allows dbms_output.put_line() to work.

"/" runs the anonymous block, which print the "Welcome to globalguideline!" message.

 143 views

331⟩ How To Define an Anonymous Procedure with Variables?

Anonymous procedure is a procedure without any name. If you have some variables to declare, you can define an anonymous procedure by using the DECLARE keyword in SQL*Plus as shown in the following tutorial script:

SQL> set serveroutput on;

SQL> declare

2 site char(80) := 'globalguideline';

3 begin

4 dbms_output.put_line('Welcome to ' || site);

5 end;

6 /

Welcome to globalguideline

PL/SQL procedure successfully completed.

SQL> /

Welcome to globalguideline

PL/SQL procedure successfully completed.

"/" runs the anonymous block, which print the "Welcome to globalguideline" message.

 127 views

332⟩ How To Create a Stored Procedure in Oracle?

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> CREATE PROCEDURE Greeting AS

2 BEGIN

3 DBMS_OUTPUT.PUT_LINE('Welcome to globalguideline!');

4 END;

5 /

Procedure created.

 140 views

333⟩ How To Execute a Stored Procedure in Oracle?

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> set serveroutput on;

SQL> CREATE PROCEDURE Greeting AS

2 BEGIN

3 DBMS_OUTPUT.PUT_LINE('Welcome to globalguideline!');

4 END;

5 /

Procedure created.

SQL> EXECUTE Greeting;

Welcome to globalguideline!

 138 views

334⟩ How To Drop a Stored Procedure in Oracle?

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> CREATE PROCEDURE Greeting AS

2 BEGIN

3 DBMS_OUTPUT.PUT_LINE('Welcome to globalguideline!');

4 END;

5 /

Procedure created.

SQL> DROP PROCEDURE Greeting;

Procedure dropped.

 113 views

335⟩ How To Pass Parameters to Procedures in Oracle?

Store procedures or functions can take parameters. You need to define parameters while defining the procedure, and providing values to parameters while calling the procedure. The script below shows you how to do this:

SQL> CREATE OR REPLACE PROCEDURE DBA_TASK (day VARCHAR2) AS

2 BEGIN

3 IF day = 'MONDAY' THEN

4 DBMS_OUTPUT.PUT_LINE('Checking log files.');

5 ELSIF day = 'FRIDAY' THEN

6 DBMS_OUTPUT.PUT_LINE('Rebuild indexes.');

7 ELSE

8 DBMS_OUTPUT.PUT_LINE('Reading some papers.');

9 END IF;

10 END;

11 /

SQL> EXECUTE DBA_TASK('MONDAY');

Checking log files.

SQL> EXECUTE DBA_TASK('SUNDAY');

Reading some papers.

As you can see, procedures with parameters can make procedures more flexible.

 124 views

336⟩ How To Create a Stored Function in Oracle?

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

2 RETURN VARCHAR2 AS

3 BEGIN

4 RETURN 'globalguidelinee.com';

5 END;

6 /

Function created.

 128 views

337⟩ How To Call a Stored Function in Oracle?

A stored function can be called as part of expression in any PL/SQL statement. One simplest way to call a stored function is to a dummy SELECT statement as shown in the following tutorial script using SQL*Plus:

SQL> CREATE OR REPLACE FUNCTION GET_SITE

2 RETURN VARCHAR2 AS

3 BEGIN

4 RETURN 'globalguidelinee.com';

5 END;

6 /

Function created.

SQL> SELECT get_site() FROM dual;

GET_SITE()

---------------------------------

globalguidelinee.com

DUAL is not a real table or view. It is commonly used to with SELECT statement to evaluate expressions.

 159 views

338⟩ How To Drop a Stored Function?

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

2 RETURN VARCHAR2 AS

3 BEGIN

4 RETURN 'globalguidelinee.com';

5 END;

6 /

Function created.

SQL> DROP FUNCTION GET_SITE;

Function dropped.

 136 views

339⟩ How To Call a Stored Function with Parameters?

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> CREATE OR REPLACE FUNCTION GET_DOUBLE(X NUMBER)

2 RETURN NUMBER AS

3 BEGIN

4 RETURN X * 2;

5 END;

6 /

Function created.

SQL> SELECT GET_DOUBLE(36) FROM DUAL;

GET_DOUBLE(36)

--------------

72

 123 views

340⟩ How To Define an Oracle Sub Procedure?

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

2 PROCEDURE DBA_TASK (day VARCHAR2) AS

3 BEGIN

4 IF day = 'MONDAY' THEN

5 DBMS_OUTPUT.PUT_LINE('Checking log files.');

6 ELSIF day = 'FRIDAY' THEN

7 DBMS_OUTPUT.PUT_LINE('Rebuild indexes.');

8 ELSE

9 DBMS_OUTPUT.PUT_LINE('Reading some papers.');

10 END IF;

11 END;

12 BEGIN

13 DBA_TASK('MONDAY');

14 DBA_TASK('TUESDAY');

15 END;

16 /

SQL> EXECUTE DBA_WEEK;

Checking log files.

Reading some papers.

 128 views