Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Join us in Bucharest on Sept 18th for Qlik's AI Reality Tour! Register Now
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

Insert or update with ID from context (that can be null)

Hello,

 

I would like to insert or update datas in a database depending on the value of the ID available in my context. (see screenshots below)

 

0683p000009M1Ed.png

 

2 cases :

  • My ID (context.asset_id) is not null so it exists in the output table => update => working well
  • My ID (context.asset_id) is null => insert (using auto_increment on the ID which has been enabled) => throw an exception

 

0683p000009M1Vr.png

0683p000009M1Vw.png

At the moment, in order to make it works, I'm using two different jobs (one for updates and one for inserts) and a tFilterRow which switch null id's to insert job and not null id's to update job. The problem is both jobs are quite identical so that's not really clean ...

 

How could I make it work as I wish? 0683p000009MACn.png

 

Thanks

 

Labels (2)
4 Replies
Anonymous
Not applicable
Author

 

Update and Insert are two distinct operations: why do you want to combine the jobs? It sounds like more trouble than it's worth.

 

How is the job handling null values for the ID field now?

Anonymous
Not applicable
Author

I would like to combine these two jobs because they are identical and it doubles the cost of maintenance if a modification need to be done... 

Null values are not handled at all, let me explain you how it works.

 

I have a table with two ID's columns (ID from old db, ID from new db). I read each row of this table and 2 cases are possible:

  1. if ID from new db is not null then I update datas by using the ones from old db.
  2. if ID from new db is null then I insert datas from old db to new db.

I use a tFilterRow to switch on the value of the ID (Not null => update job, null => insert job).

 

The only difference between these 2 jobs is in the tMap before insert or update datas (tDBOutput). If I am inside inserting job then the field ID is left empty, if I am inside updating job then the field ID is filled.

 

Anonymous
Not applicable
Author

I'm assuming the new records have an auto-increment ID field, so that you just need to handle the NullPointer exception, and not generate a new unique ID, correct? If so, you could test the ID field in tMap to see if it's empty, and if it is, assign a placeholder value to avoid the exception. You might be able to use Java's isNull() function; something like (in the tMap expression for the ID field):

 

row1.ID.isNull() ? "123" : row1.ID

 

The "123" can be any value, since you're just going to discard it. If isNull() doesn't work (sometimes it doesn't), then try comparing to the empty string:

 

row1.ID.equals("") ? "123" :row1.ID

 

If ID is a number, then:

 

row1.ID == "" ? 123 : row1.ID

 

Hope this helps.

Anonymous
Not applicable
Author

Yes, as you assume, I have an auto-incrementing ID inside my new table but what do you mean by using any value which will be discarded ? If I fill the ID field with any value, it will try to insert a row with this ID, or even try to update a row with this ID (supposing I have defined the action "insert or update" on the table).