update a table by copying existing rows and change one field

Questions and answers on designing your Servoy solutions, database modelling and other 'how do I do this' that don't fit in any of the other categories

update a table by copying existing rows and change one field

Postby bobm » Sat Sep 24, 2016 1:29 pm

I am trying to update a table by copying existing rows and change one field in the new row. I was trying with SQL through pgadmin, but running into primary key issues.
The table has sequential primary keys and getting the following error:

ERROR: null value in column "diagnosis_procedure_id" violates not-null constraint

I'm thinking it might be better to do this through Servoy, but not sure how to accomplish this for all rows that contain the current diagnosis_id

The SQL I'm using in pgadmin is:
Code: Select all
INSERT INTO diagnosis_procedures
      (diagnosis_procedure_id, flag_mutially_exclusive, flag_not_appropriate, region_char_code, procedure_id, hide, code_type, diagnosis_id)

SELECT , flag_mutially_exclusive, flag_not_appropriate, region_char_code, procedure_id, hide, code_type, 14733

FROM diagnosis_procedures

WHERE diagnosis_id = 13781


Ultimately I would like to have input boxes to enter the current diagnosis_id and the new diagnosis_id

Thank you in advance for any help.
Bob
Bob Mezzadri
EyeCOR By Nteon
bobm
 
Posts: 81
Joined: Thu Dec 29, 2011 8:24 pm

Re: update a table by copying existing rows and change one f

Postby mboegem » Sat Sep 24, 2016 7:11 pm

Hi Bob,

in order to do this, the best way would be to use a PostgreSQL sequence
https://www.postgresql.org/docs/8.1/sta ... uence.html

This way PostgreSQL will handle the unique values for you, and if you like it's possible to use these sequences with regular Servoy foundsets as well.

PostgreSQL sequences have a couple of functions that can be used as documented here:
https://www.postgresql.org/docs/9.1/sta ... uence.html

Let's say you've created a sequence with name 'pk_seq_diagnosis_procedures'
The insert statement you've given would be:
Code: Select all
INSERT INTO diagnosis_procedures
      (diagnosis_procedure_id, flag_mutially_exclusive, flag_not_appropriate, region_char_code, procedure_id, hide, code_type, diagnosis_id)

SELECT nextval('pk_seq_diagnosis_procedures'), flag_mutially_exclusive, flag_not_appropriate, region_char_code, procedure_id, hide, code_type, 14733


Although all of this is perfectly possible using the rawSQL plugin from within Servoy, please be aware that any Servoy client running at the time of executing this statement, won't receive any broadcast event of the insert and therefor doesn't know about the existence of the new record(s). (There's some exception when foundset based on the table has not been cached yet, but generally you shouldn't count on that)
This could be resolved by using the plugin.rawSQL.flushClientCache() function, but it's an expensive operation not worth when only inserting single rows.
Please also note in case you use a servoy-sequence on the PK of the diagnosis_procedures table, this won't be updated which will result in a violation of unique value whenever you insert records 'the Servoy way'

I recently had to do the exact same thing in an application, but handling 10.000 records at a time which took Servoy almost 3 minutes to insert and could be done using rawSQL within 1 second!
I used a database sequence for this and have Servoy use this as well, so there will never be a violation of unique value.

The Servoy way would be something like having to foundsets, 1 source, 1 destination.
On the source foundset, perform a find/search operation.
Iterate over the records you've found and create records in the destination foundset, while setting the dataproviders with the correct values.

Having written all of the above:
I'd say keep things simple and use the power of Servoy to accomplish what you want, unless you really need to handle high volume data.
Although doing it the Servoy way will stall your development at first, but you will gain speed in building your apps/code once your Servoy skills improve.
Also using rawSQL and mix this with the way Servoy works and behaves is something you shouldn't start with.
Only do this when you have experience in Servoy and fully understand how Servoy handles foundsets, pk, sequences, etc., etc.

Hope this helps.
Marc Boegem
Solutiative / JBS Group, Partner
Servoy Specialist
• Servoy Certified Developer
• Servoy Valued Professional
• Freelance Developer

Image
User avatar
mboegem
 
Posts: 1750
Joined: Sun Oct 14, 2007 1:34 pm
Location: Amsterdam

Re: update a table by copying existing rows and change one f

Postby bobm » Sun Sep 25, 2016 6:31 pm

Marc,
Thank you for your reply - this sounds like exactly what I need. But, I'm getting the following error:

ERROR: syntax error at or near "SEQUENCE"
LINE 1: ...E off, VERBOSE off, COSTS on, BUFFERS off )CREATE SEQUENCE p...
********** Error **********
ERROR: syntax error at or near "SEQUENCE"
SQL state: 42601
Character: 66

Code: Select all
CREATE SEQUENCE pk_seq_diag_proc START WITH 1610447 INCREMENT By 1;

INSERT INTO diagnosis_procedures
      (diagnosis_procedure_id, flag_mutially_exclusive, flag_not_appropriate, region_char_code, procedure_id, hide, code_type, diagnosis_id)

SELECT nextval(pk_seq_diag_proc), flag_mutially_exclusive, flag_not_appropriate, region_char_code, procedure_id, hide, code_type, 14733

FROM diagnosis_procedures

WHERE diagnosis_id = 13781


Any idea why?
Thanks again,
Bob
Bob Mezzadri
EyeCOR By Nteon
bobm
 
Posts: 81
Joined: Thu Dec 29, 2011 8:24 pm

Re: update a table by copying existing rows and change one f

Postby mboegem » Sun Sep 25, 2016 6:58 pm

Hi Bob,

can't tell what's wrong from what you've posted here.
My PG db doesn't complain on the exact same statement. (v.9.4)

What version of PG are you running? ( SELECT version() )
I also installed a separate version of PG, using the EnterpriseDB distibution (http://www.enterprisedb.com)
Marc Boegem
Solutiative / JBS Group, Partner
Servoy Specialist
• Servoy Certified Developer
• Servoy Valued Professional
• Freelance Developer

Image
User avatar
mboegem
 
Posts: 1750
Joined: Sun Oct 14, 2007 1:34 pm
Location: Amsterdam

Re: update a table by copying existing rows and change one f

Postby bobm » Sun Sep 25, 2016 7:20 pm

Marc,

I'm running version 9.1.3 I will try a newer version.

Thanks again,
Bob
Bob Mezzadri
EyeCOR By Nteon
bobm
 
Posts: 81
Joined: Thu Dec 29, 2011 8:24 pm

Re: update a table by copying existing rows and change one f

Postby bobm » Sun Sep 25, 2016 8:24 pm

Marc,
I have loaded PG v 9.4 and was getting the same error. I changed the name of the SEQUENCE "pk_seq_diag_proc" to be the actual column name "diagnosis_procedure_id" so my query now looks like this:

Code: Select all
CREATE SEQUENCE diagnosis_procedure_id START WITH 1610447 INCREMENT By 1;


INSERT INTO diagnosis_procedures
      (diagnosis_procedure_id, flag_mutially_exclusive, flag_not_appropriate, region_char_code, procedure_id, hide, code_type, diagnosis_id)

SELECT nextval(diagnosis_procedure_id), flag_mutially_exclusive, flag_not_appropriate, region_char_code, procedure_id, hide, code_type, 14733

FROM diagnosis_procedures

WHERE diagnosis_id = 13781;



But now getting the following:

[QUERY ] CREATE SEQUENCE diagnosis_procedure_id START WITH 1610447 INCREMENT By 1
[WARNING ] INSERT INTO diagnosis_procedures
(diagnosis_procedure_id, flag_mutially_exclusive, flag_not_appropriate, region_char_code, procedure_id, hide, code_type, diagnosis_id)
SELECT nextval(diagnosis_procedure_id), flag_mutially_exclusive, flag_not_appropriate, region_char_code, procedure_id, hide, code_type, 14733
FROM diagnosis_procedures
WHERE diagnosis_id = 13781
ERROR: could not open relation with OID 1058304

The same is happening in version 9.1


Thanks,
Bob
Bob Mezzadri
EyeCOR By Nteon
bobm
 
Posts: 81
Joined: Thu Dec 29, 2011 8:24 pm

Re: update a table by copying existing rows and change one f

Postby mboegem » Sun Sep 25, 2016 11:16 pm

Hi Bob,

try executing SELECT nextval(diagnosis_procedure_id) with quotes around the sequence name, so:
Code: Select all
SELECT nextval('diagnosis_procedure_id')
Marc Boegem
Solutiative / JBS Group, Partner
Servoy Specialist
• Servoy Certified Developer
• Servoy Valued Professional
• Freelance Developer

Image
User avatar
mboegem
 
Posts: 1750
Joined: Sun Oct 14, 2007 1:34 pm
Location: Amsterdam

Re: update a table by copying existing rows and change one f

Postby bobm » Mon Sep 26, 2016 2:13 pm

Thank you Marc! That did it!

I do appreciate all your help

Thanks again,
Bob
Bob Mezzadri
EyeCOR By Nteon
bobm
 
Posts: 81
Joined: Thu Dec 29, 2011 8:24 pm


Return to Programming with Servoy

Who is online

Users browsing this forum: No registered users and 11 guests