Home > Oracle Tips > Oracle Database Administrator > Procedure to create synonyms
Oracle Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

ORACLE DATABASE ADMINISTRATOR

Procedure to create synonyms


Jaime Marin
11.20.2002
Rating: -3.60- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


This tip will allow you to create synonyms and grant privileges for a user or a user's group, on one object or all objects from a specific user. You can run this procedure from your system account without connecting to each user. It has been tested on Oracle v8.1.7.

--    N  O  T  E :    The user of these
--    utilities must have the following
--    system privs. EXPLICITLY granted
--
--
-- ANALYZE ANY
-- CREATE ANY SYNONYM
-- CREATE ANY PROCEDURE
-- DROP ANY PROCEDURE
-- EXECUTE ANY PROCEDURE
-- SELECT ANY TABLE
  create or replace procedure synonyms     -- Create synonyms for db objects
  (
     p_action  IN varchar2,                        -- C-Create or D-Drop
     p_owner   IN varchar2,                        -- Object's owner
     p_grantee IN varchar2,                        -- New synonyms's owner
     p_type    IN varchar2 default 'A',            -- T=table; V=view;
                                                   -- S=sequence; F=function;
                                                   -- P=procedure; K=package;
                                                   -- A=all objects
     p_object  IN varchar2 default '%'             -- specific object name
  )  is

     v_sql        varchar2(1000) := '';
     v_action     varchar2(10);
     v_role       char(1)        := 'N';
     v_exi_gran   number(6);

     e_exists_exception EXCEPTION;
     PRAGMA EXCEPTION_INIT(e_exists_exception, -955);    --  object name in
                                                         --  use exception
     e_not_exists_exception EXCEPTION;
     PRAGMA EXCEPTION_INIT(e_not_exists_exception, -1434);  -- object name
                                                            -- in use exception
     e_role_especific EXCEPTION;
     PRAGMA EXCEPTION_INIT(e_role_especific, -20335);    -- p_grantee is a role

  Begin
     begin
       select 'Y'               --  ask if p_grantee is a role
         into v_role
         from dba_roles
        where role = upper(p_grantee);
       begin
         for usr in (select grantee
                       from dba_role_privs
                      where granted_role = upper(p_grantee)
                        and grantee     != upper(p_owner)) loop
--  build the synonyms, acum role granted
            sinonimos(p_action,p_owner,usr.grantee,p_type,p_object); 
         end loop;
       end;
       raise e_role_especific;
     exception
       when no_data_found then
          v_role := 'N';
     end;

     if upper(p_action) = 'C' then
       v_action := 'create';
     elsif upper(p_action) = 'D' then
       v_action := 'drop';
     else
       v_action := 'create';
     end if;

--  obtain the objects which satisfy the parameter list
     for obj in(select *                                 
                from   dba_objects 
                where  owner = upper(p_owner) 
                and    object_name like upper(p_object) 
                and    object_type like decode(upper(p_type),'T','TABLE','V','VIEW','S','SEQUENCE','F','FUNCTION',
                                                             'P','PROCEDURE','K','PACKAGE','A','%')
                and    object_type in ('TABLE','VIEW','SEQUENCE','FUNCTION','PROCEDURE','PACKAGE')) loop

--  build dynamic synonym string
        v_sql := v_action||' synonym ' ||p_grantee||'.' ||obj.object_name; 

        if v_action = 'create' then

--  build dynamic synonym string
          v_sql := v_sql|| ' for '||p_owner|| '.'||obj.object_name;        
        end if;

        begin
           execute immediate v_sql;                     --  create the synonym
           dbms_output.enable(100000);
           dbms_output.put_line(v_sql);
        exception
           when e_exists_exception then         -- don't stop just ignore dups
              dbms_output.enable(100000);
              dbms_output.put_line('Synonym '||p_grantee||'.'||obj.object_name||' exists - skipping');
           when e_not_exists_exception then      -- don't stop just ignore dups
              dbms_output.enable(100000);
              dbms_output.put_line('Synonym '||p_grantee||'.'||obj.object_name||' not exists - skipping');
           when OTHERS then
              raise_application_error(-20322,'OTHERS raised for exec immediate: '||sqlerrm);
        end;

     end loop;

  Exception
     when e_role_especific then
        null; -- pass the exception up
     when OTHERS then
        raise_application_error(-20456,'OTHERS error occured in SYNONYMS: '||sqlerrm);

  End synonyms;
/

For More Information

  • Feedback: E-mail the editor with your thoughts about this tip.
  • More tips: Hundreds of free Oracle tips and scripts.
  • Tip contest: Have an Oracle tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize -- submit your tip today!
  • Ask the Experts: Our SQL, database design, Oracle, SQL Server, DB2, metadata, and data warehousing gurus are waiting to answer your toughest questions.
  • Forums: Ask your technical Oracle questions--or help out your peers by answering them--in our active forums.
  • Best Web Links: Oracle tips, tutorials, and scripts from around the Web.

Rate this Tip
To rate tips, you must be a member of SearchOracle.com.
Register now to start rating these tips. Log in if you are already a member.


Submit a Tip




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
Oracle Database Administrator
What is the difference between a database engineer, architect and administrator?
Import on one table from dump file
Error during RMAN backup
Can I drop a column in SYS schema?
STATSPACK tool: transaction vs. execution measurement
Should I port from Microsoft Access?
Installing multiple Oracle homes
How can I find statistics on total memory usage and database connections?
Modifying SYS password in a RAC environment
How to create Datafiles in a Data Guard (10g) environment

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.

HomeNewsTopicsTipsAsk the ExpertsWebcastsWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2003 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts