Ad Hoc Reporting

I had written a script to restore some data that had been lost. A team convened to run my script. The thing completed very quickly. After some spot checks, we determined my script was a success. The DBAs left the call shortly after that. There was one last task left. My boss wanted me to write some SQL to produce a comma separated file with the data.

That sounded simple enough. SPOOL the data out to a file. The main command was a big SELECT statement which included all the column values separated by commas. I gave it a try in development and found the lines were wrapping. So I SET LINESIZE to 200. We run the job in the production environment.

Ooops. The headers are showing up on every page. Should have SET HEADING OFF. Then we import the data into Microsoft Excel. Some rows are not lining up correctly. The LINESIZE needed to be bigger. Maybe set it to 2000? Is that too high to work? Also some of the data had commas within the values. Oh man this is getting tricky.

In the end I resorted to a tool like PL/SQL Developer to do the trick. It can export data with ease.

Overcoming the ORA-01440

I had to test the effect of changing a column type from NUMBER(1) to NUMBER. Luckily our client code did not choke. I guess they translate into integers in the programming language on the client. After the test was done, I tried to alter the table back to its original state. Unfortunately I ran into an ORA-01440.

The ORA-01440 happened because I was trying to change the type to a type with decreased precision. Apparently you can only do that if the column you are changing is empty. Well I figured I could get around that by making all the values in the column to be NULL. I could clean the data up later.

There was one additional problem in my plan. The column I was dealing with could not be NULL. I was in a bind. That's when Tech on the Net provided the idea to just DELETE the records. That way there was no data to hold me back. Actually I exported the data and reimported it after the type was changed. This is something you won't learn in the books. You got to experience this specific type of problem to be able to work around it.