DECLARE
   V_COUNT NUMBER;
   V_NRORG NUMBER;
BEGIN
   SELECT COUNT(*) INTO V_COUNT
   FROM DBA_TRIGGERS
   WHERE TRIGGER_NAME LIKE 'SET_NRORG_TRG';

   SELECT NRORG INTO V_NRORG
   FROM OPERADOR
   WHERE NRORG IS NOT NULL
     AND ROWNUM = 1
   ORDER BY NRORG DESC;

   IF V_COUNT < 1 AND V_NRORG IS NOT NULL THEN
      EXECUTE IMMEDIATE 'CREATE OR REPLACE CONTEXT NRORG_CTX USING NRORG_CTX_PKG_NEW';
      EXECUTE IMMEDIATE 'CREATE OR REPLACE PACKAGE NRORG_CTX_PKG_NEW IS PROCEDURE SET_NRORG(P_NRORG NUMBER); END;';
      EXECUTE IMMEDIATE ' CREATE OR REPLACE PACKAGE BODY NRORG_CTX_PKG_NEW IS PROCEDURE SET_NRORG (P_NRORG NUMBER) AS
                                             BEGIN
                                              DBMS_SESSION.SET_CONTEXT('||CHR(39)||'NRORG_CTX'||CHR(39)||','||CHR(39)|| 'NRORG_PADRAO'||CHR(39)||', 0);
                                              DBMS_SESSION.SET_CONTEXT('||CHR(39)||'NRORG_CTX'||CHR(39)||', '||CHR(39)||'NRORG'||CHR(39)||', P_NRORG);
                                           END SET_NRORG; END;';
      EXECUTE IMMEDIATE 'CREATE OR REPLACE TRIGGER SET_NRORG_TRG AFTER LOGON ON DATABASE BEGIN NRORG_CTX_PKG_NEW.SET_NRORG('||V_NRORG||'); END; ';
   END IF;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('ATUALIZACAO_ESTRUTURA_BD','6.35.041'); END;
/

