# help with inserting into the same table and changing a field

**URL:** <https://forum.servoy.com/t/help-with-inserting-into-the-same-table-and-changing-a-field/18989>\
**Category:** Classic Servoy\
**Created:** [September 23, 2016, 2:31pm UTC](https://forum.servoy.com/t/help-with-inserting-into-the-same-table-and-changing-a-field/18989 "2016-09-23T14:31:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bobm](https://avatars.discourse-cdn.com/v4/letter/b/9fc348/32.png) [@bobm](https://forum.servoy.com/u/bobm)\
**Post date:** [September 23, 2016, 2:31pm UTC](https://forum.servoy.com/t/help-with-inserting-into-the-same-table-and-changing-a-field/18989/1 "2016-09-23T14:31:03Z")

</div>

I am trying to update a table by copying existing rows and change one field in the new row but getting various errors when doing so. The latest is:  
ERROR: column “diagnosis\_id” is of type integer but expression is of type boolean  
LINE 2: …region\_char\_code, procedure\_id, hide, code\_type, (diagnosis\_…  
^  
HINT: You will need to rewrite or cast the expression.

```auto
INSERT INTO diagnosis_procedures (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, (diagnosis_id = '14742')
FROM diagnosis_procedures
WHERE diagnosis_id = '13783'

```

Any help would be much appreciated. I am using postgresql for my database.  
Thank you,  
Bob

---

<div class="post-metadata">

**Author:** ![Bernd.N](https://avatars.discourse-cdn.com/v4/letter/b/7feea3/32.png) [@Bernd.N](https://forum.servoy.com/u/Bernd.N)\
**Post date:** [September 23, 2016, 3:53pm UTC](https://forum.servoy.com/t/help-with-inserting-into-the-same-table-and-changing-a-field/18989/2 "2016-09-23T15:53:23Z")

</div>

It is as postgres says, your last field diagnosis\_id is of type integer, but the expression below results into a boolean, because of the brackets around the expression: (diagnosis\_id = ‘14742’).  
That evaluates into a true or false.  
In case you want to put the diagnosis\_id 14742 into the new record(s), just write  
14742 AS diagnosis\_id  
That should do it.

I guess that your WHERE clause will also complain, when the diagnosis\_id is really an integer, because then the 13783 may not have quotation marks ( ’ ) around it.  
Tip: In those cases, I always align the fields in the first INSERT INTO line to the fields in the second line (with blanks), so that the field pairs can be easily seen and checked.  
And I think you do not need the AS diagnosis\_id, just try.

```auto
INSERT INTO diagnosis_procedures
      (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, 14742
FROM diagnosis_procedures
WHERE diagnosis_id = 13783

```

---

<div class="post-metadata">

**Author:** ![bobm](https://avatars.discourse-cdn.com/v4/letter/b/9fc348/32.png) [@bobm](https://forum.servoy.com/u/bobm)\
**Post date:** [September 23, 2016, 4:24pm UTC](https://forum.servoy.com/t/help-with-inserting-into-the-same-table-and-changing-a-field/18989/3 "2016-09-23T16:24:23Z")

</div>

Thank you Bernd,  
This is getting me closer. I guess something I forgot to mention is I have a primary key field set to not-null -  
so when I run this I am getting the following message:

ERROR: null value in column “diagnosis\_procedure\_id” violates not-null constraint

How can I set the diagnosis\_procedure\_id to auto increment as new records are added?  
Thanks again,  
Bob

---

<div class="post-metadata">

**Author:** ![Bernd.N](https://avatars.discourse-cdn.com/v4/letter/b/7feea3/32.png) [@Bernd.N](https://forum.servoy.com/u/Bernd.N)\
**Post date:** [September 23, 2016, 5:30pm UTC](https://forum.servoy.com/t/help-with-inserting-into-the-same-table-and-changing-a-field/18989/4 "2016-09-23T17:30:21Z")

</div>

That depends on the data type of your primary key.  
We use for example only UUIDs, and we would have to create a new UUID with the expression  
UPPER(uuid\_generate\_v4()::varchar)

So in case you use UUIDs, that would be

```auto
INSERT INTO diagnosis_procedures
      (flag_mutially_exclusive, flag_not_appropriate, region_char_code, procedure_id, hide, code_type, diagnosis_id, diagnosis_procedure_id)
SELECT flag_mutially_exclusive, flag_not_appropriate, region_char_code, procedure_id, hide, code_type, 14742, UPPER(uuid_generate_v4()::varchar)
FROM diagnosis_procedures
WHERE diagnosis_id = 13783

```

In case you use integers as primary keys, I do not know at the moment how to do that in such an INSERT case, but I guess other users here can easily answer that.  
You can also google with a smart word combination, like “postgres insert primary key autoincrement”, I learned a lot with that strategy.  
You will find for example

> <https://dba.stackexchange.com/questions/65662/postgres-how-to-insert-row-with-autoincrement-id>

Servoy is using the same strategy for integers as far as I know, when you have autoincrement switched on for the table and Servoy adds a record.

---

<div class="post-metadata">

**Author:** ![bobm](https://avatars.discourse-cdn.com/v4/letter/b/9fc348/32.png) [@bobm](https://forum.servoy.com/u/bobm)\
**Post date:** [September 23, 2016, 6:09pm UTC](https://forum.servoy.com/t/help-with-inserting-into-the-same-table-and-changing-a-field/18989/5 "2016-09-23T18:09:04Z")

</div>

Thanks Bernd,

Yes, my primary key is an integer. I’ll google this as well and see if I can figure it out from there. But if anyone has suggestions, please let me know.

Thanks,  
Bob
