0

I am trying to create oracle users and grant connection using a loop. This is what I have so far.

I get an error Encountered the symbol "USERID". I assume I need to escape the userid somehow.

BEGIN
  FOR USERNAME IN (SELECT USERID FROM SCHEMA.TABLENAME WHERE USERID NOT IN(SELECT NAME FROM USER$))
  LOOP
    EXECUTE IMMEDIATE 'CREATE USER ' || USERNAME || ' IDENTIFIED BY ' USERNAME;
    EXECUTE IMMEDIATE 'GRANT CONNECT TO ' || USERNAME;                                  
  END LOOP;
END;

Any help to fix the statement would be appreciated.

Edit: Change cursor name for clarity.

2 Answers 2

2

You appear to be missing the string concatenation operator || after the "IDENTIFIED BY" string and before the second USERID.

EXECUTE IMMEDIATE 'CREATE USER ' || USERID || ' IDENTIFIED BY ' || USERID;

You also appear to want to refer to the USERID column in the USERNAME cursor. Assuming that is the case, you'd need to use the syntax USERNAME.USERID

EXECUTE IMMEDIATE 'CREATE USER ' || USERNAME.USERID || ' IDENTIFIED BY ' || USERNAME.USERID;
EXECUTE IMMEDIATE 'GRANT CONNECT TO ' || USERNAME.USERID;   
4
  • That fixed the error. Now I get this error PLS-00306: wrong number or types of arguments in call to '||'. Any insights?
    – theking963
    Apr 10, 2013 at 18:41
  • @theking963 - Updated. I didn't notice that you were trying to use the USERID column from the USERID cursor. Apr 10, 2013 at 18:45
  • Poor choice or variable name...Thanks. I will accept it in a few mins when it will allow me to.
    – theking963
    Apr 10, 2013 at 18:48
  • Regardless of the variable name i needed varname.userid. For clarity's sake for other people I am going to change the cursor name in the question. Please modify the answer variable name. And I will accept.
    – theking963
    Apr 10, 2013 at 18:58
0

you can not create user with the numeric value like 1,2,3,4.... in the loop.

if you want to can concate with the string value like abc1,abc2,abc3. abc is default value and 1,2,3 count in a loop..

declare
user varchar(10);
id number(10);
pwd varchar(10);

begin
for id in 1..3
loop
user:='abc';
pswd:='abcd';--password
user=user||id;--concate user+numeric value here.
execute immediate'create user'||user||'identifid by '||pswd;
execute immediate'grant connect to'||user;
execute immediate'grant resource to'||user;
end loop;
end;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.