Oracle PL/SQL is an advanced procedural language designed to enhance SQL functionalities in Oracle Database environments. Comments can be used to make code more human-readable. It is a kind of note that programmers usually add while writing the code. In this article, we will learn about Comments in detail along with the types and benefits of using Comments.
The comment makes the code more easy to read and understand. these statements we write as comments are not executed by the compiler and simply get ignored while executing the code. For example, suppose you are writing a PL/SQL block for Manipulating data in the database but the queries you are writing are way too complex for to understand others, hence in this case we can specify the comments wherever needed to make queries more readable and understandable by ourselves or by other programmers.
In databases sometimes we require some context about the query. To specify that context we use comments. In databases, comments are non-executable text that is inserted along with the query. We can specify comments while creating the following:
- Procedures in PL/SQL
- Views in DB
- Functions in PL/SQL
- Triggers in PL/SQL
Types of PL/SQL Comments
Like most programming languages, we can also write comments in PL/SQL. Oracle PL/SQL supports two main types of comments:
Comments in PL/SQLA Single line comment starts with the (--) double hyphen. It will affect the whole line (till end of line). We don't need to specify the end. Everything after the hyphens on that line is treated as a comment. They are helpful for brief explanations or temporary disabling of code lines during debugging.
Syntax
-- This is a single line comment
-- Here is a program for addition of two numbers in PL/SQL
DELIMITER //
CREATE PROCEDURE add_two_numbers()
BEGIN
-- Declare variables a, b, and c of datatype INT
DECLARE a INT;
DECLARE b INT;
DECLARE c INT;
-- Assign value to variable a
SET a := 10;
-- Assign value to variable b
SET b := 20;
-- Performing addition of a and b and storing it into c
SET c := a + b;
-- Printing the result
SELECT CONCAT('Sum is ', c) AS result;
END //
DELIMITER ;
Output:
Output after executing procedure
Explanation: As we can see after calling the procedure named add_two_numbers, all the comments are ignored by the compiler and the result of addition is returned.
Instead of commenting single line, we could also set of consecutive lines at ones using multi-line comments in PL/SQL. Multi-line comment in PL/SQL starts with a forward slash and asterisk (/*) and ends with an asterisk and forward slash (*/). Text or code between /* and */ will get ignored by the compiler.
Syntax
/* Multi line comment starts
. . . continue . . .
. . . continue . . .
. . . continue . . .
Multi line comment ends */
/* demonstrate the use of IN parameter in PL/SQL */
delimiter //
CREATE PROCEDURE inProcedure(IN param INT)
BEGIN
/*
using the IN parameter in procedure
adding the 5 to parameter and then printing the result
*/
select param + 5 as result;
END //
DELIMITER ;
Output:
Result after calling procedure inProcedure(23)
Explanation: Here, we have called the inProcedure(), with passing 23 as a parameter. program is adding 5 to 23 and we are getting results as 28. The Point to note here is that we have used multi-line comments to explain the program.
- Comments make your code easy to read and understand.
- Comments play a crucial role while debugging, They assist you by easily understanding the logic behind the code.
- Comments provide ways to document your code by explaining the use of variables,and functions.
- Commenting can be used to mark future improvements.
- Commenting is used in documenting the code. especially in Version control systems.
Important Points About PL/SQL Comments
- Too many comments, especially redundant ones, can clutter code. Use comments to explain the "why" rather than the "what".
- Unlike some programming languages, you cannot nest multi-line comments in Oracle PL/SQL.
- Multi-line comments can also be used to temporarily disable large blocks of code, which is helpful during debugging or testing.
- In Oracle tools like SQL*Plus, comments are ignored when executed, but if you store scripts with comments, they can serve as documentation.
Similar Reads
SQL Comments SQL comments play an essential role in enhancing the readability, maintainability, and documentation of our SQL code. By using comments effectively, developers can provide context, clarify complex logic, and temporarily disable parts of the code. Whether we're working alone or collaborating with a t
4 min read
PL/SQL Functions PL/SQL functions are reusable blocks of code that can be used to perform specific tasks. They are similar to procedures but must always return a value. A function in PL/SQL contains:Function Header: The function header includes the function name and an optional parameter list. It is the first part o
4 min read
Solidity - Comments Comments are an important aspect of programming as they help in providing clarity and understanding to the code. They allow developers to document the code and explain its purpose, making it easier for others to read and maintain the code. Solidity, being a programming language, also supports the us
4 min read
PL/SQL JOIN JOIN is a powerful operation in PL/SQL that allows us to combine data from two or more related tables based on a common key. The PL/SQL JOIN is used to select data from multiple tables using this key to match records. This powerful PL/SQL feature allows for selecting data across multiple tables usin
5 min read
PL/SQL Packages PL/SQL is a programming language that extends SQL by incorporating features of procedural programming languages. It is a highly structured language. A key feature of PL/SQL is the use of packages, which allow developers to group related procedures, functions, variables, and other PL/SQL constructs i
6 min read