{"id":14,"date":"2011-10-24T20:23:00","date_gmt":"2011-10-24T20:23:00","guid":{"rendered":"https:\/\/bijoos.com\/oraclenotes\/?p=14"},"modified":"2026-02-07T15:29:24","modified_gmt":"2026-02-07T21:29:24","slug":"script-to-grant-privileges-and-create-synonyms","status":"publish","type":"post","link":"https:\/\/bijoos.com\/oraclenotes\/2011\/14\/","title":{"rendered":"Script to Grant Privileges and Create Synonyms"},"content":{"rendered":"<div dir=\"ltr\" trbidi=\"on\">Extending the previous post\u00a0<span><span><a href=\"http:\/\/bt-oracle.blogspot.com\/2011\/10\/granting-privileges-on-all-or-multiple.html\">Granting privileges on all or multiple objects to user\/role<\/a><\/span><\/span>\u00a0, here is another script.<\/p>\n<p>Often we have a requirement to create read only accounts for a schema. Typically these accounts are named <schema>_QUERY. The script when executed will prompt for the user name of the read only account, and the schema name where read privileges on the objects to be granted. It produces output to the screen and writes to file named tmpgrants.sql. Execute the tmpgrants.sql to grant SELECT privilege and to create a synonym under the read only account, so that the query account need not worry about qualifying the table with schema name.<\/schema><\/p>\n<p>First create the read only user account using similar syntax as below&#8230;<\/p>\n<p><span>create user tdmaa_query identified by tdm123q default tablespace users;<\/span><br \/><span>grant create session to tdmaa_query;<\/span><\/p>\n<p>Be sure to save all the below lines to a file and run the file in SQL*Plus&#8230; Executing this script will produce a script file named tmpgrants.sql. Run that script to grant privileges and create synonyms.<\/p>\n<p><span>set pages 0 lines 200 trims on verify off feedback off<\/span><br \/><span><br \/><\/span><br \/><span>accept grants_to \u00a0prompt &#8216;Enter user to grant privileges: &#8216;<\/span><br \/><span>accept schema \u00a0 \u00a0 prompt &#8216;Enter schema on which to grant: &#8216;<\/span><br \/><span><br \/><\/span><br \/><span>spool tmpgrants.sql<\/span><br \/><span><br \/><\/span><br \/><span>select &#8216;grant select on &#8216;||owner||&#8217;.&#8217;||table_name ||&#8217; to &#038;grants_to;&#8217;, chr(10),<\/span><br \/><span>\u00a0 \u00a0 \u00a0 \u00a0&#8216;create synonym &#038;grants_to..&#8217;||table_name ||&#8217; for &#8216;||owner||&#8217;.&#8217;||table_name||&#8217;;&#8217;, chr(10)<\/span><br \/><span>from \u00a0dba_tables<\/span><br \/><span>where owner = upper(&#8216;&#038;schema&#8217;)<\/span><br \/><span>union all<\/span><br \/><span>select &#8216;grant select on &#8216;||owner||&#8217;.&#8217;||view_name ||&#8217; to &#038;grants_to;&#8217;, chr(10),<\/span><br \/><span>\u00a0 \u00a0 \u00a0 \u00a0&#8216;create synonym &#038;grants_to..&#8217;||view_name ||&#8217; for &#8216;||owner||&#8217;.&#8217;||view_name||&#8217;;&#8217;, chr(10)<\/span><br \/><span>from \u00a0dba_views<\/span><br \/><span>where owner = upper(&#8216;&#038;schema&#8217;)<\/span><br \/><span>;<\/span><br \/><span><br \/><\/span><br \/><span>spool off<\/span><br \/><span><br \/><\/span><br \/><span>set pages 99 lines 80 verify on feedback on<\/span><br \/><span><br \/><\/span><br \/><span>prompt &#8220;Run tmpgrants.sql if you are satistified with the script&#8230;&#8221;<\/span><\/p>\n<p>Enjoy!<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Extending the previous post\u00a0Granting privileges on all or multiple objects to user\/role\u00a0, here is another script. Often we have a requirement to create read only accounts for a schema. Typically these accounts are named _QUERY. The script when executed will prompt for the user name of the read only account, and the schema name where &#8230; <a title=\"Script to Grant Privileges and Create Synonyms\" class=\"read-more\" href=\"https:\/\/bijoos.com\/oraclenotes\/2011\/14\/\" aria-label=\"Read more about Script to Grant Privileges and Create Synonyms\">Read more<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[133],"tags":[],"class_list":["post-14","post","type-post","status-publish","format-standard","hentry","category-oracledb"],"acf":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/bijoos.com\/oraclenotes\/wp-json\/wp\/v2\/posts\/14","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bijoos.com\/oraclenotes\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bijoos.com\/oraclenotes\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bijoos.com\/oraclenotes\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bijoos.com\/oraclenotes\/wp-json\/wp\/v2\/comments?post=14"}],"version-history":[{"count":1,"href":"https:\/\/bijoos.com\/oraclenotes\/wp-json\/wp\/v2\/posts\/14\/revisions"}],"predecessor-version":[{"id":3002,"href":"https:\/\/bijoos.com\/oraclenotes\/wp-json\/wp\/v2\/posts\/14\/revisions\/3002"}],"wp:attachment":[{"href":"https:\/\/bijoos.com\/oraclenotes\/wp-json\/wp\/v2\/media?parent=14"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bijoos.com\/oraclenotes\/wp-json\/wp\/v2\/categories?post=14"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bijoos.com\/oraclenotes\/wp-json\/wp\/v2\/tags?post=14"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}