Sunteți pe pagina 1din 16

CLOB, or Character Based LOBs are used for

text files. Binary LOBs, or BLOBs are used for


binary files and images, audio, video, etc.
BFILE also stores binary files, but outside the
database, and it is a link, or locator to those
files. Then there are collection types, like
VARRAYS, where we define the length of the
collection during its declaration. NESTED
TABLES allows us to extend the length of the
collection programmatically. ASSOCIATIVE
ARRAYS is another form of PL/SQL
collection, which is indexed. Then, there are
datatypes like ROWID, UROWID. ROWIDs
are addresses of rows in the database. It can
be a physical ROWID, which pertains to a
physical ROW in an ordinary table. Or,
logical ROWID, which pertains to rows in
index organized tables. A variable of type
ROWID can only store physical ROWIDs,
while a variable of type UROWID can hold
both physical, and logical ROWIDs.
The FOR loop counter is implicitly
declared as PLS_INTEGER by Oracle, and
so you should not assign values to it
which will go beyond the PLS_INTEGER
range. Otherwise, you will get a numeric
Oracle exception. You cannot use the
counter variable as an assignment target.
If you do so, you will get an error. The
loop counter variable has a scope only
within the loop, and referencing it
outside will give you an error. In this
example, if we reference l_counter
outside of the loop, we will get an error.
In the block, if there is a variable with the
same name as the loop counter inside
the loop, the loop counter will overwrite
the visibility of that variable. However, if we need to access the block variable inside the loop, we can do so using
labels.
Another way of preventing NULL evaluation is using the built-in NVL function. NVL evaluates the first parameter
passed in, and if it is null, it returns the second parameter. So here, IF the l_sales_amt is null, it will return a value of 0
for it.

Cursors
A cursor is a handle, or a name to an area in memory used by Oracle for processing of SQL statements. Whenever you
issue a SELECT, INSERT, UPDATE, or a DELETE in your SQL code directly, Oracle creates an implicit cursor, or an area in
memory to parse and execute the query and hold its results. The implicit cursors are handled by Oracle, but they have
certain limitations. Oracle also gives you the ability to define cursors explicitly based on a query. Oracle assigns a
name area in memory, parses and executes a query for you, but gives you control to open, fetch, and close the cursor.

Implicit cursors are open for you when you issue


a SELECT, INSERT, UPDATE, or a DELETE
statement in PL/SQL. Oracle opens these cursors
for you, fetches the row, and closes it for you
automatically. In this code snippet, we issue a
SELECT to get the dept_id, and dept_name from
the departments table. And fetch INTO
l_dept_id, and l_dept_name. Note the SELECT,
INTO syntax in PL/SQL, which is different from
SQL statements. Here, you need to have the
same number and type of variables declared to
fetch the SELECT columns in the SQL query. For
example, here we have l_dept_id to fetch the
dept_id, and l_dept_name to fetch the
dept_name from the SELECT statement. If your
number and type of variables differ from the
SELECT columns, Oracle will give you an error for
too many values if we have more variables than
the SELECT columns, and not enough values, if
we have more columns selected than the
variables to hold them. SQL%FOUND is a built-in
implicit cursor attribute, which returns TRUE if the SELECT statement fetched a row, or if an UPDATE statement
updated any rows, or if a DELETE statement deleted any rows. It returns FALSE otherwise. SQL%NOTFOUND is just the
opposite of SQL%FOUND. SQL%ROWCOUNT gives the number of rows fetched, or updated, or deleted. For a SELECT
query if you're sure that's going to fetch a row, and only one row, then using an implicit SELECT cursor is the most
efficient way to go. However, if there are more than one rows fetched, or no rows fetched at all, then Oracle raises
errors. Sometimes you might want to use them when you actually want an error to be raised for these conditions. It
might also help someone reading your code to understand that this SELECT statement is supposed to fetch one row
only, and it is an erroneous condition if that is not the case. I wanted to show you an example of how to issue a
DELETE statement in PL/SQL as in this code snippet below, where we delete from the departments table. In the next
code snippet we show how to use an UPDATE statement in PL/SQL to update dept_name to Marketing for dept_id too.
Note that the DELETE and UPDATE statements follow the same syntax we use as when normally writing SQL
statements. Implicit cursors are okay to use for UPDATEs, and DELETEs in PL/SQL, and for SELECTs where you're sure
that only one row is going to be fetched. What happens if there is more than row fetched, as in this code snippet,
where we have removed the WHERE condition, and now, this query will fetch dept_id and dept_name for all the
departments? In this case, Oracle will raise an error, EXACT FETCH RETURNS MORE THAN REQUESTED NUMBER OF
ROWS, as it can only use a single variable to hold a collection of values. And if your fetch returns no row, as in this
example, we are trying to fetch information for dept_id 10, which does not exist. In this case, Oracle will raise NO
DATA FOUND EXCEPTION. If you are expecting more than one row, explicit cursors would be the way to go.
COMMIT saves the data to the database, and makes the changes permanent. ROLLBACK, on the other hand reverts
back the changes and brings the data to the state before the change.
Ref Cursors or Cursor Variables
A ref cursor, or cursor variable, is a reference, or a pointer to a cursor. It is explicitly named, and can be used for
multiple queries, so you can open it for one query, and then, later open it for another query if you need to. Before you
point it to the next query, you need not close it. It will just lose reference to the old query. After we are done using it,
you need to close it to release the memory. It can be a parameter to a program unit, like a procedure, or a function.
SQL BASICS

S-ar putea să vă placă și