I have created below Oracle Procedure where i am granting DML priviledges on all tables to TST user. Now i also want to grant Truncate priviledge to TST user in the same Procedure but dont know how to do it.
CREATE OR REPLACE PROCEDURE "TBL_MER"."PROCEDURE_GRANT_PRIV"
IS
BEGIN
FOR tab IN (SELECT table_name
FROM all_tables
where owner = USER
order by table_name) LOOP
EXECUTE IMMEDIATE 'GRANT SELECT, INSERT, UPDATE, DELETE ON '||tab.table_name||' TO TST';
END LOOP;
COMMIT;
END;
Did you read the documentation on the TRUNCATE command?
Quoting from the 19c SQL Language Reference
Prerequisites
To truncate a table, the table must be in your schema or you must have
the DROP ANY TABLE system privilege.
TRUNCATE is not DML.
Related
SYSTEM creates several tables, and would like to grant userA the ability to truncate tables. On oracle's docs, the minimum privilege is :
GRANT DROP ANY TABLE TO userA
https://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_10007.htm#SQLRF01707
DROP ANY TABLE as stated is too powerful of a privilege to be granted to a user, and from looking around there is no way to limit the tables the user can truncate with it.
The proper way as previous topics has touched on is to use stored procedures:
CREATE OR REPLACE procedure truncateTables
AS
BEGIN
execute immediate 'TRUNCATE TABLE table1';
end;
/
GRANT EXECUTE on system.truncateTables TO userA;
However, if I want to avoid using any stored procedures at all, is there an alternative way to allow a user that is not the table owner to truncate tables, but not with a privilege that is potentially destructive like "DROP ANY TABLE" ?
You cant do it without procedure.
you can do it dynamically like this:
CREATE OR REPLACE procedure pr_truncate_table(p_table_name varchar2) is
begin
execute immediate 'truncate table ' || p_table_name || '';
end;
/
A work-around that I have used is to grant delete to the other user, then use a delete instead of truncate to remove the rows. This is NOT a proper truncate as it does not clear the used storage, but it allows the same tasks to be achieved.
With the table owner account
GRANT delete on Table_Name to "User_Account";
With the other account
Delete from Table_Name where 1=1;
I have a Scenario where I need to write a PLSQL Query to Grant permission to multiple table at once for Particular User Only for example : User_A
tbl_1
tbl_2
tbl_3
tbl_4
tbl_5
tbl_6
tbl_7
tbl_8
The above table as Read Acess only. Now I want to Provide write access , delete access to it
How do I achieve this Scenario?
You can use such a cursor with execute immediate command to grant the desired privileges to User_A for all tables of your current schema(a schema other than User_A) :
SQL> set serveroutput on
SQL> begin
for c in
(select 'grant insert, update, delete on '||t.table_name||' to User_A' as cmd
from user_tables t
order by t.table_name)
loop
dbms_output.put_line(c.cmd);
execute immediate c.cmd;
end loop;
end;
To literally answer your question, and run multiple grants at once, you need to use the CREATE SCHEMA command.
The syntax is a bit tricky because you have to hard-code the name of the user running the statement. And if one of the grants fails, they all fail, which may or may not be a good thing. But compared to a PL/SQL script, or multiple SQL statements, this approach uses less code and is a bit faster.
create schema authorization USER_RUNNING_THE_QUERY
grant select, insert, update, delete on tbl_1 to test_user1
grant select, insert, update, delete on tbl_2 to test_user1
grant select, insert, update, delete on tbl_3 to test_user1
grant select, insert, update, delete on tbl_4 to test_user1
grant select, insert, update, delete on tbl_5 to test_user1
grant select, insert, update, delete on tbl_6 to test_user1
grant select, insert, update, delete on tbl_7 to test_user1
grant select, insert, update, delete on tbl_8 to test_user1;
I have a request to grant user TOM Insert/Update access to all the tables on a schema JERRY.
I use the following query achieve it.
FOR x IN (SELECT * FROM user_tables)
LOOP
EXECUTE IMMEDIATE 'GRANT SELECT ON ' || x.table_name || ' TO <<someone>>';
END LOOP;
After granting the rights on the existing tables, i have to create few more tables on JERRY schema. Will TOM have insert/update access to those newly created tables?
If NO do i need to execute the above statement every time i create a new table?
or is there a way to grant access to the whole schema?
Hi I have this question.
Is posibble GRANT ANY privileges excluding some tables of the same schema.
For Example:
EXECUTE IMMEDIATE
'CREATE USER USREJECUTA_SUI_ABAS
IDENTIFIED BY VALUES ''test''
DEFAULT TABLESPACE USERS
TEMPORARY TABLESPACE TEMP
PROFILE DEFAULT
ACCOUNT UNLOCK';
-- 2 Roles for USREJECUTA_SUI
EXECUTE IMMEDIATE 'GRANT CONNECT TO USREJECUTA_SUI_ABAS';
EXECUTE IMMEDIATE 'GRANT RESOURCE TO USREJECUTA_SUI_ABAS';
EXECUTE IMMEDIATE 'ALTER USER USREJECUTA_SUI_ABAS DEFAULT ROLE ALL';
-- 1 System Privileges for USREJECUTA_SUI
EXECUTE IMMEDIATE 'GRANT SELECT ANY TABLE TO USREJECUTA_SUI_ABAS';
EXECUTE IMMEDIATE 'GRANT UPDATE ANY TABLE TO USREJECUTA_SUI_ABAS';
EXECUTE IMMEDIATE 'GRANT INSERT ANY TABLE TO USREJECUTA_SUI_ABAS';
EXECUTE IMMEDIATE 'GRANT UNLIMITED TABLESPACE TO USREJECUTA_SUI_ABAS';
Now revoke the permissions from some tables
REVOKE INSERT,UPDATE ON VELITTDA.TAPROVEEDORESXPAIS FROM USREJECUTA_SUI';
but the system launch this error:
ORA-01927: cannot REVOKE privileges you did not grant.
Do you kow some wat to do this? I will apreciate a lot your help.
No, you can't. If you give someone SELECT ANY TABLE, you've given them the ability to query any table in the database. You can't revoke object-level privileges when you haven't granted object-level privileges.
The proper approach is almost always to create a role that has object-level privileges on the actual set of objects that the user needs access to. Grant the role to the user (and any other users that need a similar set of privileges). The various ANY roles are really only appropriate for folks like DBAs.
Granting the SELECT ANY TABLE (or any other ANY TABLE) privilege is generally the wrong thing to do and is almost as bad as granting DBA to arbitrary schema users.
If you are trying to avoid issuing a lot of grant statements, then use the simple trick of generating your DDL from the data dictionary.
set head off
set pagesize 0
spool grant_foo.sql
select 'GRANT SELECT ON '||table_name||' TO FOO_ROLE;'
from all_tables where owner = 'FOO'
order by table_name
;
spool off
Then edit grant_foo.sql as needed before executing.
I have 3 users in my oracle Database
user_admin
user
user_query
The user_admin have dba rol.
user have connect and resource rol
and user_query the same as user.
I want to configure the last one to deny him of all kind of insert,update,delete but form the full tablespace not from a table, view or procedure...
The point is to have a secure-only-querying data user.
But i can't do it for a tablespace, at least as far i know.
Any idea?
You can loop through all table that use the tablespace in question and grant select. I would try to stay away from powerful privs like "SELECT ANY TABLE", as that would apply to the entire database.
For example, if your tablespace is named XXX then:
BEGIN
FOR tbl IN (SELECT owner, table_name
FROM dba_tables dt
WHERE dt.tablespace_name = 'XXX') LOOP
EXECUTE IMMEDIATE 'GRANT SELECT ON ' || tbl.owner || '.' || tbl.table_name || ' TO USER_QUERY';
END LOOP;
END;
there is the following which grants select to any table or view (except those owned by SYS):
grant select any table to user_query;
It doesn't restrict to a single tablespace though - any table in the entire database would be available for select.
Firstly, the use of the CONNECT and RESOURCE roles is discouraged, and documented as such.
That aside, no, there is no privilege for granting by tablespace, or even by user. For one thing, a partitioned table or index can use multiple tablespaces, none of which might be the default for that object.
Grants are at the object level. You could create a procedure to grant privileges to a user (or better to a role) based on the tablespace of a table though.