Thursday, 23 March 2023

ROWID Usages

 In Oracle, ROWID is a pseudo column that is used to uniquely identify a specific row in a table. It represents the physical address of a row and consists of the data file number, the data block number, and the location of the row within the block.

The ROWID column can be useful in several ways in DQL (Data Query Language), DML (Data Manipulation Language), and programming of PL/SQL. Here are some examples:

Retrieving specific rows:

Suppose you have a table with thousands of rows, and you need to retrieve a specific row or set of rows based on a condition. You can use the ROWID column to retrieve the rows efficiently. For example:

SELECT * FROM mytable WHERE ROWID = 'AAABBBCCC';

Updating specific rows:

If you want to update specific rows, you can also use the ROWID column in the UPDATE statement. For example:

UPDATE mytable SET column1 = 'value' WHERE ROWID = 'AAABBBCCC';

Deleting specific rows:

Similarly, you can use the ROWID column to delete specific rows. For example:

DELETE FROM mytable WHERE ROWID = 'AAABBBCCC';

PL/SQL programming:

In PL/SQL, you can use the ROWID column to loop through a table and perform operations on each row. For example:

DECLARE

  CURSOR c1 IS SELECT ROWID, column1 FROM mytable;

BEGIN

  FOR r1 IN c1 LOOP

    IF r1.column1 = 'value' THEN

      -- do something with the row

      UPDATE mytable SET column2 = 'newvalue' WHERE ROWID = r1.ROWID;

    END IF;

  END LOOP;

END;

In this example, we are using a cursor to loop through the table and retrieve each row's ROWID and column1 value. We then use the ROWID to update the row's column2 value if the column1 value is 'value'.

Overall, the ROWID column is a powerful tool in Oracle that can be used to efficiently retrieve, update, and delete specific rows, as well as perform operations on each row in a table using PL/SQL.

No comments:

Post a Comment