Thursday, September 18, 2008

Using PL/SQL to Grant Permissions to Role(s) or Person(s)

When you have a large DB and it goes through an upgrade and _ALL_ the views have to be rebuild, that means that any/all permissions / Grants to those views are gone! (*sigh*) To quickly replace the permissions here is a little PL/SQL annonymous block to pull the view names, and grant permissions. Obviously this could be a lot intellegent and flexable, but here is the idea.

DECLARE
CURSOR myviews
IS
SELECT view_name
FROM SYS.user_views
WHERE view_name LIKE '%A_Z%';
BEGIN
FOR x IN myviews
LOOP
BEGIN
EXECUTE IMMEDIATE 'GRANT SELECT ON ' x.view_name ' TO COOL_PROGRAMMER';
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line ( 'GRANT SELECT ON SYSADM.'
x.view_name
' TO COOL_PROGRAMMER; '
SQLERRM
);
END;
END LOOP;
END;

No comments: