Solved

Let Oracle Trigger do the work

Posted on 2014-10-08
2
417 Views
Last Modified: 2014-10-27
I need to create a trigger to create the synonyms when any new table is created.

Currently; this is a manual task as below:-
Create created (say Test). Create table test a(number);

After that the manual tasks as below:-
create public synonym test for test;
grant select,insert,delete on test to emp_role;

I need help in automate the process using trigger. ( The trigger should create the synonym for the new objects and also grant privileges to roles).
0
Comment
Question by:Oranew
2 Comments
 
LVL 16

Accepted Solution

by:
Wasim Akram Shaik earned 500 total points
ID: 40368173
You directly cannot use grants in triggers, you may have to use Pragma and call it via a procedure..

first create the procedure and afterwards create the trigger

Try something like this, this is an extract from the oracle forums website, https://community.oracle.com/thread/402826

create or replace TRIGGER TRIG_CREATE_TABLE
AFTER CREATE ON SCHEMA
begin
  if ora_dict_obj_type='TABLE' then
    grant_privs (ora_dict_obj_owner, ora_dict_obj_name);
  end if;
end;

create or replace procedure grant_privs (p_owner in varchar2, p_tablename in varchar2) authid current_user
is
   pragma autonomous_transaction;
begin
   execute immediate 'grant select, insert, delete, update on ' || p_owner ||'.'|| p_tablename ||' to role_crud_mtd';
execute immediate 'create public synonym '||p_tablename||' for '||p_table_name;

   commit;
end grant_privs;
0
 

Author Closing Comment

by:Oranew
ID: 40407707
Thank you so much.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question