Create user using a Function

Asked

Viewed 84 times

0

someone could help me, she keeps giving error when saved '-'

Error(19,5): PLS-00103: Encountered the Symbol "CREATE" when expecting one of the following: ( start case declare quit for goto if loop mod null pragma raise return select update while with << continue closing current delete fetch lock Insert open rollback savepoint set sql execute commit forall intercalar pipe expurgar

create or replace
FUNCTION   FC_CRIA_USUARIO_SADP(USERN IN VARCHAR2)
RETURN VARCHAR2 
iS
RES VARCHAR2;
bd varchar2;

BEGIN
MSG:='NULL';

  select USERNAME INTO RES FROM DBA_USERS 
  WHERE USERNNAME = USERN; 
  SELECT name into bd FROM V$DATABASE;


  IF RES IS NULL THEN   


  CREATE USER USERN IDENTIFIED BY 12345678;
  GRANT CREATE SESSION TO USERN;
  GRANT RL_SADP_USUARIO TO USERN;
  GRANT RL_ACESSO_CONSULTA TO USERN;

  MSG:= 'Usuário ' || USERN ||' criado no banco' ||bd;

RETURN MSG;

  ELSE
  MSG:= 'Usuário ' || USERN ||' já existe no banco' ||bd;


END IF;
END FC_CRIA_USUARIO_SADP;
  • 1

    I think that DDL cannot be done in PLSQL , I believe the solution is to run via execute immediate vide Sample DDL Operation Using Native Dynamic SQL in https://docs.oracle.com/cd/B10501_01/appdev.920/a96590/adg09dyn.htm

1 answer

1

Change to the one described below:

execute_immediate('CREATE USER USERN IDENTIFIED BY 12345678');
execute_immediate('GRANT CREATE SESSION TO USERN');
execute_immediate('GRANT RL_SADP_USUARIO TO USERN');
execute_immediate('GRANT RL_ACESSO_CONSULTA TO USERN');

I just can’t remember if ";" is required at the end of each run. It is also necessary for the logged in user to have the rights to create users.

Note: It might be better and safer instead of creating a user in the bank, creating a user table with passwords and encryption, and letting the application do the authentication, if the focus is to insert users for use in systems, because putting in the bank, these users can modify the bank without needing a system, which is potentially dangerous

Browser other questions tagged

You are not signed in. Login or sign up in order to post.