Optimize Oracle Query
We can optimized/tune a query by following way
EXPLAIN PLAN FOR
SELECT * FROM emp e, dept d WHERE e.deptno = d.deptno AND e.ename = 'SMITH';
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);
The DBMS_XPLAN.DISPLAY function can accept 3 optional parameters
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY('PLAN_TABLE','TSH','BASIC'));
SELECT /*+ FIRST_ROWS(10) */ * FROM employees;=>First 10 rows in employee table
How to bind array in oracle
Oracle Procedure contain Associative Array Call from Dot Net
Looping Associative array In Oracle
******************************************************
How to Defined an array IN Oracle ==>
******************************************************
DECLARE
TYPE prod_name IS RECORD(product_id varchar2(500),product_desc varchar2(500),product_brand varchar2(500));
type prod_prod_grp_1_assoc is table of prod_name index by binary_integer;
type prod_prod_grp_2_assoc is table of prod_name index by binary_integer;
prod_product_group_1 prod_prod_grp_1_assoc;
BEGIN
prod_product_group_1(1).product_id := 'ProductID1';
prod_product_group_1(1).product_desc := 'Product Description';
prod_product_group_1(1).product_brand := 'Product Brand';
prod_product_group_1(2).product_id := 'ProductID2';
. . . . . . . . . . . . . . .
prod_product_group_1(3).product_id := 'ProductID3';
. . . . . . . . . . . . . . .
prod_product_group_1(4).product_id := 'ProductID4';
. . . . . . . . . . . . . . .
prod_product_group_1(5).product_id := 'ProductID5';
. . . . . . . . . . . . . . .
prod_product_group_1(6).product_id := 'ProductID6';
. . . . . . . . . . . . . . .
for i in 1 .. 6 LOOP
dbms_output.put_line (prod_product_group_1(i).product_id);
dbms_output.put_line (prod_product_group_1(i).product_desc);
dbms_output.put_line (prod_product_group_1(i).product_brand);
END LOOP;
END;
*************************************************
How to defiend procedure in oracle.==>
*************************************************
In the scope of the package defined the t_associative_id
type t_associative_id is table of VARCHAR2(4000) index by binary_integer;
NOTE: Here one thing we need to remember that if we pass associative array as VARCHAR2[i.e => t_associative_id .OracleDbType = OracleDbType.Varchar2;] from .net code then we need to defiened the associative array type as above but if we pass associative array as Number[i.e:=> t_associative_id.OracleDbType = OracleDbType.Int64/Int32;] then we need to defiend the associative array type as following
type t_associative_id is table of Number index by binary_integer;
PART 1:Signature of the procedure
PROCEDURE testExample(v_id IN v_table.v_id%type,
v_age IN NUMBER,
v_name IN VARCHAR2,
v_array_id IN t_associative_id,
v_retcode OUT NUMBER,
v_reterror OUT VARCHAR2) AS
PART2:Defined variable of the procedure
v_var1 VARCHAR2(50); =>Defiend a varchar type variable.
v_var2 NUMBER(5);=>Defiend a number type variable.
v_tmp_id v_table.v_id%type;=>Defiened a type which are equivalent to the type of v_id field of v_table table.
c_select SYS_REFCURSOR;=>Defiend a cursor.
v_query VARCHAR2(1000);
Begin
PART3:Body of the procedure
v_retcode :=0;
v_reterror :=NULL;
--Now looping through associative array
FOR I in .. t_associative_id LOOP=>Looping through the associative array.
v_query:=”SELECT fieldval1,filedval2,filedval3 FROM v_table WHERE v_id=”|| t_associative_id (i);
OPEN c_select FOR v_query;=>Looping through the cursor.
LOOP
FETCH c_select
INTO fieldval1,fieldval2,fieldval3;
EXIT WHEN c_select%NOTFOUND;
INSERT INTO t_table(field1,field2,field3)
VALUES(fieldval1,fieldval2,fieldval3);
END LOOP;
END LOOP;
CLOSE c_select;=>Close the cursor.
EXCEPTION
WHEN OTHERS THEN
v_retcode :=SQLCODE;=>Return the exception sql code.
V_reterror :=SQLERRM;=>Return the exception sql message.
ROLLBACK;=>Rollback the transaction.
END testExample=>End the procedure.
How to test the ORACLE PROCEDURE
To test the oracle procedure we need to right click on the procedure and click on the test link.
Now we need to pass the parameter value which is required to call the procedure.
We can pass the associative array value in the following way :
t_associative_id (1):='12';=>First array element .
t_associative_id (2) :='23';=>Second array element.
Timestamp Conversion In oracle
1>For converting timezone format
SELECT TO_TIMESTAMP_TZ('2012-02-25 03:15:04.062827 +00:00', 'yyyy-mm-dd HH24:MI:SS.FF6 TZH:TZM') FROM dual;
2>For converting millisecond format
SELECT To_timestamp('2012-02-25 03:15:04.062827', 'yyyy-mm-dd HH24:MI:SS.FF6') FROM dual;
For Converting milliseconds format
Oracle Troubleshooting
ora-01031 insufficient privileges while creating a view
1>Open the command prompt and type=>sqlplus / as sysdba
3>grant create view to scott;
ORA-28000: the account is locked
After oracle installation we may get the account locked error.TO resolve this error we need to logging as database administrator and unlock the user account.
1>Open the command prompt and type=>sqlplus / as sysdba
2>alter user scott account unlock;
3>grant connect,resource to scott;
ORACLE Few Required query
select * from dba_all_tables
select * from dba_users
select * from all_objects
select * from V$DATAFILE
select * from V$session
select * from v$
select * from gv$session s
select * from gv$process