Block Connection From Excel To Oracle

There might be several reasons why you want to block a connection to a database from excel files. It might not be only the Excel but other programs as well. It’s up to you how strict you chose to be 🙂

CREATE OR REPLACE TRIGGER trg_block_excel AFTER LOGON ON DATABASE
DECLARE
  prog_ sys.v_$session.program%TYPE;
BEGIN
  SELECT UPPER(program) INTO prog_ 
  FROM sys.v_$session
  WHERE 1=1
  AND audsid = USERENV('SESSIONID')
  AND audsid != 0; 
 
  IF prog_ LIKE '%EXCEL%' THEN
     RAISE_APPLICATION_ERROR(-20000, 'You are not allowed to connect to this database using Excel!');
  END IF;
  
END;

You can also restrict the usage for a specific set of users; such as the CRM department (based on roles).

CREATE OR REPLACE TRIGGER trg_block_excel AFTER LOGON ON DATABASE
DECLARE
  prog_ sys.v_$session.program%TYPE;
  v_role_trigger NUMBER := 1; 
BEGIN
  SELECT UPPER(program) INTO prog_ 
  FROM sys.v_$session
  WHERE 1=1
  AND audsid = USERENV('SESSIONID')
  AND audsid != 0; 

  -- check if the actual user has CRM_USER role
  SELECT count(*) INTO v_role_trigger 
  FROM dba_role_privs
  WHERE 1=1
  AND grantee = user
  AND granted_role = 'CRM_USER'
 
  IF prog_ LIKE '%EXCEL%' 
     AND v_role_trigger <> 0  
  THEN
     RAISE_APPLICATION_ERROR(-20000, 'CRM users are not allowed to connect to this database using Excel!');
  END IF;
  
END;