Simulando sessão Oracle Application em Ferramentas de acesso a Banco de Dados
Autor: Eduardo Gonçalves | Publicado: 2010-03-22T13:20:00-03:00

Hi!

Algumas vezes necessitamos executar programas ou SELECTS direto no banco de dados, ou seja, por fora do Aplicativo Oracle, através de ferramentas como SQL Navigator, SQL Tools, PL/SQL Developer ou outras ferramentas do gênero. Quase sempre, para VIEWS que são utilizadas no EBS, é necessário que o ambiente esteja com algumas variáveis “setadas” para que possamos visualizar os dados. Caso contrário, nossa QUERY ou programa não retorna nada. Segue abaixo um SCRIPT que sempre uso e que prepara o ambiente como se estivéssemos conectados no Oracle Applications. Ele funciona para 99% dos casos. Salvo outros procedimentos mais específicos que podem ocorrer dependendo do módulo e responsabilidade que desejamos acessar. Execute este SCRIPT na ferramenta que você estiver utilizando para acessar o banco de dados e substitua as varáveis: '&amp;user_name' e '&amp;responsibility_key' pelo nome do usuário e chave da responsabilidade, respectivamente. Este último você encontra no cadastro de responsabilidades no Administrado do Sistema.

 declare
&nbsp; p_application_id&nbsp;&nbsp;&nbsp;&nbsp; number;
&nbsp; p_responsibility_id&nbsp; number;
&nbsp; p_responsibility_key varchar2(200);
&nbsp; p_user_id&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; number;
&nbsp; p_user_name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; varchar2(200);
&nbsp; p_org_id&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; number;
begin
&nbsp; --
&nbsp; begin
&nbsp;&nbsp;&nbsp; p_user_name := '&amp;user_name';
&nbsp;&nbsp;&nbsp; --
&nbsp;&nbsp;&nbsp; select user_id
&nbsp;&nbsp;&nbsp; into&nbsp;&nbsp; p_user_id
&nbsp;&nbsp;&nbsp; from&nbsp;&nbsp; fnd_user
&nbsp;&nbsp;&nbsp; where&nbsp; user_name = p_user_name;
&nbsp; exception
&nbsp;&nbsp;&nbsp; when no_data_found then
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; raise_application_error(-20001,'Usuário não encontrado: '||p_user_name);
&nbsp;&nbsp;&nbsp; when others then
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; raise_application_error(-20002,'Erro ao recuperar o id do usuário '||p_user_name||'. Erro: '||sqlerrm);
&nbsp; end;
&nbsp; --
&nbsp; begin
&nbsp;&nbsp;&nbsp; p_responsibility_key := '&amp;responsibility_key';
&nbsp;&nbsp;&nbsp; --
&nbsp;&nbsp;&nbsp; select application_id
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ,responsibility_id
&nbsp;&nbsp;&nbsp; into&nbsp;&nbsp; p_application_id
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ,p_responsibility_id
&nbsp;&nbsp;&nbsp; from&nbsp;&nbsp; fnd_responsibility
&nbsp;&nbsp;&nbsp; where&nbsp; responsibility_key = p_responsibility_key;
&nbsp; exception
&nbsp;&nbsp;&nbsp; when no_data_found then
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; raise_application_error(-20003,'Responsabilidade não encontrada: '||p_responsibility_key);
&nbsp;&nbsp;&nbsp; when others then
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; raise_application_error(-20004,'Erro ao recuperar o id da responsabilidade '||p_responsibility_key||'. Erro: '||sqlerrm);
&nbsp; end;
&nbsp; --
&nbsp; begin
&nbsp;&nbsp;&nbsp; fnd_client_info.setup_client_info( p_application_id
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ,p_responsibility_id
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ,p_user_id
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ,0);
&nbsp; exception
&nbsp;&nbsp;&nbsp; when others then
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; raise_application_error(-20005,'Erro ao configurar variáveis de sessão (FND_CLIENT_INFO.SETUP_CLIENT_INFO). Erro: '||sqlerrm);
&nbsp; end;
&nbsp; --
&nbsp; begin
&nbsp;&nbsp;&nbsp; fnd_global.apps_initialize( p_user_id
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ,p_responsibility_id
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ,p_application_id
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ,0);
&nbsp; exception
&nbsp;&nbsp;&nbsp; when others then
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; raise_application_error(-20006,'Erro ao configurar variáveis de sessão (FND_GLOBAL.APPS_INITIALIZE). Erro: '||sqlerrm);
&nbsp; end;
&nbsp; --
&nbsp; begin
&nbsp;&nbsp;&nbsp; p_org_id := &amp;p_org_id;
&nbsp;&nbsp;&nbsp; --
&nbsp;&nbsp;&nbsp; -- Para versão R12 do Oracle Applications (caso não seja, comente esta parte)
&nbsp;&nbsp;&nbsp; mo_global.set_policy_context( p_access_mode&nbsp; =&gt; 'S'
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ,p_org_id&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =&gt; p_org_id);
&nbsp; exception
&nbsp;&nbsp;&nbsp; when others then
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; raise_application_error(-20007,'Erro ao configurar variáveis de sessão (MO_GLOBAL.SET_POLICY_CONTEXT). Erro: '||sqlerrm);
&nbsp; end;
end;

Obs.: Note que existe um adendo no código, para a versão R12 do Oracle Applications.

Falows!