-- ====================================================================
-- Additional Grants from MARS-770
-- ====================================================================
-- Purpose: Additional grants and dynamic privilege assignments
-- Source: MARS-770 install_package_MarsGeneralRelease_MARS770_grants.sql
-- ====================================================================

-- Grant SELECT ANY TABLE to specific schemas
GRANT SELECT ANY TABLE TO OU_RQSD;
GRANT SELECT ANY TABLE TO RQSD;

-- Dynamic grants to all non-Oracle schemas for A_WORKFLOW_HISTORY access
BEGIN
    FOR user_rec IN (SELECT username FROM dba_users WHERE oracle_maintained != 'Y' AND common != 'YES') LOOP
        DECLARE
            schema_name VARCHAR2(128) := user_rec.username;
        BEGIN
            EXECUTE IMMEDIATE 'GRANT SELECT ON CT_MRDS.A_WORKFLOW_HISTORY TO "'|| schema_name ||'" WITH GRANT OPTION';
        EXCEPTION
            WHEN OTHERS THEN
                DBMS_OUTPUT.PUT_LINE('Error processing schema ' || schema_name || ': ' || SQLERRM);
        END;
    END LOOP;
END;
/