0

I try to execute this but it gives an error at the default tablespace. I want to give a default tablespace to the users I create but how?

BEGIN
  FOR USERNAME IN (SELECT studentname from students)
  LOOP
    EXECUTE IMMEDIATE 'CREATE USER ' || USERNAME.studentname  || ' IDENTIFIED BY ' || USERNAME.studentname;
    EXECUTE IMMEDIATE 'DEFAULT TABLESPACE "USERS"'; 
    EXECUTE IMMEDIATE 'TEMPORARY TABLESPACE "TEMP"';
    EXECUTE IMMEDIATE 'GRANT STUDENT TO ' || USERNAME.studentname ;                                    
  END LOOP;
END;


Error report - ORA-00900: invalid SQL statement ORA-06512: at line 5 00900. 00000 - "invalid SQL 
5
  • Which error exactly? Commented Nov 26, 2016 at 10:38
  • 1
    Specifying the error is useful. Your first three statements look like they should all be part of a single statement. You'd want a single EXECUTE IMMEDIATE that did the whole CREATE USER including the default and temporary tablespace. Of course, you'd want to ensure that you have appropriate whitespace in your DDL statement (spaces before DEFAULT and TEMPORARY for example). Commented Nov 26, 2016 at 10:54
  • 1
    The first three lines should be a single statement. There is no valid SQL statement default tablespace Commented Nov 26, 2016 at 10:58
  • Error report - ORA-00900: invalid SQL statement ORA-06512: at line 5 00900. 00000 - "invalid SQL statement" *Cause: *Action: Commented Nov 26, 2016 at 12:16
  • When I make one statement of the first three, I get the following error Error report - ORA-00922: missing or invalid option ORA-06512: at line 4 00922. 00000 - "missing or invalid option" *Cause: *Action: Commented Nov 26, 2016 at 12:17

1 Answer 1

1

You need to combine the first three statements into one and add in appropriate spaces. You don't need the double-quotes around the tablespace names since you're using case-insensitive identifiers.

BEGIN
  FOR USERNAME IN (SELECT studentname from students)
  LOOP
    EXECUTE IMMEDIATE 'CREATE USER ' || USERNAME.studentname  || ' IDENTIFIED BY ' || USERNAME.studentname ||
                      '  DEFAULT TABLESPACE USERS  
                        TEMPORARY TABLESPACE TEMP';
    EXECUTE IMMEDIATE 'GRANT UNLIMITED TABLESPACE TO ' || USERNAME.studentname;
    EXECUTE IMMEDIATE 'GRANT STUDENT TO ' || USERNAME.studentname ;                                    
  END LOOP;
END;

Personally, I'd always generate a string with the SQL statement and pass that string to EXECUTE IMMEDIATE. That makes it easy to do things like log the SQL statements that are executed or to log which statement failed. That's going to make debugging far easier.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.