Skip to main content

Posts

Showing posts from January, 2016

Introduction to Oracle Analytical functions , rank , denserank.

In this video we tried to demonstrate how oracle anaytical function works. How to do Top N query with oracle's rank and dense_rank function. Also explained is how outputs and functionality of rank and dense_rank differs , what should be used when. The demonstration video is from Linux Mint OS and I have used my own Query Light application. Hope you find it enlightening.   Here are some of the screen prints.  Use of Oracle Rank Analytical Function. Oracle Top N Query using rank analytical functions.

Get a PLSQL code dump of all your Oracle database code.

I used to work as a part time DBA and when I was doing my supposedely menial tasks like unlocking accounts. One of the frequent request was to get an code for the object a Function or Package or Trigger. While this was trivial task to get an single object code it had to be quick. I never bothered to write any code or script to get that.  However one day a developer came and asked me for a code dump for a whole schema ( We had 2500 eligible objects). What he wanted was the code dump for every plsql object stored in the system. This included triggers, functions, procedures , packages. So I searched the internet and came up with a code to do that. It was fairly small 10 liner code which was astonishing. Curiously I never saw the function used anywhere. The function clob2file is from package dbms_xslprocessor. This one is available in oracle 11g so if you ever found yourself yearning to dump a clob object to file you should try using this. There was just a little problem I wanted to

Oracle Procedures to delete the parent child table rows with integrity constraints.

With some very hard work and innovative thinking we came up with the procedures (can be easily clubbed into package) to delete the parent child relationship row with any number of levels. Below code would find the child row of the parent row being deleted and delete it first , if the child row has its own child then that childs children are fetched and deleted so its basically a recursive algorithm There are not many situations where you would want to use this. We had a customer mstr record to be deleted which had 20 levels of child records held tight by (ORA-02292: integrity constraint   violated - child record found ) with their parent child constraints. Since this only was a delete for 1 Master record we went ahead and used this script. If you have more than 20 levels Wise course would be to disable constraints and really think why you want to delete such master record. Note of caution : BE CAREFUL using this on production environment. The procedures create the insert stat

Cyclic blocking session removal script written by me for Oracle Database.

Plenty of times we have cyclic database blocking sessions. We have the script which runs in loop and kills them , only to see new blocking sessions have resurfaced. This is a classic scenario that happens many time due to poor application design. A was blocking B , B was blocking C. Unless you have some automated blocking session clearing script. You would run a script at point in time killing session of A. Giving the lock ownership to B.   B could realize it late that he has the lock and not commit his work. So DBA again goes in and sees block kills B's session. In meantime Frustrated A logs in and start his activity again queuing him up in wait for lock retrieval . This goes on and you end up running the script 5-10 times to kill these sessions to finally clear the blocks. Note this is not a deadlock which is normally apprehended and identified by oracle. In such case we had written below plsql to clear the sessions. This basically goes in and check for locks ever

Modify Windows file Change / modified date.

There is no straightforward command in windows to change the file modify date (Commonly referred to as timestamp) . I have spent significant time searching UNIX touch -t like command equivalent in windows but could not find a command that could achieve this feat. However there is a a way around through  a VBS . This VBScript would let you modify the file timestamp (Modified date ) to the one you give in the parameter. In below example it would be 29-Oct-2015.   Sub ChangeModifiedDate(strFolder, strFile, dteNew) Dim oShell Dim objFolder Set oShell = CreateObject("Shell.Application") Set oFolder = oShell.NameSpace(strFolder) oFolder.Items.Item(strFile).ModifyDate = dteNew End Sub changemodifieddate "D:\myfolder\fmx","new_empl.fmx","29-10-2015" So here are the things to change. Change the last line 1 st argument is the windows folder. 2nd Argument is the file name 3rd Argument is the Date modified desired. One needs

Oracle bulk insert with a single insert statement ignoring errors.

Starting from Oracle database release 10g you can have DML's succeed with partial data failures. How ? Default reaction of any bulk Insert or Update to an excption is atomic failure. i.e. If you were inserting 100 rows and one of the row fails to insert due to some constraint violation or duplicate records or any other check stuff. Your whole Insert fails. Of course you can write a quick PLSQL block with a LOOP and a null exception handler/logger which will basically ignore the exception or write it down somewhere and carry out your next loop iteration insertion. But first this makes you write the code for a loop and is more resource hogging. You should not have to start coding blocks everytime you want such data insertion. Below is how you can handle it. First you need to create a table for error logging it can be in any schema. Ex. EXECUTE DBMS_ERRLOG.CREATE_ERROR_LOG('EMPLOYEES' , 'EMPL_ERROR'); -- FOR CREATING ERROR LOG TABLE : TO LOG ERRORS DURING

Oracle simple function to create CSV.

This PL/SQL code function will quickly help to write CSV without having to code a single line. Just change the query argument to function. One will need a writable directory as a  utl_file_dir parameter from init.ora or pfile , or a stored directory object. Make sure you have read access to operating system directory you are supplying else it does not make much sense to run a program at all. You can create a database object as well just copy and replace function definition with create or replace function. --alter session set nls_date_format = 'DD-MON-YYYY HH24:MI:SS'; DECLARE cnt number ; function dump_csv( p_query in varchar2, p_separator in varchar2 default ',' , p_dir in varchar2 default '/non_edi_data', p_filename in varchar2 default 'temp1.csv' , qualify_with_quotes in boolean