Let's discuss how to run PL/SQL program, a power-up for your Oracle database. Imagine adding some brains and muscles to your plain SQL. Oracle built it, and PL/SQL allows you to do more than just fetch and manipulate data. It gives you the power to create high-performance, complex programs within the database itself.
Why is it so handy? PL/SQL brings all of the comfortable programming concepts like loops for iteration, if/then statements for decision making, and smart error handling into the realm of SQL. This implies that developers can create robust applications that communicate with the database in a much more managed, and effective manner. If you're a database administrator or developer looking for best-in-class application performance and dependability, you will want to discover how to program using PL/SQL.
PL/SQL provides the developer the ability to construct reusable code blocks, put main business rules inside the database itself, and mechanize activities often done manually, all without imposing excessive complexity. Being constructed the way it is, and due to the fact that it is completely compatible with SQL, learning how to run PL/SQL programs enables you to run database commands, alter data, and control transactions with more accuracy and security than SQL would permit. For those determined to get the most out of their database application capabilities, enhancing resource utilization, and enhancing the reliability and efficiency of applications trying to behave like "well-oiled machines," mastering the how to run PL/SQL program is the next inevitable step.
In regardless of whether or not you are used to working in SQL*Plus, Oracle SQL Developer, or some other working environment, the practice of learning how to run PL/SQL code on your database server environment will instill a richer understanding of what features are available.
Prerequisites to Run PL/SQL Program
Prior to running a PL/SQL program, make certain that you possess the following:
1. Oracle Database Installed: An installation of Oracle Database that is correct and properly installed.
2. Database Access Credentials: A valid username and password with the necessary privileges.
3. PL/SQL Development Tools: Tools such as SQL*Plus, Oracle SQL Developer, or compatible alternatives.
Writing a Simple PL/SQL Program
To run PL/SQL programs, you need to know blocks that can include declarations, executable commands, and exception handlers. Here’s a basic example:
SQL
BEGIN
DBMS_OUTPUT.PUT_LINE('Hello, PL/SQL!');
END;
/
|
This anonymous block outputs the text 'Hello, PL/SQL!' to the console.
Also, check out: The Ultimate Guide To Master Responsive Web Design with CSS
Anonymous vs. Named Blocks
PL/SQL programs may be anonymous blocks (such as the one shown above) or named blocks (such as stored procedures and functions).
1. Anonymous blocks are convenient for executing PL/SQL code once. They are not saved in the database and are usually used for testing or executing ad-hoc scripts.
2. Named blocks (stored procedures, functions, packages, and triggers) are persisted in the database schema and may be invoked and reused by many applications or users. They are critical to creating modular and sustainable database applications. You would usually create named blocks when you have PL/SQL logic that you want to be run repeatedly or accessed by various parts of your system.
Run PL/SQL Program Using SQL*Plus

SQL*Plus is an Oracle command-line utility used to run SQL and PL/SQL commands.
Starting SQL*Plus
1. On Windows:
● Go to Start > Programs > Oracle-<OraHomeName> > Application Development > SQL*Plus
● Alternatively, go to Command Prompt and enter: sqlplus
2. On Linux/Unix:
● Open a terminal window.
● Enter: sqlplus
Connecting to the Database
Once SQL*Plus is launched, connect to your Oracle Database by entering:
Enter user-name: your_username
Enter password: your_password
|
Substitute your_username and your_password with your database login credentials.
How to Run PL/SQL Program | Step-by-Step Process
1. Turn On Output:
This command allows you to view the output of DBMS_OUTPUT.PUT_LINE statements.
2. Enter the PL/SQL Block:
SQL
BEGIN
DBMS_OUTPUT.PUT_LINE('Hello, PL/SQL!');
END;
/
|
3. Execute the Block:
End the block with a / on a new line and press Enter to execute.
Executing PL/SQL Programs Using Oracle SQL Developer
Oracle SQL Developer provides a graphical interface for database development.
Launching SQL Developer
Open Oracle SQL Developer from your applications menu or desktop shortcut.
Connecting to the Database
1. Click on the "Connections" tab (often on the left side).
2. Right-click and choose "New Connection."
3. Enter your connection information in the "New/Select Database Connection" window:
- Connection Name: A friendly name for your connection (e.g., "MyLocalDB").
- Username: your_username
- Password: your_password
- Hostname: your_host (e.g., localhost or an IP address)
- Port: The database listener port (typically 1521).
- SID/Service Name: your_service_name (refer to your database configuration).
4. Click "Test" to test the connection.
5. Click "Connect."
Running the PL/SQL Program
1. Create a new SQL Worksheet by navigating to File > New > SQL Worksheet (or Ctrl+Shift+N). Choose your newly established connection if asked.
2. Type your PL/SQL code into the worksheet:
SQL
BEGIN
DBMS_OUTPUT.PUT_LINE('Hello, PL/SQL!');
|
3. Click the "Run Script" button (it's a running man with a green arrow) or press F5. Make sure the "Script Output" window is open (Window > Script Output) to view the results.
Dealing with Common PL/SQL Errors

PL/SQL offers exception handling to deal with runtime errors. When an error is encountered, an exception is raised, which can be trapped to avoid the program crashing and to offer more descriptive error messages.
Common PL/SQL Errors and Solutions
Error 1: ORA-00942: Table or View Does Not Exist
Cause: You are attempting to reference a table or view that does not exist or is not accessible to the user.
Solution: Re-check spelling and case for the table/view. Ensure you are using the correct schema (you might need to use the schema owner preceding the table/view name, i.e., schema_owner.table_name). Ensure the user has the correct SELECT authorization on the object.
Example case: You are attempting to SELECT * FROM non_existent_table; without having created the table or being authorized to create it.
Error 2: PLS-00201: Identifier Must Be Declared
Cause: You are attempting to use a variable, constant, or procedure that has not been declared within the correct scope.
Solution: Ensure that all variables, constants, and procedures are declared within the declaration portion of your PL/SQL block or within the scope where you are using them.
Example Scenario:
SQL
BEGIN
my_variable := 10; -- my_variable is not declared
DBMS_OUTPUT.PUT_LINE(my_variable);
my_variable := 10;
DBMS_OUTPUT.PUT_LINE(my_variable);
END;
/
|
Error 3: ORA-06512: Line number in the error stack
Cause: This error accompanies other PL/SQL errors and tells you the line number in your PL/SQL code where the error actually occurred. This is part of the error stack trace, showing the call sequence that led to the error state.
Solution: Take a close look at the error message immediately above the "ORA-06512" error number. The "ORA-06512" message directs you to the exact line of code that caused the initial error. Correct your PL/SQL code by examining the error stack trace from the first error to the end.
Running SQL in PL/SQL
PL/SQL provides a way to nest SQL statements directly within its blocks. This facilitates your communication with database objects and your manipulation of data. An example is provided below illustrating SELECT, INSERT, UPDATE, and DELETE:
SQL
DECLARE
v_count NUMBER;
BEGIN
SELECT COUNT(*) INTO v_count FROM employees;
DBMS_OUTPUT.PUT_LINE('Number of employees: ' || v_count);
INSERT INTO employees (id, name) VALUES (101, 'John Doe');
DBMS_OUTPUT.PUT_LINE('Inserted employee with ID 101.');
UPDATE employees SET name = 'Jane Doe' WHERE id = 101;
DBMS_OUTPUT.PUT_LINE('Updated employee with ID 101.');
DELETE FROM employees WHERE id = 101;
DBMS_OUTPUT.PUT_LINE('Deleted employee with ID 101.');
COMMIT; -- Important: Commit your changes to make them permanent
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
/
|
Best Practices to Run PL/SQL Program Efficiently
1. Improve SQL Queries: A faster execution time for your PL/SQL programs can be achieved by improving the efficiency of your SQL queries. Index frequently queried columns, and eliminate joins and complex subqueries as they are not needed.
2. Use Error Handling: Incorporate good exception handling to manage run-time errors well and prevent your program from terminating. Exception handling can also offer a mechanism to log errors or code that will execute on the occurrence of an error.
3. Modularize Code: Break down complicated logic into tiny reusable pieces, like stored procedures and functions. This will make your program's code more maintainable, readable, and reusable.
4. Use Performance Tuning: Monitor execution performance information into running your SQL statements using Oracle's performance tuning tools (like EXPLAIN PLAN and SQL tracing).
5. Ensure Code Security: Always use bind variables when running dynamic SQL or passing user input into SQL statements to prevent SQL-injection attacks.
SQL
DECLARE
v_emp_id NUMBER := &employee_id; -- Using a substitution variable (same idea as bind variable)
v_emp_name VARCHAR2(100);
BEGIN
SELECT name INTO v_emp_name FROM employees WHERE id = v_emp_id;
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_emp_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee with ID ' || v_emp_id || ' not found.');
END;
/
|
(Note: In real-world applications with dynamic SQL, use the EXECUTE IMMEDIATE statement with the USING clause for proper bind variable usage.)
Conclusion
Learning how to run a PL/SQL program is a fundamental skill that needs to be acquired, before you can proceed with developing Oracle database applications. It does not matter whether you're running it with SQL*Plus, Oracle SQL Developer, or another product; knowing what's happening when you run it, as well as how to handle errors, will allow you to write good database applications.
Adhering to best practices, and query optimization will render your PL/SQL applications increasingly efficient. By keeping these best practices in mind, you won't only learn how to run PL/SQL program as fast as possible, but you'll become an improved database developer as well.
Are you looking for a specialist in database application or enterprise solution development?
Rasonix specializes in professional database design, tuning, and tailored software solutions. Partner with us to create secure, scalable, and high-performing systems that accompany your business across growth. Contact Us Now.