Você está na página 1de 3

CREAT OR REPLACE FUNCTION SERVIE_YEARS(HIREDATE IN DATE) RETURN NUMBER IS YEARS NUMBER; BEGIN YEARS: = TRUNC(MONTHS_BETWEEN(SYSDATE,HIREDATE)/12,0); END; / create or replace

function servie_years(hdate in date,weeks out number) is years number; begin YEARS: = TRUNC(MONTHS_BETWEEN(SYSDATE,HIREDATE)/12,0); weeks:= YEARS: = TRUNC(MONTHS_BETWEEN(SYSDATE,HIREDATE)*4,0 ); end; / declare hdate date:=to_date('03/08/2000','dd/mm/yyyy'); weeks number; years number; beginyears: =service|_years(hdate,weeks); dbms_output.put_line(years); dbms_output.put_line(weeks); end; / 1.Oracle automatically declares and implicit cursor every time a sql statemnet i s executed the user is unware f this and cannot control or process the inforamti on in an implicit cursor. 2.The e row plsql each program defines an explicit cursor for any query that returns more than on of data. This means that the programmer has declared the cursr within the code block.This declaration allows the application to sequentially process row of data as the cursor retruns it.

3. Unlike with an implicit cursor, the program defines an explicit cursor for an y query that returns more than one row of data, to process an explicit cursor,fi rst you declare it and then you open it then you fetch it, and finally you close it. 4. A record is a composite data structure which means that is composed of one or more elements records are very much like a row of database table but each eleme nt of the record does not stand on its own,plsql supports three kinds of records table based,cursor based,and programmer defined. A table-based record is one whose structure is drawn from the list of columns in the table. A cursor-based record is one whose structure matches the elements of a predefined cursor. To create a table-based or cursor-based record, use the %ROWTYPE attribute: record_name table_name or cursor_name%ROWTYPE 5. DECLARE CURSOR C_EMP IS SELECT * FROM EMP; R_EMP C_EMP%ROWTYPE; BEGIN OPEN C_EMP;

LOOP FETCH C_EMP INTO R_EMP; EXIT WHEN C_EMP%NOTFOUND; DBMS_OUTPUT.PUT_LINE('Employee Number is :'||R_EMP.EMPNO); DBMS_OUTPUT.PUT_LINE('Employee Name is :'||R_EMP.ENAME); END LOOP; CLOSE C_EMP; END; / 6.DECLARE TYPE RECORD_EMP IS RECORD (NAME EMP.ENAME%TYPE, SALARY EMP.SAL%TYPE); R_EMP RECORD_EMP; BEGIN SELECT ENAME,SAL INTO R_EMP FROM EMP WHERE EMPNO=7934; DBMS_OUTPUT.PUT_LINE(R_EMP.NAME); DBMS_OUTPUT.PUT_LINE(R_EMP.SALARY); END; / 7. DECLARE V_DEPTNO DEPT.DEPTNO%TYPE; CURSOR C_DEPT IS SELECT DISTINCT DEPTNO FROM DEPT; CURSOR C_EMP IS SELECT EMPNO FROM EMP WHERE DEPTNO=V_DEPTNO; BEGIN FOR R_DEPT IN C_DEPT LOOP V_DEPTNO :=R_DEPT.DEPTNO; DBMS_OUTPUT.PUT_LINE('Department Is : '|| ' '|| R_DEPT.DEPTNO); FOR R_EMP IN C_EMP LOOP DBMS_OUTPUT.PUT_LINE(R_EMP.EMPNO); END LOOP; END LOOP; END; / 8.Parameter Cursors DECLARE CURSOR C_EMP(V_DEPTNO IN DEPT.DEPTNO%TYPE) IS SELECT EMPNO,ENAME FROM EM P WHERE DEPTNO=V_DEPTNO; BEGIN FOR R_EMP IN C_EMP(30) LOOP DBMS_OUTPUT.PUT_LINE(R_EMP.EMPNO); END LOOP; END; / 9. DECLARE CURSOR C_DEPT IS SELECT DEPTNO FROM DEPT; CURSOR C_EMP(V_DEPTNO IN DEPT.DEPTNO%TYPE) IS SELECT EMPNO,ENAME FROM EMP WHERE DEPTNO=V_DEPTNO; BEGIN FOR R_DEPT IN C_DEPT LOOP DBMS_OUTPUT.PUT_LINE(CHR(10)); DBMS_OUTPUT.NEW_LINE; DBMS_OUTPUT.PUT_LINE(R_DEPT.DEPTNO); FOR R_EMP IN C_EMP(R_DEPT.DEPTNO) LOOP DBMS_OUTPUT.PUT_LINE(R_EMP.EMPNO); END LOOP;

END LOOP; END; / 10.The cursor FOR UPDATE clause is used only with a cursor when you want to upda te tables in the database. Generally, when you execute a SELECT statement, you are not lockin g any rows. The purpose of using the FOR UPDATE clause is to lock the rows of the tables you want to update so that another user cannot perform an update until you perform your upda te and release the lock. The next COMMIT or ROLLBACK statement releases the lock. The F OR UPDATE clause changes the manner in which the cursor operates in only a few resp ects. When you open a cursor, all rows that meet the restriction criteria are identified as part of the active set. Using the FOR UPDATE clause locks these rows that have been identified in t he active set. If the FOR UPDATE clause is used, rows may not be fetched from the cursor until a COMMIT has been issued. It is important to think about where to place the COMMIT. 11.DECLARE CURSOR C_EMP IS SELECT EMPNO,COMM FROM EMP_BKP FOR UPDATE OF COMM; BEGIN FOR R_EMP IN C_EMP LOOP DBMS_OUTPUT.PUT_LINE(R_EMP.EMPNO); UPDATE EMP_BKP SET COMM=NVL(R_EMP.COMM,0)+10 WHERE EMPNO=R_EMP.EMPNO; END LOOP; END; / 12. Use WHERE CURRENT OF when you want to update the most recently fetched row.W HERE CURRENT OF can be used only with a FOR UPDATE OF cursor.The advantage of the WHE RE CURRENT OF clause is that it enables you to eliminate the WHERE clause in the UP DATE statement: The WHERE CURRENT OF clause allows you to eliminate a match in the UPDATE statement, because the update is b eing performed for the cursor s current record only. 13. The FOR UPDATE and WHERE CURRENT OF syntax can be used with cursors that ar e performing a delete as well as an update 14. The ability to access a sequence via PL/SQL expression is a new feature in O racle 11g. Prior to Oracle 11g, sequences could be accessed only via queries. 15.292 http://video.xnxx.com/video943525/zee_telugu_hot_serial_soyagam_8

Você também pode gostar