[英]In Oracle, Can you create a new table with the same groups if you use CTAS Query?
我使用查詢 CTAS 創建一個新表,但是,當 CTAS 完成后,其他用戶不能 select 新表,但他們可以訪問舊表,這是一種將所有用戶和組傳遞給新表的方法桌子? 因為舊表將被刪除。
“一種方法”是向所有這些用戶授予(至少) select
特權。
如果您使用一個角色並將select
權限授予該角色,然后將角色授予這些用戶,事情會變得非常簡單 - 只需將新表上的select
權限授予相同的角色,每個人都會“看到”它。
否則,您可以編寫查詢來為您創建這些grant
語句。
例如,在 Scott 的模式中有EMP
表。 我之前已將權限授予我數據庫中的其他用戶,現在我將創建一個“新”CTAS 表並將權限授予同一組用戶。
SQL> create table my_new_table as select * from emp;
Table created.
SQL> select 'grant select on my_new_table to ' || grantee ||';' run_me
2 from all_tab_privs_made
3 where owner = 'SCOTT'
4 and table_name = 'EMP';
RUN_ME
---------------------------------------------------------------
grant select on my_new_table to SYS;
grant select on my_new_table to SUPERUSER;
grant select on my_new_table to MY_ROLE;
grant select on my_new_table to MIKE;
現在只需復制/粘貼上面的一堆grant
語句:
SQL> grant select on my_new_table to SYS;
Grant succeeded.
SQL> grant select on my_new_table to SUPERUSER;
Grant succeeded.
SQL> grant select on my_new_table to MY_ROLE;
Grant succeeded.
SQL> grant select on my_new_table to MIKE;
Grant succeeded.
SQL>
如果有無數用戶,PL/SQL 選項會更簡單,因為它會為你做所有事情(即沒有復制/粘貼):
SQL> begin
2 for cur_r in (select grantee
3 from all_tab_privs_made
4 where owner = 'SCOTT'
5 and table_name = 'EMP'
6 )
7 loop
8 execute immediate 'grant select on my_new_table to ' || cur_r.grantee;
9 end loop;
10 end;
11 /
PL/SQL procedure successfully completed.
SQL>
如果您使用 CTAS 從現有表創建表,則新表是一個新段,因此它缺乏特權。 您需要恢復授予舊表的權限並授予新表。 為此,您可以使用幾種替代方法( dbms_metadata、動態 sql )。
出於目的,我會這樣做
SQL> CREATE TABLE T2 AS SELECT * FROM T1 ;
SQL> begin
dbms_metadata.set_transform_param (dbms_metadata.session_transform,
'SQLTERMINATOR', true);
dbms_metadata.set_transform_param (dbms_metadata.session_transform, 'PRETTY',
true);
end;
/
select replace(dbms_metadata.get_dependent_ddl('OBJECT_GRANT', 'T1', 'OWNER_OF_T1' ),'T1','T2') AS ddl
from dual;
第一部分是用於以良好的格式創建必要的授權列表。 第二部分檢索授予 T1 的所有權限,並生成授予語句列表以運行到 T2 表。 然后你只需要運行授權列表
正如我所說,有幾種方法可以做到這一點。
問候
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.