- Log in to google drive (https://drive.google.com)
- select Settings-Upload Settings- " Convert documents, presentations, and spreadsheets to the corresponding Google Docs formats" is ticked.
- Select the source files from your computer.
- Click Start Upload ...
- When the upload is finished, choose Go Back to Google Docs.
- Open the file in Docs.
- Select the contents that you want to upload (possibly use Select All from the Docs menu).
- Copy (you may need to use Edit / Copy from the browser, not from inside Docs).
- Switch to the Blogger post editor, in Compose mode.
- Paste.
- Add any other content that you want in your post.
- Preview the post, and do any final formatting changes that are needed.
To live a creative life we must lose the fear of being wrong.
Tuesday, July 15, 2014
Uploading MS Word documents to Blogger - via Google Drive to avoid text formatting issue
REF Cursors in Oracle PLSQL
REF Cursors in Oracle PLSQL -
The Primary purpose of this post is to provide fair idea on the advanced concepts in PL/SQL like REF Cursor. We have given a try and hope to be useful for my audiences.
REF CURSOR
• A ref cursor is a variable, defined as a cursor type, which will point to, or reference a cursor result.
• To execute a multi-row query, Oracle opens an unnamed work area that stores processing information. You can access this area through an explicit cursor, which names the work area, or through a cursor variable, which points to the work area. To create cursor variables, you define a REF CURSOR type, and then declare cursor variables of that type.
Syntax of the REF Cursor
Define a REF Cursor TYPE:
TYPE ref_type_name IS REF CURSOR
[RETURN {
cursor_name%ROWTYPE
|ref_cursor_name%ROWTYPE
|record_name%TYPE
|record_type_name
|db_table_name%ROWTYPE
}
];
RETURN
specifies the data type of a cursor variable return value. You can use the %ROWTYPE attribute in the RETURN clause to provide a record type that represents a row in a database table or a row from a cursor or strongly typed cursor variable. You can use the %TYPE attribute to provide the datatype of a previously declared record.
specifies the data type of a cursor variable return value. You can use the %ROWTYPE attribute in the RETURN clause to provide a record type that represents a row in a database table or a row from a cursor or strongly typed cursor variable. You can use the %TYPE attribute to provide the datatype of a previously declared record.
cursor_name
An explicit cursor previously declared within the current scope.
An explicit cursor previously declared within the current scope.
ref_cursor_name
An ref cursor previously declared within the current scope.
An ref cursor previously declared within the current scope.
record_name
A user-defined record previously declared within the current scope.
A user-defined record previously declared within the current scope.
record_type_name
A user-defined record type that was defined using the data type specifies RECORD.
db_table_name
A database table or view, which must be accessible when the declaration is elaborated.
A database table or view, which must be accessible when the declaration is elaborated.
%ROWTYPE
A record type that represents a row in a database table or a row fetched from a cursor or strongly typed cursor variable. Fields in the record and corresponding columns in the row have the same names and datatypes.
A record type that represents a row in a database table or a row fetched from a cursor or strongly typed cursor variable. Fields in the record and corresponding columns in the row have the same names and datatypes.
%TYPE
Provides the datatype of a previously declared user-defined record.
Provides the datatype of a previously declared user-defined record.
type_name
A user-defined cursor variable type that was defined as a REF CURSOR.
A user-defined cursor variable type that was defined as a REF CURSOR.
Cursor_variable_declaration:
cursor_variable_name ref_type_name;
OPEN a REF cursor...
OPEN cursor_variable_name
FOR select_statement;
/*To be sure it's not open already:*/
IF NOT cursor_variable_name%ISOPEN THEN
OPEN cursor_variable_name FOR select_statement;
END IF;
Types of REF CURSOR
Strongly Typed: A REF CURSOR that specifies a specific return type
DECLARE
TYPE EmpCurTyp IS REF CURSOR
RETURN emp%ROWTYPE; -- strongly typed ref cursor
cursor1 EmpCurTyp;
BEGIN
NULL;
END;
Weakly Typed: A REF CURSOR that does not specify the return type
DECLARE
TYPE EmpCurTyp IS REF CURSOR -- Weakly typed ref cursor
cursor1 EmpCurTyp;
BEGIN
NULL;
END;
Three statements to control a cursor variable
OPEN-FOR
FETCH
CLOSE
- OPEN-FOR statements can open the same cursor variable for different queries. You need not close a cursor variable before reopening it. When you reopen a cursor variable for a different query, the previous query is lost.
- PL/SQL makes sure the return type of the cursor variable is compatible with the INTO clause of the FETCH statement.
Simple Example:
DECLARE
TYPE EmpCurTyp IS REF CURSOR
RETURN emp%ROWTYPE; -- strong cursor
emp1 EmpCurTyp;
PROCEDURE process_emp_cv (emp_cv IN EmpCurTyp) IS
person emp%ROWTYPE;
BEGIN
DBMS_OUTPUT.PUT_LINE('-----');
DBMS_OUTPUT.PUT_LINE('Here are the names from the result set:');
LOOP
FETCH emp_cv INTO person;
EXIT WHEN emp_cv%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Name = ' || person.ENAME ||' ' || person.JOB);
END LOOP;
END;
BEGIN
OPEN emp1
FOR SELECT *
FROM emp
WHERE ROWNUM < 11;
process_emp_cv(emp1);
CLOSE emp1;
OPEN emp1
FOR SELECT *
FROM emp
WHERE ENAME LIKE 'R%';
process_emp_cv(emp1);
CLOSE emp1;
END;
Difference between REF CURSOR and CURSOR with Example:
DECLARE
TYPE rc IS REF CURSOR;
CURSOR c IS SELECT * FROM dual;
l_cursor rc;
BEGIN
IF (to_char(SYSDATE,'dd') = 30) THEN
OPEN l_cursor FOR SELECT * FROM emp;
ELSIF (to_char(SYSDATE,'dd') = 29) THEN
OPEN l_cursor FOR SELECT * FROM dept;
ELSE
OPEN l_cursor FOR SELECT * FROM dual;
END IF;
OPEN c;
-----
/* some manipulation here */
-----
CLOSE c;
END;
Comparisons
1. Cursor C will always be select * from dual. The ref cursor can be anything.
2. Cursor can be global -- a ref cursor cannot (you cannot define them OUTSIDE of a procedure / function).
3. Ref cursor can be passed from subroutine to subroutine -- a cursor cannot be.
Usage Restrictions
The following are restrictions on cursor variable usage.
1. Comparison operators cannot be used to test cursor variables for equality, inequality, null, or not null.
2. Null cannot be assigned to a cursor variable.
3. The value of a cursor variable cannot be stored in a database column.
4. Static cursors and cursor variables are not interchangeable. For example, a static cursor cannot be used in an OPEN FOR statement.
*************************************************************************************************************
**************************************************************************************************************
*************************************************************************************************************
**************************************************************************************************************
Cursor Management Best Practices - Use Ref Cursors
Ref cursors are a special type of cursors where the query is not defined beforehand. Instead, the cursor is defined as a shell or a placeholder and the query is passed during the runtime. Here is an example of a ref cursor:
1 DECLARE 2 TYPE ty_bookrefcur IS REF CURSOR; 3 4 cu_bookcur ty_bookrefcur; 5 rec_books books%ROWTYPE; 6 l_title books.title%TYPE; 7 l_author books.author%TYPE; 8 BEGIN 9 OPEN cu_bookcur FOR 10 SELECT * 11 FROM books; 12 13 FETCH cu_bookcur 14 INTO rec_books; 15 16 CLOSE cu_bookcur; 17 18 DBMS_OUTPUT.put_line ('Book is ' || rec_books.title); 19 20 OPEN cu_bookcur FOR 21 SELECT title 22 FROM books; 23 24 FETCH cu_bookcur 25 INTO l_title; 26 27 DBMS_OUTPUT.put_line ('Title is ' || l_title); 28 29 CLOSE cu_bookcur; 30 31 OPEN cu_bookcur FOR 32 SELECT author 33 FROM books; 34 35 FETCH cu_bookcur 36 INTO l_author; 37 38 DBMS_OUTPUT.put_line ('Author is ' || l_author); 39* END;
A generic cursor, cu_bookcur, is defined in line 4. In line 9, I have assigned the query "SELECT * FROM books". Later, in line 20, the same cursor is used to execute the query "SELECT select FROM books". This technique is very useful when you don't know the exact query while writing the code. Instead of creating a plethora of cursors for all possible queries, you can define a simple ref cursor and use it to fetch any query on that table.
If you want to make sure the ref cursor returns a specific datatype, define it as "strong" one. Example:
TYPE ty_bookrefcur IS REF CURSOR RETURN books%ROWTYPE;
Here the return type is always the record of the table BOOKS. This is useful when you know that the return from the cursor must be all the columns; but the predicate (the "WHERE" clause is different and used at the runtime). Here is an example:
1 DECLARE 2 TYPE ty_bookrefcur IS REF CURSOR 3 RETURN books%ROWTYPE; 4 5 cu_bookcur ty_bookrefcur; 6 rec_books books%ROWTYPE; 7 l_title books.title%TYPE; 8 l_author books.author%TYPE; 9 BEGIN 10 OPEN cu_bookcur FOR 11 SELECT * 12 FROM books 13 WHERE title LIKE '%PL/SQL%'; 14 15 FETCH cu_bookcur 16 INTO rec_books; 17 18 CLOSE cu_bookcur; 19 20 DBMS_OUTPUT.put_line ('Book is ' || rec_books.title); 21 22 OPEN cu_bookcur FOR 23 SELECT * 24 FROM books 25 WHERE title LIKE '%Security%'; 26 27 FETCH cu_bookcur 28 INTO rec_books; 29 30 CLOSE cu_bookcur; 31 32 DBMS_OUTPUT.put_line ('Book is ' || rec_books.title); 33* END;
In line 10, I opened the generic cursor to select titles of PL/SQL and in line 22, I used the same generic cursor to get the titles related to Security. I didn't need to open a new cursor.
Subscribe to:
Posts (Atom)