Slides
Slides
Cross join SQL> SELECT empno,dept.deptno FROM emp CROSS JOIN dept;
Without statement:
Searched
SQL> SELECT ename, sal, deptno, CASE WHEN sal <=500 THEN 0 WHEN Sal > 500 and sal<1500 THEN 100 WHEN sal >=1500 and sal<= 2500 and deptno=10 THEN 200 WHEN sal >1500 and sal < 2500 and deptno=20 THEN 500 WHEN sal >=2500 THEN 300 ELSE 0 END Bonus FROM emp;
Scalar Subqueries
SQL> SELECT empno, (SELECT ename FROM emp b WHERE b.empno=a.mgr) manager FROM emp a ORDER BY mgr;
Another Example: SQL> SELECT empno,ename,deptno FROM emp a ORDER BY (SELECT dname FROM dept b WHERE a.deptno=b.deptno);
In this case, a column that is not readily available in the EMP table, and not even one is not displayed in the query. Scalar subqueries are still not valid in Oracle 9i in the following cases: In check constraints In when conditions of case expressions In group by and having clauses In start with and connect by clauses
SQL> INSERT INTO my_table VALUES (1, DEFAULT); SQL> INSERT INTO my_table VALUES (2, Not a default); SQL> UPDATE my_table set uname=DEFAULT WHERE id=2;
Current_timestamp: Return the current date based on the session time zone using a timestamp with time zone data type localtimestamp: Returns the current date and time using the session time zone. The time value is returned using a timestamp data type. Dbtimezone: Returns the value of the database time zone. To_timestamp: Converts a character string to a timestamp.
CREATE OR REPLACE FUNCTION my_function(p_input NUMBER) RETURN NUMBER IS v_return NUMBER; BEGIN v_return:=CASE WHEN p_input < 1000 THEN 0 WHEN p_input BETWEEN 1000 AND 10000 THEN 1000 ELSE 2000 END; RETURN v_return; END;
Bulk-Bind Improvements
-Oracle9i allows to use bulk binding with Oracle collection types with the select and fetch clauses. - Bulk binding of records used in insert and update statements is also supported. -Error processing for bulk binds has been much improved. -Bulk error handling is provided through the use of the new save exceptions keyword in a forall statement. -All errors will be stored in a new Oracle cursor attribute, %bulk_exceptions . This cursor stores the error number and message with in it for each SQL error. - The total number of errors is also stored as an attribute of %bulk_exceptions(%bulk_exceptions.count).
DBMS_OUTPUT.PUT_LINE(count = || v_count); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE(Number of errors recorded || SQL%BULK_EXCEPTIONS.COUNT); FOR counter IN 1..SQL%BULK_EXCEPTIONS.COUNT LOOP DBMS_OUTPUT.PUT_LINE(Record Number: ||counter); DBMS_OUTPUT.PUT_LINE (Error Code: || SQL%BULK_EXCEPTIONS(counter).error_code); DBMS_OUTPUT.PUT_LINE(Error Message: || SQLERRM(SQL%BULK_EXCEPTIONS(counter).ERROR_I NDEX)); END LOOP;
BEGIN EXECUTE IMMEDIATE SELECT empno FROM emp WHERE deptno=10 BULK COLLECT INTO v_num_list; END;
Example of setting an audit policy in Oracle Database 10g: Begin Dbms_fga.add_policy( Object_schema=>retiree, Object_name=>personal_info, Policy_name=>retire_policy, Audit_condition=Null, Audit_column=>SSN, Enable=>TRUE Statement_type=>update, delete); END; /
MERGE Statement
>MERGE INTO incr D USING (SELECT empno,sal FROM emp) S ON (D.empno=S.empno) WHEN MATCHED THEN UPDATE SET D.incramt=D.incramt + S.sal*01, D.incrdate=SYSDATE WHEN NOT MATCHED THEN INSERT VALUES(S.empno, S.sal*0.1, SYSDATE)