Thursday, August 21, 2014

VirtualBox: Accessing windows shared folder in Linux guest

Make sure you have installed Guest Additions.
Share a folder on the host machine by going to settings/SharedFolders
Now, login to Linux guest machine and run following commands:

# sudo mkdir /media/some_name
# sudo mount -t vboxsf Host_Shared_Folder_Name /media/some_name

Now cd to /media/some_name and you will see all your windows files.

Add those commands to /etc/init.d/rc.local file to start this mount automatically on the reboot.

Monday, August 18, 2014

Converting xls, xlsx files into csv file thru command line

Thanks to "ScottF", "plang" and "Jon of All Trades" from "StackOverflow" for this solution.

I had a situation where there are hundreds of xls files that needs to be loaded into Oracle and feeling lazy (as usual) to convert all of them into csv file to use in sqlldr.
So, doing little google found this solution:

Copy below code and write into a file Convert_xls2csv.vbs file:
-------------------------------------------------------------------------------------------------------
if WScript.Arguments.Count < 2 Then
    WScript.Echo "Please specify the source and the destination files. Usage: ExcelToCsv "
    Wscript.Quit
End If

csv_format = 6

Set objFSO = CreateObject("Scripting.FileSystemObject")

src_file = objFSO.GetAbsolutePathName(Wscript.Arguments.Item(0))
dest_file = objFSO.GetAbsolutePathName(WScript.Arguments.Item(1))

Dim oExcel
Set oExcel = CreateObject("Excel.Application")

Dim oBook
Set oBook = oExcel.Workbooks.Open(src_file)

oBook.SaveAs dest_file, csv_format

oBook.Close False
oExcel.Quit
-------------------------------------------------------------------------------------------------------

Now, create another Convert2csv.bat batch file and copy the below to run thru the loop:

-------------------------------------------------------------------------------------------------------
FOR /f "delims=" %%i IN ('DIR "*.xls" /b') DO Convert_xls2csv.vbs "%%i" "%%i.csv"
-------------------------------------------------------------------------------------------------------

Please these two files in the directory where you have all your xls(x) files and run the batch file and relax :)

Original Source:

Sunday, August 17, 2014

Fetching records in buckets

This is not a pagination but fetching records in buckets form. Meaning, if you have a table that has huge number of rows and wants to process them in buckets with multiple sessions simultaneously by dividing the rows.

Using analytic function NTAIL:

create table san_bucket_test (col1 number, col2 varchar2(30), statusvarchar2(20));

begin
  for i in 1..20 loop
    insert into san_bucket_test values (i, 'Record # '||i, 'PENDING');
  end loop;
end;
/

select *
  from (
        select sbt.*, (ntile(4) over (order by sbt.col1)) bucket
          from san_bucket_test sbt
         where status = 'PENDING'
       )
 where bucket =3;

As you see above, the above NTILE function with argument 4 divides the rows into 4 buckets and the outer join with bucket=3 will only gets the data for 3rd bucket.