0
I need to create a column in an existing table but need to do with a checking condition if this column already exists in the bank I did so but it throws an error:
DECLARE
col_count integer;
BEGIN
SELECT count(*)
into col_count
FROM user_tab_columns
WHERE table_name = '<nomeTabela>'
AND column_name = '<nomeColuna>';
IF col_count = 0 THEN
EXECUTE IMMEDIATE 'ALTER TABLE <nomeTabela> add <nomeColuna> char(1) default 'A' not null';
END IF;
END;
The log of the error:
Erro a partir da linha : 4 no comando -
DECLARE
col_count integer;
BEGIN
SELECT count(*)
into col_count
FROM user_tab_columns
WHERE table_name = 'populis.painel'
AND column_name = 'atrAtivoInativo';
IF col_count = 0
THEN
EXECUTE IMMEDIATE 'ALTER TABLE POPULIS.PAINEL add atrAtivoInativo char(1) default 'A' not null';
END IF;
END;
Relatório de erros -
ORA-06550: linha 12, coluna 88:
PLS-00103: Encontrado o símbolo "A" quando um dos seguintes símbolos era esperado:
* & = - + ; < / > at in é mod lembrete not rem retornar
retornando <um expoente (**)> <> ou != ou ~= >= <= <> e ou
como like2 like4 likec entre into usando || multiset bulk
membro submultiset
O símbolo "* foi inserido antes de "A" para continuar.
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
I hadn’t really noticed that, and now this command worked. But now I have another problem that is the fact that this whole code was to include a column in the table and if by chance it runs again does not try to include this column again in the table; but if I run again it returns error that the column already exists, needed some way that he wouldn’t get into the if. And just to explain to you when I put default 'A' not null was intended to insert a column that does not accept nulls and by default has the character To. Thank you
– Guilherme Garcia Alves
@Maybe the user of the bank you are using is not the owner of this table. If you run the command SELECT * FROM user_tab_columns WHERE table_name = '<tableName>' is any result returned? If no results return you can use the ALL_TAB_COLS in place of USER_TAB_COLUMNS or change this table so that its owner is the logged in user. It depends on the situation but I think the first option is more viable.
– Flavio Andrade
Running this command it returns the empty fields, now I don’t know much to do.
– Guilherme Garcia Alves
Sorry @Guilhermegarciaalves, did not understand, the consultation SELECT * FROM ALL_TAB_COLS Where table_name = '<table name>'; returns no results?
– Flavio Andrade
The two return the columns but empty, both the SELECT * FROM ALL_TAB_COLS Where table_name = '<table name>'; how much SELECT * FROM user_tab_columns Where table_name = '<table name>';
– Guilherme Garcia Alves
So I don’t know how to help you anymore, what it looks like is that you’re not allowed to see these tables, but the weird thing is that you can do the alter table, so I don’t know what could be going on. I saw now your explanation of default, you are right, I used few times the default so I did not remember it. Thanks for the clarification!
– Flavio Andrade