Question

I need to write a stored procedure which reads input from a text file which contains employee records (multiple rows) and compares those values to that in the employee table and edits the row in the employee table if there are any modifications. I am using the DB Visualizer tool with connection type as Informix. As I am new to Informix, I don't know how to start.

The scenario is:

I have an Employee table which has fields emp_no, dept_no, fname, lname, crp_id, sal, hours etc. I have a text file in 'C:\sample.txt' with data

111  222 fname lname 3456
112  223 fname2 lname2 3457..

I need to read the first line of the text file, get the emp_no(111) and get the particular record from the emp table. Now, check for the other fields and modify if necessary. For Ex, if the first name for emp 111 in emp table is 'fname1' instead of 'fname', I need to update it. And BTW, it is from a text file that I am reading not from an external table.

Was it helpful?

Solution

Assuming you are using a recent enough version of Informix (12.10, or 11.70), then you should probably be looking to map the file to an external table, and then using the MERGE command to merge the updates and inserts (and possibly deletes) from the external table into the new table. In theory, that's about 5 SQL commands:

BEGIN WORK;
CREATE EXTERNAL TABLE mergeable_data (...) ...;
MERGE ... YourMainTable ... FROM mergeable_data
    ON MATCH UPDATE ...
    ON NO MATCH INSERT ...;  -- Check the syntax; I'm inventing on the fly!
COMMIT WORK;
DROP TABLE mergeable_data;

The BEGIN and COMMIT aren't necessary if you have a non-logged database (but why?). On the whole though, your database should be logged. You could place the COMMIT after the DROP; you could need another COMMIT after the DROP if you have a MODE ANSI database.


Given the extra information now in the question, I am more convinced than ever that using CREATE EXTERNAL TABLE that specifies the file you've got on disk as the data source and the MERGE statement is exactly what you need. The CREATE EXTERNAL TABLE statement allows IDS to treat the file as if it was a table. The MERGE statement automatically (with no programming on your part) does things like 'update the first name because it is different'.

You can do it in another way if you wish — be my guest. Are you using Informix 4GL (I4GL) or some other language? I4GL is a separate product that must be purchased, and creates compiled programs that run against the database. I've no idea how DB Visualizer would fit into that system. If you are using I4GL, you can create your temporary table and then (probably) load the data into it, and then do your matching work. Or you could use CREATE EXTERNAL TABLE and MERGE in SQL ... END SQL blocks.

I'm sure it is confusing for you, too; you're not terribly familiar with Informix and you're not quite sure what you've got and what you're using. I4GL does not use JDBC at all; it is built atop Informix ESQL/C. Using plain ESQL/C is another option, but only for masochists like myself (who've been using it for æons). And even so, I'd still be looking to use CREATE EXTERNAL TABLE and MERGE.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top