Thursday, 22 May 2014

BULK COLLECT


CREATE OR REPLACE
PROCEDURE EMP_BULKCOLLECT
as
 
  type TY_EMP IS TABLE OF EMP%ROWTYPE;
  V_EMP TY_EMP;
BEGIN
  SELECT * BULK COLLECT INTO V_EMP FROM EMP;
  FOR i IN 1 .. V_EMP.COUNT
  LOOP
    DBMS_OUTPUT.PUT_LINE(i|| '   ' ||V_EMP(i).ENAME);
  END LOOP;
END;



The most important thing to remember when you learn about and start to take advantage of features such as BULK COLLECT is that there is no free lunch. There is almost always a trade-off to be made somewhere. The tradeoff with BULK COLLECT, like so many other performance-enhancing features, is "run faster but consume more memory."

Specifically, memory for collections is stored in the program global area (PGA), not the system global area (SGA). SGA memory is shared by all sessions connected to Oracle Database, but PGA memory is allocated for each session. Thus, if a program requires 5MB of memory to populate a collection and there are 100 simultaneous connections, that program causes the consumption of 500MB of PGA memory, in addition to the memory allocated to the SGA.


Consequently, you should avoid this sort of "unlimited" use of BULK COLLECT. Instead, move the SELECT statement into an explicit cursor declaration and then use a simple loop to fetch many, but not all, rows from the table with each execution of the loop body, as shown in below example.



CREATE OR REPLACE
PROCEDURE EMP_BULKCOLLECT_LIMIT(
    limit_in IN PLS_INTEGER)
as
  type TY_EMP IS  TABLE OF EMP%ROWTYPE;
  V_EMP TY_EMP;
 
  CURSOR C1
  IS
    SELECT * FROM EMP;
BEGIN
  OPEN C1;
  FETCH C1 BULK COLLECT INTO V_EMP LIMIT LIMIT_IN;
  FOR i IN 1 .. V_EMP.COUNT
  LOOP
    DBMS_OUTPUT.PUT_LINE(i|| '   ' ||V_EMP(i).ENAME);
  END LOOP;
  close C1;

END;

Monday, 19 May 2014

UTL_FILE

CREATE OR REPLACE
PROCEDURE EMPDTLSTOFILE
AS
  F1 UTL_FILE.FILE_TYPE;
  V_EMPINFO VARCHAR2(1000);
  CURSOR EMPDATA
  IS
    SELECT ENAME
      || ' is working '
      || ' as '
      || job
      || ' since '
      || TO_CHAR(HIREDATE, 'DD/MON/RRRR') AS STMT
    FROM EMP;
BEGIN
  DBMS_OUTPUT.PUT_LINE ('Dhanunjay');
  F1 := SYS.UTL_FILE.FOPEN('DEVDIR', 'EMPINFO.txt', 'A');
      SYS.UTL_FILE.PUT_LINE(F1,sysdate);
  FOR EMPINFO IN EMPDATA
  LOOP
    V_EMPINFO := EMPINFO.STMT;
    DBMS_OUTPUT.PUT_LINE (V_EMPINFO);
    SYS.UTL_FILE.PUT_LINE(F1,V_EMPINFO);
  end LOOP;
   DBMS_OUTPUT.PUT_LINE ('');
  SYS.UTL_FILE.FCLOSE(F1);
END ;

Oracle 11g New features for Developers



1.       Virtual Columns
2.       Named Notation in SQL
BEGIN
   package_name.procedure_name( p_parameter1 => 'some value',
                                p_parameter2 => 'another_value' );
END;
3.       SIMPLE_INTEGER Data type
Technically speaking it's not really a data type, but rather a subtype of the data type pls_integer. The simple integer is defined to take advantage of hardware arithmetic rather than software arithmetic. When simple integer is combined with real native compilation, the performance will be significantly better.
 If you set the variable to NULL inside the program, such as:
num1 := NULL;
 you will get a compilation error:   PLS-00382: expression is of wrong type  
If your program expects to set a variable to null, you can't define that variable as simple_integer.

4.       REGEXP_COUNT

SELECT REGEXP_COUNT(JOB,'A') FROM EMP;

5.        


How does one escape special characters when writing SQL queries?


Escape quotes
Use two quotes for every one displayed. Examples:
SQL> SELECT 'Frank''s Oracle site' AS text FROM DUAL;
 TEXT
 --------------------
 Franks's Oracle site

 SQL> SELECT 'A ''quoted'' word.' AS text FROM DUAL;
 TEXT
 ----------------
 A 'quoted' word.

 SQL> SELECT 'A ''''double quoted'''' word.' AS text FROM DUAL;
 TEXT
 -------------------------
 A ''double quoted'' word.
Use Q expression:
SQL> SELECT q'[Frank's Oracle site]' AS text FROM DUAL;
 TEXT
 -------------------
 Frank's Oracle site

 SQL> SELECT q'[A 'quoted' word.]' AS text FROM DUAL;
 TEXT
 ----------------
 A 'quoted' word.

 SQL> SELECT q'[A ''double quoted'' word.]' AS text FROM DUAL;
 TEXT
 -------------------------
 A ''double quoted'' word.
Escape wildcard characters
The LIKE keyword allows for string searches. The '_' wild card character is used to match exactly one character, while '%' is used to match zero or more occurrences of any characters. These characters can be escaped in SQL. Examples:
SELECT name FROM emp
 WHERE id LIKE '%/_%' ESCAPE '/';
SELECT name FROM emp
 WHERE id LIKE '%\%%' ESCAPE '\';
Escape ampersand (&) characters in SQL*Plus
When using SQL*Plus, the DEFINE setting can be changed to allow &'s (ampersands) to be used in text:
SET DEFINE ~
SELECT 'Laurel & Hardy' FROM dual;
Other methods:
Define an escape character:
SET ESCAPE '\'
SELECT '\&abc' FROM dual;
Don't scan for substitution variables:
SET SCAN OFF
SELECT '&ABC' x FROM dual;
Another way to escape the & would be to use concatenation, which would not require any SET commands -
SELECT 'Laurel ' || '&' || ' Hardy' FROM dual;
Use the 10g Quoting mechanism:
Syntax
 q'[QUOTE_CHAR]Text[QUOTE_CHAR]'
 Make sure that the QUOTE_CHAR followed by an ' doesn't exist in the text.
SELECT q'{This is Orafaq's 'quoted' text field}' FROM DUAL;


Connect To Oracle Using Easy Connect



sqlplus scott/tiger@BIGSERVER/DEV01

If the port was 1616 instead of 1521, I would use

sqlplus scott/tiger@BIGSERVER:1616/DEV01

You can also connect to a URL using the Easy Connect Syntax. If you want to connect using this syntax, use syntax such as:

sqlplus scott/tiger@//BIGSERVER/DEV01