Joshua Bixby wrote:
Blake, why do you want "current RowID"? Or, are you after the highest/largest ObjectID in use?
I assumed they would be the same. Is it possible for a new feature to get an ObjectID lower than the highest ObjectID already assigned?
My case is that we're trying to build a database trigger (yeah, I know, probably shaking your head already) that will assign our organization's custom "GlobalID" we call EID (for Enterprise ID). It's just a long integer assigned from a sequence. The trigger calling the sequence is built on the Adds table. When a record is created in the Adds table, it generates an EID value if:
- Current EID is Null (for new records)
- If new ObjectID > max ObjectID (for features that are copy/pasted or split)
The extra catch I ran into was that finding the max ObjectID needs to look at both the Base table and the Adds table or else a brand new feature will get a new EID every time it's modified until it's been compressed to the Base table.
My Original idea was to just query the base table and adds table in the trigger but apparently that causes a "mutation" error in Oracle. So my current solution is a function that queries the base and adds tables for the max ObjectID.
<SPAN class="" style="color: #0077aa; border: 0px; font-weight: inherit;">CREATE</SPAN> <SPAN class="" style="color: #a67f59; background: rgba(255, 255, 255, 0.5); border: 0px; font-weight: inherit;">OR</SPAN> REPLACE <SPAN class="" style="color: #0077aa; border: 0px; font-weight: inherit;">FUNCTION</SPAN> GIS<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;">.</SPAN>MaxObjectID<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;">(</SPAN>p_owner VARCHAR2<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;">,</SPAN> p_tablename VARCHAR2<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;">)</SPAN> <SPAN class="" style="color: #0077aa; border: 0px; font-weight: inherit;">RETURN</SPAN> NUMBER <SPAN class="" style="color: #a67f59; background: rgba(255, 255, 255, 0.5); border: 0px; font-weight: inherit;">IS</SPAN>
max_objectid NUMBER<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;">;</SPAN>
regid NUMBER<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;">;</SPAN>
vc2SqlStr VARCHAR2<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;">(</SPAN><SPAN class="" style="color: #990000; border: 0px; font-weight: inherit;">1000</SPAN><SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;">)</SPAN><SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;">;</SPAN>
<SPAN class="" style="color: #0077aa; border: 0px; font-weight: inherit;">BEGIN</SPAN>
<SPAN class="" style="color: #0077aa; border: 0px; font-weight: inherit;">BEGIN</SPAN>
<SPAN class="" style="color: #0077aa; border: 0px; font-weight: inherit;">select</SPAN> tr<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;">.</SPAN>REGISTRATION_ID
<SPAN class="" style="color: #0077aa; border: 0px; font-weight: inherit;">into</SPAN> regid
<SPAN class="" style="color: #0077aa; border: 0px; font-weight: inherit;">from</SPAN> SDE<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;">.</SPAN>TABLE_REGISTRY tr
<SPAN class="" style="color: #0077aa; border: 0px; font-weight: inherit;">where</SPAN> tr<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;">.</SPAN>OWNER <SPAN class="" style="color: #a67f59; background: rgba(255, 255, 255, 0.5); border: 0px; font-weight: inherit;">=</SPAN> p_owner <SPAN class="" style="color: #a67f59; background: rgba(255, 255, 255, 0.5); border: 0px; font-weight: inherit;">and</SPAN> tr<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;">.</SPAN>TABLE_NAME <SPAN class="" style="color: #a67f59; background: rgba(255, 255, 255, 0.5); border: 0px; font-weight: inherit;">=</SPAN> p_tablename<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;">;</SPAN>
EXCEPTION <SPAN class="" style="color: #0077aa; border: 0px; font-weight: inherit;">WHEN</SPAN> OTHERS <SPAN class="" style="color: #0077aa; border: 0px; font-weight: inherit;">THEN</SPAN>
regid :<SPAN class="" style="color: #a67f59; background: rgba(255, 255, 255, 0.5); border: 0px; font-weight: inherit;">=</SPAN> <SPAN class="" style="color: #990000; border: 0px; font-weight: inherit;">0</SPAN><SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;">;</SPAN>
<SPAN class="" style="color: #0077aa; border: 0px; font-weight: inherit;">END</SPAN><SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;">;</SPAN>
vc2SqlStr :<SPAN class="" style="color: #a67f59; background: rgba(255, 255, 255, 0.5); border: 0px; font-weight: inherit;">=</SPAN> <SPAN class="" style="color: #669900; border: 0px; font-weight: inherit;">'select NVL(max(objectid), 0) from (
select max(OBJECTID) as objectid from '</SPAN><SPAN class="" style="color: #a67f59; background: rgba(255, 255, 255, 0.5); border: 0px; font-weight: inherit;">||</SPAN>p_owner<SPAN class="" style="color: #a67f59; background: rgba(255, 255, 255, 0.5); border: 0px; font-weight: inherit;">||</SPAN><SPAN class="" style="color: #669900; border: 0px; font-weight: inherit;">'.'</SPAN><SPAN class="" style="color: #a67f59; background: rgba(255, 255, 255, 0.5); border: 0px; font-weight: inherit;">||</SPAN>p_tablename<SPAN class="" style="color: #a67f59; background: rgba(255, 255, 255, 0.5); border: 0px; font-weight: inherit;">||</SPAN>
<SPAN class="" style="color: #669900; border: 0px; font-weight: inherit;">' union all
select max(OBJECTID) as objectid from '</SPAN><SPAN class="" style="color: #a67f59; background: rgba(255, 255, 255, 0.5); border: 0px; font-weight: inherit;">||</SPAN>p_owner<SPAN class="" style="color: #a67f59; background: rgba(255, 255, 255, 0.5); border: 0px; font-weight: inherit;">||</SPAN><SPAN class="" style="color: #669900; border: 0px; font-weight: inherit;">'.A'</SPAN><SPAN class="" style="color: #a67f59; background: rgba(255, 255, 255, 0.5); border: 0px; font-weight: inherit;">||</SPAN>regid<SPAN class="" style="color: #a67f59; background: rgba(255, 255, 255, 0.5); border: 0px; font-weight: inherit;">||</SPAN>
<SPAN class="" style="color: #669900; border: 0px; font-weight: inherit;">')'</SPAN><SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;">;</SPAN>
<SPAN class="" style="color: #0077aa; border: 0px; font-weight: inherit;">BEGIN</SPAN>
<SPAN class="" style="color: #0077aa; border: 0px; font-weight: inherit;">EXECUTE</SPAN> IMMEDIATE vc2SqlStr <SPAN class="" style="color: #0077aa; border: 0px; font-weight: inherit;">INTO</SPAN> max_objectid<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;">;</SPAN>
EXCEPTION <SPAN class="" style="color: #0077aa; border: 0px; font-weight: inherit;">WHEN</SPAN> OTHERS <SPAN class="" style="color: #0077aa; border: 0px; font-weight: inherit;">THEN</SPAN>
max_objectid :<SPAN class="" style="color: #a67f59; background: rgba(255, 255, 255, 0.5); border: 0px; font-weight: inherit;">=</SPAN> <SPAN class="" style="color: #a67f59; background: rgba(255, 255, 255, 0.5); border: 0px; font-weight: inherit;">-</SPAN><SPAN class="" style="color: #990000; border: 0px; font-weight: inherit;">1</SPAN><SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;">;</SPAN>
<SPAN class="" style="color: #0077aa; border: 0px; font-weight: inherit;">END</SPAN><SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;">;</SPAN>
<SPAN class="" style="color: #0077aa; border: 0px; font-weight: inherit;">RETURN</SPAN> max_objectid<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;">;</SPAN>
<SPAN class="" style="color: #0077aa; border: 0px; font-weight: inherit;">END</SPAN> MaxObjectID<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;">;</SPAN><SPAN class="" style="border-width: 0px 1px 0px 0px; border-style: initial solid initial initial; border-color: initial #999999 initial initial; font-weight: inherit;"></SPAN>
This function is called in the trigger.
BEFORE <SPAN class="" style="color: #0077aa; border: 0px; font-weight: inherit;">INSERT</SPAN> <SPAN class="" style="color: #0077aa; border: 0px; font-weight: inherit;">ON</SPAN> WS<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;">.</SPAN>A658 <SPAN class="" style="color: slategray; border: 0px; font-weight: inherit;">-- Adds Table</SPAN>
<SPAN class="" style="color: #0077aa; border: 0px; font-weight: inherit;">for each row</SPAN>
<SPAN class="" style="color: #0077aa; border: 0px; font-weight: inherit;">declare</SPAN>
max_objectid WS<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;">.</SPAN>A658<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;">.</SPAN>objectid<SPAN class="" style="color: #a67f59; background: rgba(255, 255, 255, 0.5); border: 0px; font-weight: inherit;">%</SPAN><SPAN class="" style="color: #0077aa; border: 0px; font-weight: inherit;">type</SPAN>:<SPAN class="" style="color: #a67f59; background: rgba(255, 255, 255, 0.5); border: 0px; font-weight: inherit;">=</SPAN><SPAN class="" style="color: #990000; border: 0px; font-weight: inherit;">0</SPAN><SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;">;</SPAN>
<SPAN class="" style="color: #0077aa; border: 0px; font-weight: inherit;">begin</SPAN>
max_objectid :<SPAN class="" style="color: #a67f59; background: rgba(255, 255, 255, 0.5); border: 0px; font-weight: inherit;">=</SPAN> GIS<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;">.</SPAN>MaxObjectID<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;">(</SPAN><SPAN class="" style="color: #669900; border: 0px; font-weight: inherit;">'WS'</SPAN><SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;">,</SPAN> <SPAN class="" style="color: #669900; border: 0px; font-weight: inherit;">'HYDRANT_PNT'</SPAN><SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;">)</SPAN><SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;">;</SPAN> <SPAN class="" style="color: slategray; border: 0px; font-weight: inherit;">-- Base Table Owner, Name</SPAN>
<SPAN class="" style="color: #0077aa; border: 0px; font-weight: inherit;">if</SPAN> max_objectid <SPAN class="" style="color: #a67f59; background: rgba(255, 255, 255, 0.5); border: 0px; font-weight: inherit;"><></SPAN> <SPAN class="" style="color: #a67f59; background: rgba(255, 255, 255, 0.5); border: 0px; font-weight: inherit;">-</SPAN><SPAN class="" style="color: #990000; border: 0px; font-weight: inherit;">1</SPAN> <SPAN class="" style="color: #a67f59; background: rgba(255, 255, 255, 0.5); border: 0px; font-weight: inherit;">and</SPAN> <SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;">(</SPAN>:new<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;">.</SPAN>eid <SPAN class="" style="color: #a67f59; background: rgba(255, 255, 255, 0.5); border: 0px; font-weight: inherit;">is</SPAN> <SPAN class="" style="color: #990000; border: 0px; font-weight: inherit;">NULL</SPAN> <SPAN class="" style="color: #a67f59; background: rgba(255, 255, 255, 0.5); border: 0px; font-weight: inherit;">or</SPAN> :new<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;">.</SPAN>objectid <SPAN class="" style="color: #a67f59; background: rgba(255, 255, 255, 0.5); border: 0px; font-weight: inherit;">></SPAN> max_objectid<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;">)</SPAN> <SPAN class="" style="color: #0077aa; border: 0px; font-weight: inherit;">then</SPAN>
<SPAN class="" style="color: #0077aa; border: 0px; font-weight: inherit;">SELECT</SPAN> HYDRANT_EID_SEQ<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;">.</SPAN>NEXTVAL <SPAN class="" style="color: #0077aa; border: 0px; font-weight: inherit;">INTO</SPAN> :NEW<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;">.</SPAN>EID <SPAN class="" style="color: #0077aa; border: 0px; font-weight: inherit;">FROM</SPAN> DUAL<SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;">;</SPAN>
<SPAN class="" style="color: #0077aa; border: 0px; font-weight: inherit;">end</SPAN> <SPAN class="" style="color: #0077aa; border: 0px; font-weight: inherit;">if</SPAN><SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;">;</SPAN>
<SPAN class="" style="color: #0077aa; border: 0px; font-weight: inherit;">end</SPAN><SPAN class="" style="color: #999999; border: 0px; font-weight: inherit;">;</SPAN><SPAN class="" style="border-width: 0px 1px 0px 0px; border-style: initial solid initial initial; border-color: initial #999999 initial initial; font-weight: inherit;"></SPAN>
So I'm looking for a better way to determine the max ObjectID (or just whether a feature is new). I was hoping the <owner>.R<registration_id> sequence would be the magic bullet I could call CURRVAL on but the value it returns does not seem to be correct.
Also paging David Blanchardâ