Error in query with WHERE AND COUNT in Oracle. "SQL command not properly ended"

2

I have the following query:

INSERT INTO TB_CLASSIFICACAO_UG 
    (COD_CLASSIFICACAO_UG, TXT_DESCRICAO, IND_ATIVO) 
VALUES 
    (SEQ_COD_TB_CLASSIFICACAO_UG.nextval, 'TESTANDO', 1)
WHERE 
    (SELECT COUNT(TXT_DESCRICAO) FROM TB_CLASSIFICACAO_UG WHERE TXT_DESCRICAO = 'TESTANDO') = 0

This query generates the error:

"SQL command not properly ended"

But if I run only the first part it works.

INSERT INTO TB_CLASSIFICACAO_UG 
    (COD_CLASSIFICACAO_UG, TXT_DESCRICAO, IND_ATIVO) 
VALUES 
    (SEQ_COD_TB_CLASSIFICACAO_UG.nextval, 'TESTANDO', 1)

And if I run the second part too.

(SELECT COUNT(TXT_DESCRICAO) FROM TB_CLASSIFICACAO_UG WHERE TXT_DESCRICAO = 'TESTANDO') = 0

What am I doing wrong?

What I want at the end of the day is an INSERT IF NOT EXISTS. Any suggestions for a query? I've tried it in other ways besides that and it did not work.

    
asked by anonymous 07.02.2014 / 13:44

5 answers

1

The INSERT (SQL ANSI) command does not accept the WHERE clause, this need to insert only if it does not exist is relatively common but its implementation changes according to the database, one approach I recommend is to check this in your code. application so that the logic becomes clearer.

If you choose to do everything in SQL it is still possible, as follows (in oracle):

begin
  insert into TB_CLASSIFICACAO_UG
         (COD_CLASSIFICACAO_UG, TXT_DESCRICAO, IND_ATIVO) 
  values (SEQ_COD_TB_CLASSIFICACAO_UG.nextval, 'TESTANDO', 1)
  ;
  commit;
exception
  when DUP_VAL_ON_INDEX
  then ROLLBACK;
end;
    
07.02.2014 / 14:32
0

Try to invert the order of your query to something similar to the following:

INSERT INTO TB_CLASSIFICACAO_UG 
    (SELECT COD_CLASSIFICACAO_UG, TXT_DESCRICAO, IND_ATIVO FROM TB_CLASSIFICACAO_UG WHERE TXT_DESCRICAO <> 'TESTANDO')
VALUES 
(SEQ_COD_TB_CLASSIFICACAO_UG.nextval, 'TESTANDO', 1)
    
07.02.2014 / 14:27
0

For conditional insertion into Oracle you can use the % with_% / a>

MERGE
   INTO  TB_CLASSIFICACAO_UG
   USING (SELECT COUNT(TXT_DESCRICAO) AS count FROM TB_CLASSIFICACAO_UG WHERE TXT_DESCRICAO = 'TESTANDO') s
     ON   (s.count > 0)
 WHEN NOT MATCHED THEN
     INSERT (COD_CLASSIFICACAO_UG, TXT_DESCRICAO, IND_ATIVO)
     VALUES (SEQ_COD_TB_CLASSIFICACAO_UG.nextval, 'TESTANDO', 1);

Test in SQLFiddle: link

    
07.02.2014 / 14:33
0

For a record to be inserted only if it does not already exist, databases have unique key constraints.

In your example, simply transform the TXT_DESCRICAO field into a unique key using the command:

ALTER TABLE TB_CLASSIFICACAO_UG
ADD CONSTRAINT TB_CLASSIF_TXT UNIQUE (TXT_DESCRICAO);

In this way, any routine or query will be prevented from entering a value for TXT_DESCRICAO that already exists in the table.

    
07.02.2014 / 14:53
0

I was able to work out a solution to my problem:

INSERT INTO TB_CLASSIFICACAO_UG 
    (COD_CLASSIFICACAO_UG, TXT_DESCRICAO, IND_ATIVO) 
VALUES 
    (SEQ_COD_TB_CLASSIFICACAO_UG.nextval, (SELECT '" + descricao + "' FROM dual WHERE NOT EXISTS (SELECT NULL FROM TB_CLASSIFICACAO_UG WHERE TXT_DESCRICAO = '" + descricao + "')) , 1);
    
07.02.2014 / 14:59