Search code examples
sqloracle-databasesql-insertdatabase-sequence

Cannot insert a number using SQL - whole column is removed / number is null


I'm trying to insert a Number as an ID in a column in my database by a loop which sets the new ID by a Sequence. When I try to insert it, the row hasn't been created or the field where my number should be is null.

This only appears when I'm trying to insert the value of the current number of the sequence. Any other number can be inserted. The id isn't a primary key or foreign key, just a normal field. I have tried to insert it in a few different methods like to select it from a field or to hardcode the value in the insert script. If its hardcoded with, the value of the sid, no row is inserted.

insert into SCHULP_BEZIEHUNG (R_ID, B_ID,S_ID,WDH,HOTEL)
 values( 
      SEQ_SCHULP_ID.NEXTVAL
       ,l_selected(i)
       ,(select distinct SSID from SCHULP_SCHULUNGEN where SID = :P2_ID and SSID is not null)    
       ,5
       ,'test');

My expected result is that it inserts the nummber of SID in the column S_ID

Images of the Database:

select from here

insert here


Solution

  • What is this:

    This only appears when I'm trying to insert the value of the current number of the sequence.

    related to? If seq_schulp_id.nextval which - in your attempt - was actually seq_schulp_id.currval, then yes, it won't work until you fetch nextval first because currval isn't yet defined within the current session.

    Another objection goes to :P2_ID which is an Apex page item. If you want to be able to use it, it isn't enough that you see it on the screen - it has to be put into session state. The simplest way to do that is to submit the page first, then run the process which will insert data.

    On the other hand, you said that you hardcoded value (which value? :P2_ID?) but you still didn't manage to insert value into S_ID column. It means that the whole query:

    select distinct SSID from SCHULP_SCHULUNGEN where SID = :P2_ID and SSID is not null
    

    returned nothing.

    You posted incomplete code (what is l_selected(i)?); I can't tell whether there's something else that might be wrong.