Reading:  

To the point guide to PL/SQL


Working with Constants

PL/SQL constant holds a declared value and cannot change it in the program. The declaration of constant has its data type, name and value.

Syntax of Constant Declaration:

Constant_name CONSTANT datatype[Size] := Value; 

Here are some important details for above syntax

  • Constant_name: The constant name.
  • CONSTANT: is a keyword.
  • Data type: PL/SQL data type.
  • Size: size to store the maximum size value.
  • Value: User must initialize the variable value. User cannot re-assign/change it.

Example:

In this example Stud_name is constant variable where its value cannot alter.

DECLARE
	Stud_num(3) := 8
	Stud_name CONSTANT varchar2(25) := 'Suman';
 BEGIN
	dbms_output.put_line(' Student number is: ' || Stud_num );
	dbms_output.put_line('Declared Constant:');
	dbms_output.put_line(' Student name is: ' || Stud_name);	
END;
/

Result: 

Student number is: 8
Declared Constant:
Student name is: Suman
PL/SQL procedure successfully operation.

 

The PL/SQL Literals

A literal is same as a constant and an explicit value like numeric, string, character, and boolean which are not represented by identifier. Literals are usually case sensitive.

  • Numeric Literals

    This literal can be used in arithmetic expressions

    Example:

    number1 := 3.125346e3;  -- numeric literal
    number2 := -7300.00;  -- numeric literal 
  • Character Literals

    These literals include all the characters which are printable in the set like numbers, letters, spaces, and symbols.

    Example:
    'A' '%' '9' ' ' 'z' '('
    name1 VARCHAR2(1) := 'n'; -- character literal 
  • String Literals

    This literal is a sequence of zero or group of characters enclosed by single quotes, like
    'Oracle’, '$13,00,000', '1-MAY-15' 
  • BOOLEAN Literals

    This literal values are the predefined:
    TRUE, FALSE, and NULL. 
  • Date and Time Literals

    It have various date formats like 1-MAY-15' or '1-MAY-15 01:14:06 AM'.

Example

DECLARE  
  msg1   VARCHAR2(1000);
  BEGIN
  msg1 := 'I am learning PL/SQL!!!';
 END;
/

Result

'I am learning PL/SQL!!!
PL/SQL procedure successfully completed.
 

In next part of this guide we will quickly familiarize ourselves with PL/SQL Operators

 

Description

This tutorial focus on PL/SQL and covers the below topics

  • What is PL/SQL?
  • Environment Setup
  • Variables
  • Data Types 
  • Constants 
  • Operators
  • Conditions
  • Loops
  • Strings
  • Arrays
  • Procedures
  • Functions
  • Cursors
  • Records
  • Exceptions
  • Packages
  • Triggers
  • Collections
  • Transactions
  • Date & Time
  • Object Oriented
  • DBMS Output

If you found any error with any of the docs please let us know.

 



Learning Objectives

Learn PL/SQL from a beginners perspective, this guide can also help you if you are trying to brush up your PLSQL skills

Author: Subject Coach
Added on: 20th Apr 2015

You must be logged in as Student to ask a Question.

None just yet!