I have a user A who owns several tables. Now, I want to grant select, update, delete, insert on all of user A's tables to user B. For this, I have written the following in grant.sql which I run after logging in as user A:
cursor c_tables is select table_name from user_tables;
fetch c_tables into v_tables;
exit when c_tables%notfound;
grant select, insert, update, delete on v_tables.table_name to B;
But this does not work. It says, "grant not allowed". How can I correct this, or is there a better way to do it ?
It's because grant is data control language(not allowed in procedures) and not DML.
You have to use EXECUTE IMMEDIATE for 8 version or
use dbms_sql package for version bellow 8:
here a procedure for delete a oracle user (data definition language)
PROCEDURE DeleteUser(user_id IN NUMBER,
errorcode OUT NUMBER)
SELECT login_id INTO ausername FROM broker_codes
WHERE brc_id = user_id;
SET login_id = ''
Where brc_id = user_id;
v_CreateCommand := 'DROP USER ' || ausername ;
v_CursorID := DBMS_SQL.OPEN_CURSOR;
DBMS_SQL.PARSE(v_CursorID, v_CreateCommand, DBMS_SQL.NATIVE);
v_dummy := DBMS_SQL.EXECUTE(v_CursorID);
WHEN OTHERS THEN