/
How to reset PSPEC_UID sequences?
Got feedback or spotted a mistake?

Leave a comment at the end of this page or email contact@krishagni.com

How to reset PSPEC_UID sequences?

Please be sure to test below SQL on your test/dev instances before running on the prod database instance.

Oracle
merge into key_seq_generator d 
using (
  select
    p.identifier as parent_id, count(c.identifier) as children
  from 
    catissue_specimen p 
    inner join catissue_specimen c on c.parent_specimen_id = p.identifier 
  where 
    c.label is not null
  group by
    p.identifier
) s on (d.key_type = 'PSPEC_UID' AND d.key_value = to_char(s.parent_id))
when matched then 
  update set d.key_sequence_id = to_char(s.children)
when not matched then 
  insert 
    (identifier, key_type, key_value, key_sequence_id) 
  values
    (key_generator_seq.nextval, 'PSPEC_UID', to_char(s.parent_id), to_char(s.children))

Note: The above SQL doesn't work as expected when one or more child specimens in between the hierarchy are "hard" deleted. 

Related content

How to reset label sequences?
How to reset label sequences?
More like this
Why do specimen hierarchical queries return incorrect results?
Why do specimen hierarchical queries return incorrect results?
More like this
Fixing database constraint errors
Fixing database constraint errors
More like this
How to update Participant protocol identifier(PPID) in bulk?
How to update Participant protocol identifier(PPID) in bulk?
More like this
How to get rid of duplicate events from OS_SPMN_COLL_RECV_DETAILS_VIEW?
How to get rid of duplicate events from OS_SPMN_COLL_RECV_DETAILS_VIEW?
More like this
How to resolve permissible-values.xml changeset failed.
How to resolve permissible-values.xml changeset failed.
More like this
Got feedback or spotted a mistake?

Leave a comment at the end of this page or email contact@krishagni.com