Showing posts with label Scripts. Show all posts
Showing posts with label Scripts. Show all posts

Saturday, 25 April 2015

Purform Bulk Deletes on Tables with Commit for Every N number of Rows and update row count in Temp Table

1) Create Temporary Table for Logging the activity

create table snanabala.log_table (SEQ_NUM number(10), Time DATE , rows_deleted number(38));


2) Create Sequence for row sequence


create sequence snanabala.delete_sequence;

3) Now give grants on this table and sequence since we are using procedure to delete on table and logging in temp table

grant select,delete on snanabala.Sunil_archive to snanabala;  ( deletions on this table )
grant all on snanabala.log_table to snanabala;
grant all on snanabala.delete_sequence to snanabala;


4)   Now we need to perform deletions on sunil_archive Table .


CREATE OR replace PROCEDURE snanabala.Delete_rows_TEST1
AS

    ln_DelSize                      NUMBER := 1000;
    ln_DelCount                     NUMBER;

  BEGIN
    LOOP
           DELETE FROM SNANABALA.Sunil_ARCHIVE
             WHERE  
                  STARTDATETIME between to_date('10-01-2014 00:00:00','MM-DD-YYYY HH24:MI:SS') and to_date('10-01-2014 04:59:59','MM-DD-YYYY HH24:MI:SS')
              AND
                    rownum <= ln_DelSize;
ln_DelCount := SQL%ROWCOUNT;
                           dbms_output.put_line(ln_DelCount);
  INSERT INTO LOG_TABLE (SEQ_NUM, TIME, "ROWS_DELETED")  VALUES (delete_sequence.nextval,sysdate,ln_DelCount);
            EXIT WHEN ln_DelCount = 0;
            COMMIT;

   END LOOP;

END;
 /


5) Execute the above procedure

exec snanabala.Delete_rows_TEST1


6) Monitor how many deletions performing

select * from snanabala.log_table order by date desc



Purform Bulk Deletes on Tables with Commit for Every N number of Rows

-- Delete Row from Table Sunil_temp and Commit Every 10000 rows.


CREATE OR replace PROCEDURE Delete_rows_SUNIL
AS

    ln_DelSize                      NUMBER := 10000;
    ln_DelCount                     NUMBER;

  BEGIN
    LOOP
           DELETE FROM SUNIL_TEMP
             WHERE
                  STARTDATETIME between to_date('04-01-2015 00:00:00','MM-DD-YYYY HH24:MI:SS') and to_date('04-25-2015 23:59:59','MM-DD-YYYY HH24:MI:SS')
              AND
                    rownum <= ln_DelSize;

ln_DelCount := SQL%ROWCOUNT;

             dbms_output.put_line(ln_DelCount);

            EXIT WHEN ln_DelCount = 0;
            COMMIT;

   END LOOP;

END;
 /

Procedure for Giving Grants for Objects Which are Missing Grants

--  Purpose: Loop through all tables in the schema(s) specified and check if the corresponding role has been granted select.
--  This Script Needs to run As SYS
--                 If the corresponding role is missing the select privilege this script will grant select to the role.
--      We need to Create Temp Table Which is having Two Columns Which is Schema name and                   Role Name .
--               
--           Schema - Role relationship is defined in a table named "schema_roles"
--    
--             CREATE TABLE SCHEMA_ROLES
--             (
--               "SCHEMA"  VARCHAR2(50 BYTE),
--               ROLE      VARCHAR2(50 BYTE)
--             )
--  

CREATE OR replace PROCEDURE Pr_fix_missing_grants
AS
  CURSOR C1 is select * from schema_roles;
  grant_rec C1%rowtype;
  
  TYPE REF_CURSOR IS REF CURSOR;
  C2                 REF_CURSOR;
  
  V_SQL              VARCHAR2(2000);
  V_SQL2             VARCHAR2(2000);
  V_OWNER            VARCHAR2(50);
  V_TABLE            VARCHAR2(50);
  
  V_TMP_CNT          NUMBER:=0;
  
begin

DBMS_OUTPUT.ENABLE(1000000); 

--Open cursor C1 to loop through list of schemas stored in schema_roles.
OPEN C1;
 LOOP
 FETCH C1 INTO grant_rec;
 EXIT WHEN C1%NOTFOUND;
 
   --Validate the user exists:
   select count(username) into V_TMP_CNT from dba_users where username = grant_rec.schema;
   if (V_TMP_CNT) > 0 then
   
     --Validate the role exists
     select count(role) into V_TMP_CNT from dba_roles where role = grant_rec.role;
     if (V_TMP_CNT) > 0 then
     
       --Build select statement to retrieve tables missing the SELECT privilege on the specified role.
       --dbms_output.put_line(grant_rec.schema ||' '||grant_rec.role);
       V_SQL := 'select owner, table_name from dba_tables where owner =''' || grant_rec.schema 
                 || ''' MINUS
                select owner, table_name from dba_tab_privs where privilege = ''SELECT'' and GRANTEE =''' || grant_rec.role || '''';
       --dbms_output.put_line(V_SQL);
       --Open cursor C2 to loop through tables that need to have select granted to the role.
       OPEN C2 FOR V_SQL;
       LOOP
         begin
         
           FETCH C2 into v_owner, v_table;
           EXIT WHEN C2%NOTFOUND; 
             V_SQL2 := 'grant select on '||V_OWNER ||'.'||V_TABLE||' to '||grant_rec.role;             
             --DBMS_OUTPUT.PUT_LINE(V_SQL2);
             execute immediate V_SQL2;
             DBMS_OUTPUT.PUT_LINE('Granted select on '||V_OWNER ||'.'||V_TABLE||' to '||grant_rec.role);
            
             --Exception handling in case something goes wrong making the grant.
exception 
             when others then
             DBMS_OUTPUT.PUT_LINE('ERROR PROCESSING ' || V_SQL2);
             dbms_output.put_line('!!!-ERROR-!!!' ||SQLERRM);
              
         end;
       END LOOP;
       CLOSE C2;
     ELSE     
       dbms_output.put_line('*****Role '||grant_rec.role||' does not exist!*****');
     END IF;     
   ELSE   
     dbms_output.put_line('*****Schema '||grant_rec.schema||' does not exist!*****');
   END IF;
 END LOOP;
END;
/

Give Grants For Objects Which are Missing Grants on Objects & Which are Created Last 24 hrs Create

CREATE OR replace PROCEDURE snanabala.Pr_fix_missing_grants
AS
  CURSOR cu_missing_grants_identity IS
    SELECT 'grant select on '
           || owner
           ||'.'
           || object_name
           || ' to IDENTITY_READ_ROLE' AS script
    FROM   dba_objects
    WHERE  object_type IN ( 'TABLE', 'VIEW', 'SEQUENCE' )
           AND owner IN ( 'IDENTITY' )
           AND Trunc(created) = Trunc(SYSDATE);
  CURSOR cu_missing_grants_siw IS
    SELECT 'grant select on '
           || owner
           ||'.'
           || object_name
           || ' to SIW_READ_ROLE' AS script
    FROM   dba_objects
    WHERE  object_type IN ( 'TABLE', 'VIEW', 'SEQUENCE' )
           AND owner IN ( 'SIW' )
           AND Trunc(created) = Trunc(SYSDATE);
  CURSOR cu_missing_grants_edw IS
    SELECT 'grant select on '
           || owner
           ||'.'
           || object_name
           || ' to EDW_READ_ROLE' AS script
    FROM   dba_objects
    WHERE  object_type IN ( 'TABLE', 'VIEW', 'SEQUENCE' )
           AND owner IN ( 'EDW_READ_ROLE' )
           AND Trunc(created) = Trunc(SYSDATE);
BEGIN
    FOR l_missing_gr_identity IN cu_missing_grants_identity LOOP
        dbms_output.Put_line (l_missing_gr_identity.script);
   EXECUTE IMMEDIATE l_missing_gr_identity.script;
    END LOOP;

    FOR l_missing_gr_siw IN cu_missing_grants_siw LOOP
        dbms_output.Put_line (l_missing_gr_siw.script);
    EXECUTE IMMEDIATE l_missing_gr_siw.script;
    END LOOP;

    FOR l_missing_gr_edw IN cu_missing_grants_edw LOOP
        dbms_output.Put_line (l_missing_gr_edw.script);
    EXECUTE IMMEDIATE l_missing_gr_edw.script;
    END LOOP;
END;
/