insert with select if the field is null

-2

I have this TODAY in JAVA running but I need to implement a new routine:

            public void gravarLogAbonoUnificado(TbLogAbonClickUnic entity,
            RetornoAuxDto auxDto) {


            STRING SQL = "INSERT INTO" +
            "TB_LOG_ABON_CLIK_UNIC" +
            "(NR_SEQU_LOG_LOG_CLICK_UNIC, NR_SEQU_TIPO_MOTI, CD_PODR) +
            "VALUES (?,?,?)";

            Query query = em.createNativeQuery(hql);

            Query.setParameter(1, entity.getTbLogEntrClickUnic().
            getNrSequeLogClikUnic()).

            Query.setParameter(2, (long) auxDto.getCodRetorno());

            //ESSE MEU CAMPO SE VIER NULL, PRECISA QUE SEJA INSERIDO //
              Query.setParameter(3, (long) auxDto.getCodRetorno());

            query.executeUpdate;

If my CD_PODR field is NULL it should do the insertion with that select:

                  select p.nr_seque_item_podr from
            tb_tipo_grup_podr g,
            tb_tipo_item_podr p,
            where
            g.nr_seque_grup_podr = p.nr_seq_grup_podr and
            g.sg_grup_podr = '09'
            and
            p.sg_item_podr = '01'

How can I do this?

    
asked by anonymous 13.11.2018 / 14:02

1 answer

1

Bruno, in this case you can do a check before insert, if the value that will pass in the values corresponding to column "CD_PODR" is NULL, you perform the following insert:

STRING SQL = "INSERT INTO" +
        "TB_LOG_ABON_CLIK_UNIC" +
        "(NR_SEQU_LOG_LOG_CLICK_UNIC, NR_SEQU_TIPO_MOTI, CD_PODR)" +
        "(select CAMPO1, CAMPO2, CAMPO3, p.nr_seque_item_podr " +
        "  from tb_tipo_grup_podr g," +
        "       tb_tipo_item_podr p," +
        "  where" +
        "   g.nr_seque_grup_podr = p.nr_seq_grup_podr " +
        "   and g.sg_grup_podr = '09'" +
        "   and p.sg_item_podr = '01'" 

To perform an insert through a select, in the select itself you must return all the fields that are expected to be inserted. I put the "FIELDS" representing that you have this information in your select, if you do not have it and wanted to use what you are receiving in your method, you can do this sentence as follows:

"(select" + VCAMPO1 + VCAMPO2 + VCAMPO3 + "p.nr_seque_item_podr " +

Here I put "VCAMPOS" as fields of your code and if your CD_PODR field is DIFFERENT FROM NULL, you perform the insert you make today.

    
20.11.2018 / 23:51