代码改变世界

如何切换用户到不同用户Session上

2011-06-02 09:56  Tracy.  阅读(1418)  评论(0编辑  收藏  举报

CREATE OR REPLACE PACKAGE BODY TMS.SESSION_TMS
AS
   PROCEDURE SESSION_TMS (STATUS VARCHAR2)
   IS
      tmpVar           VARCHAR2 (3);
      present_schema   VARCHAR2 (10);
      present_user     VARCHAR2 (10);
      mesg             VARCHAR2 (300);
  BEGIN
      tmpVar := 'OFF';
      present_schema := '';
      present_user := '';
      mesg := '';

      IF SYS_CONTEXT ('USERENV', 'SESSION_USER') = 'TMS_GUEST'
         AND upper(STATUS) = 'ON'
      THEN
         EXECUTE IMMEDIATE ('ALTER SESSION SET CURRENT_SCHEMA=TMS');

         PRESENT_SCHEMA := 'TMS';
         PRESENT_USER := 'TMS_GUEST';
      END IF;
      IF SYS_CONTEXT ('USERENV', 'SESSION_USER') = 'TMS_GUEST'
         AND upper(STATUS) = 'OFF'
      THEN
         EXECUTE IMMEDIATE ('ALTER SESSION SET CURRENT_SCHEMA=TMS_GUEST');

         PRESENT_SCHEMA := 'TMS_GUEST';
         PRESENT_USER := 'TMS_GUEST';
      END IF;

      mesg := ' Your present schema ' || present_schema;
      mesg := mesg || '  User session is ' || present_user;
      DBMS_OUTPUT.put_line (mesg);
   END;
--SELECT   SYS_CONTEXT ('USERENV', 'CURRENT_SCHEMA'),
--             SYS_CONTEXT ('USERENV', 'session_user')
--    INTO   present_schema, present_user
--  FROM   DUAL;
END SESSION_TMS;
/