Showing posts with label oracle tutorials. Show all posts
Showing posts with label oracle tutorials. Show all posts

Oracle PL/SQL WHERE CURRENT OF & FOR UPDATE

WHERE CURRENT OF & FOR UPDATE

The WHERE CURRENT OF clause is used in some UPDATE and DELETE statements.

The WHERE CURRENT OF clause in an UPDATE or DELETE statement states that the most recent row fetched from the table should be updated or deleted. We must declare the cursor with the FOR UPDATE clause to use this feature.

Inside a cursor loop, WHERE CURRENT OF allows the current row to be directly updated.

When the session opens a cursor with the FOR UPDATE clause, all rows in the return set will hold row-level exclusive locks. Other sessions can only query the rows, but they cannot update, delete, or select with FOR UPDATE.

Oracle provides the FOR UPDATE clause in SQL syntax to allow the developer to lock a set of Oracle rows for the duration of a transaction.

The syntax of using the WHERE CURRENT OF clause in UPDATE and DELETE statements follows:

WHERE [CURRENT OF cursor_name | search_condition]

Oracle PL/SQL: SQL ROLLUP

SQL ROLLUP

You can use WITH ROLLUP to generate a summary row for each group.

Example 1 of ROLLUP

Suppose we have the following table - Sales:


EmpIdYrSales
1200512000.00
1200618000.00
1200725000.00
2200515000.00
220066000.00
3200620000.00
3200724000.00


A simple Group by results in:

SELECT Yr, SUM(Sales) AS Sales
FROM Sales
GROUP BY Yr

YrSales
200527000.00
200644000.00
200749000.00

A simple group by with ROLLUP results in:

SELECT Yr, SUM(Sales) AS Sales
FROM Sales
GROUP BY Yr WITH ROLLUP

YrSales
200527000.00
200644000.00
200749000.00
NULL120000.00

Another example of group by with ROLLUP:

SELECT EmpId, Yr, SUM(Sales) AS Sales
FROM Sales
GROUP BY EmpId, Yr WITH ROLLUP

EmpIdYrSales
1200512000.00
1200618000.00
1200725000.00
1NULL55000.00
2200515000.00
220066000.00
2NULL21000.00
3200620000.00
3200724000.00
3NULL44000.00
NULLNULL120000.00

Difference between HAVING CLAUSE & WHERE CLAUSE in Oracle

SQL - Difference between HAVING CLAUSE & WHERE CLAUSE

1. HAVING specifies a search condition for a group or an aggregate function used in SELECT statement. The WHERE clause specifies the criteria which individual records must meet to be selected by a query. It can be used without the GROUP BY clause. The HAVING clause cannot be used without the GROUP BY clause.

2. The WHERE clause selects rows before grouping. The HAVING clause selects rows after grouping.

3. The WHERE clause cannot contain aggregate functions. The HAVING clause can contain aggregate functions

Displaying Trigger Errors in Oracle

Displaying Trigger Errors

If we get a message Warning: Trigger created with compilation errors. you can check the error messages with:

Show errors trigger ;

You can also type, SHO ERR (SHOW ERRORS) to see the most recent compilation error.

Example of PL/SQL trigger in Oracle


In the below example line 2 is A triggering event or statement, lines 4-9 are A trigger action.

Example of creating a trigger based on the following two tables:

CREATE TABLE T1 (a INTEGER);
CREATE TABLE T2 (b INTEGER);

We will create a trigger that may insert a tuple into T2 when a tuple is inserted into T1. The trigger checks if the inserted row in T1 is has a value less than 5 only then a tuple is inserted in T2.

1 CREATE TRIGGER tr1
2 AFTER INSERT ON T1
3 REFERENCING NEW AS newRow
4 FOR EACH ROW
5 WHEN (newRow.a <= 5)
6 BEGIN
7 INSERT INTO T2
VALUES(:newRow.a);
8 END tr1;
9 .
10 run;

PL/SQL Triggers in Oracle

PL/SQL Triggers

A PL/SQL trigger is a construct in PL/SQL that runs or "triggered" on event of changes being made to a table in the database. The triggering event is a INSERT, UPDATE or DELETE done on a table. The trigger can be made so it can be "fired" either BEFORE or AFTER the Data Manipulation Language is executed.

  • A database trigger is a block of code that is automatically executed in response to certain events.
  • Triggers are executed implicitly whenever the triggering event happens.
  • The triggering event is an INSERT, DELETE, or UPDATE command.
  • The timing can be either BEFORE or AFTER, INSTEAD OF trigger.

The trigger can be either row-level or statement-level, where the former fires once for each row affected by the triggering statement and the latter fires once for the whole statement.

You can write triggers that fire whenever one of the following operations occurs:

  1. DML statements (INSERT, UPDATE, DELETE) on a particular table or view, issued by any user

  2. DDL statements (CREATE or ALTER primarily) issued either by a particular schema/user or by any schema/user in the database

  3. Database events, such as logon/logoff, errors, or startup/shutdown, also issued either by a particular schema/user or by any schema/user in the database

A trigger has three basic parts:

  • A triggering event or statement
  • A trigger restriction
  • A trigger action

Oracle PL/SQL Collections

Records
    Table Based Records Table Based Records Cursor Based Records Cursor Based Records Programmer-defined Records Programmer-defined Records
PL/SQL Tables PL/SQL Tables

Varrays Varrays

Nested Tables Nested Tables