Friday, 9 December 2016

A Simple Guide to Oracle Data Pump


In order to create a test system for one of our customers I needed to copy some schemas from the live oracle database. As the main schema contained a lot of objects, I realised that the best thing to use was Data Pump. Something I'd never used it before.

Oracle Data Pump first appeared in 10g and provided a newer and more flexible alternative to the 'exp' and 'imp' utilities from previous Oracle versions. It's a server based technology so you'll need access to the file directory structure on the database server. That means you'll need to be able to remote connect to both source and target servers.

Running an Export

We're going to start by running a schema export, it's quite straight forward, but we need to ensure that we have a directory object configured in Oracle. You could go ahead and just add one, but it's worth looking to see if there's one already set up that you can use. Log into the source server (in my case Live) and type the following:-

SQL> select directory_name, directory_path from all_directories;

The results should give you a column listing the object name, and a second stating it's actual directory path on disk. It makes sense to choose one where the directory actually exists, and where you have file creations rights, but that should go without saying!

If nothing suitable exists then go ahead and create one, and then grant yourself read and write on it.

You run the export from the operating systems command prompt, here's what I used:-

expdp <my user>/<my password> directory=<my directory object> dumpfile=<my export file>.dmp schemas=<schema to export>

When it runs it will output process to the screen and may take a number of minutes to complete (depending on number of objects and size of tables). If you'd prefer the progress can be sent to a file just by including the following paramater:-

  logfile=<export log>.log

Now if you go to the directory listed in the directory_path you should see your DMP file waiting for you. They can be quite large but normally they zip quite well to make file transfer quicker.

Running an Import

The obvious next step was to copy the dump file over to the target server (in my case the new test system), but don't worry about where to put it just yet. What we need to do again is find an Oracle directory object to use for the import process.

I used the same query as before:-

SQL> select directory_name, directory_path from all_directories;

If a directory object exists then move your dump file into it, or (as before) create your own directory object in Oracle.

Before running the import we need to connect as sysdba and create the empty schema in the test system.

SQL> create user newschema identified by pwd4newschema;

(NB. The "Identified By" parameter is the password.)

Finally at the command prompt run the import command:-

impdp <my user>/<my password> DIRECTORY=<my directory object> DUMPFILE=<my export file>.dmp

Again the progress will be reported to the screen. Scan through it and check you don't get any errors. I had some dependancy issues because two other schemas referenced in the packaged functions where missing. If this happens to you, copy the missing references and recompile your packages.

Wednesday, 7 September 2016

QR Code Treasure Hunt for kids


Like many, I found the introduction of QR codes quite an intriguing idea. A simple pattern of squares which could be scanned with your smartphone and take you to a webpage or display a block of text.

It wasn't long before I started seeing these alongside museum exhibitions enabling visitors to see additional information, and this in turn has tended to drive public wifi access.

Anyway, my kids think these things are magical.

QR Code with a URL (Area 5.1 Cartoon)

Let's have a Treasure Hunt

One rainy day after the kids had been scanning QR codes on the back of the MagPi magazine I told them about my idea for using QR codes for a treasure hunt. They'd heard of this activity (although not with QR Codes) and were keen to try it, but getting decent clues seemed to be a bit of a problem for them. We googled a few websites that gave some examples, but we weren't impressed. I was sure I could do better.

So I hatched the idea to write my own clues and run a treasure hunt for them about the house and garden.

Building the Clues

There's various ways you can go about creating clues, but care should be taken to ensure you pitch the difficulty level right. You don't want them getting it too quickly, but it should force them to reason it out with a bit of thought. I decided to make mine rhyme and I used the site to help me.

Here's a few of my examples:-
  • Mirrored Bathroom Cabinet: I’m a cupboard shiny and white, I reflect the world from this clean height. 
  • Door Mat: Stand on me, I won’t get sore, you’ll often find me by the door. 
  • Bike Shed: I’m never too sleepy, but I’m always two tired. In my wooden house your clue can be acquired. 
  • Nut Basket: At christmas time just get snacking, a basket of these will get you cracking. 

Try to include objects from all over the house and garden, including things from their rooms as well as everyday objects. Aim to have at least a dozen, and if you think your clue might be too hard, have a 2nd clue handy. (Before you go much further, review these with another person to ensure you have gauged the level correctly)

Generating QR Codes

Now you have your clues ready, the next step is to convert them into codes. The easiest way to do this is using one of the online webservices. (I used which worked quite well, but it added a bit of a delay between downloads. This could be reduced by refreshing the page every half a dozen or so.)

The one thing I did notice is that longer clues increased the density of the pattern.

OK, once you've finished you should have a load of image files (make sure you unzip them if your chosen service compresses them). Next I added mine into iPhoto (because I use a Mac) and this enabled me to produce a contact sheet (you select it from the printing page). This was just an easy way of getting multiple QR images onto one sheet of paper so they didnt turn out huge when printed. You could manually add them into a word document, or similar if you want.

My QR Code contact sheet
Once printed, cut them into individual squares. Here's where it gets tricky, you'll want your phone handy with your favourite bar code reader (I was using RedLaser on the iphone).

Setting up the Treasure Hunt

I got into a real mess here and ended up with clues leading round in circles, so do yourself a favour and work out the route beforehand. Work through systematically using your phone to read each code. It took me about half an hour to set up 14 clues, so make sure your kids are out of the way, or busy. (Mine were playing Minecraft)

Then I left the first clue lying around where they could find it, and they took the bait. It was great fun watching them running about, trying different ideas and occasionally even working together. An hour later they reached the end where I'd left them each a packet of smarties.

If you liked this idea, you might be interested in my later post about Secret Santa using QR Codes.

Thursday, 25 February 2016

Oracle APEX - Updating data in a report using AJAX


I have a simple report showing hundreds of rows of config type information which I wanted to be able to alter quickly. I don't want to open each row in a form, or have to submit a tabular form to update any changes,.. it's all too slow.

What I want instead is a checkbox type column in the report which when clicked causes an AJAX request will be fired off to the server, which in turn runs an update query.

Sounds simple enough!

Setting up the Report

Let's start by looking at the report..

I've added a column called 'Ignore' which looks like a radio field/item. When clicked this will run an update to toggle the value in the database table.

Here's SQL that creates this in the report (notice I'm using a graphic of a radio button instead or a HTML form elements).

CASE WHEN sm.method = '3' THEN
         '<img name='||sd.personkey||' src="#IMAGE_PREFIX#pkt_rb_chk.gif" title="Ignored, click to remove" onclick="clicked(this)">'
         '<img name='||sd.personkey||' src="#IMAGE_PREFIX#pkt_rb.gif" title="Click to ignore" onclick="clicked(this)">'
       END ignore,

My database table stores an ignore as a '3' in the 'method' column. And, the reason why I'm using images is because I wanted to store the record key against the images using its name attribute. (It simplifies the javascript.)

Adding the Javascript

Next I added the following code to the Function and Global Variable Declaration on the report page. It uses some handy JQuery functions to make the code easier.

function clicked(obj){
  var personkey =;
  var img = obj.src.replace(/http:+\/.+\/+/,'');
  var checked;
  if (img == 'pkt_rb.gif') {
    checked = 1;
    var match = $(obj).closest('td').prev('td').text();
    if (match == 'Automatic' || match == 'Manual'){
      // Prompt for confirmation..
      var msg=confirm("This action will remove the current match, continue?");
      if (msg!=true){
  else { checked = 0; }
  var ajax = new htmldb_Get(null,$v('pFlowId'),
  ajax = null;
  if (checked == 1){
    obj.src = obj.src.replace(/.gif$/,'') + '_chk.gif';
  else {
    obj.src = obj.src.replace(/_chk.gif$/,'') + '.gif';

The function takes the image object and uses a regular expression to find out the image name, and thus whether it's selected or not. (This gives us the initial 'ignore' status) We throw up a confirmation box if ignore is being set and then we start the AJAX call.

The AJAX call uses an APEX standard function called html_Get. This handles the request, the parameters you need to pass, and any browser variations. In our example we don't have any values being returned to the browser, but it would be easy to handle this by altering the 'get' command.

var gReturn = ajax.get();
// then do something with this value 

The final stage is to alter the radio image, and its hover-over text. Again we use a regular expression to alter the image name.

Next we need the back-end code that updates the table.

Adding an Application Process

You possibly noticed the AJAX function above is calling an Application Process called 'ToggleIgnore'.

So I created this Application Process using the name 'ToggleIgnore' and used the following anonymous block of PL/SQL code.

  l_person_key varchar2(20);
  l_checkbox varchar2(1);
  l_person_key := apex_application.g_x01;
  l_checkbox := apex_application.g_x02;
  // Update the table
    p_personkey => l_person_key,
    p_checked   => l_checkbox

The parameters are passed into p_personkey and p_checked, and in my example I've stored the update code in a packaged procedure. (It simply runs an update, but you shouldn't need me to go through that part).

Hopefully that's enough to help you get started building your own custom AJAX update methods.