Tuesday, August 2, 2016

Run FTP and submit concurrent job by SQL

This is a SQL script to FTP get a file from a remote Windows server to database server, and then to submit an EBS concurrent job which will read that file. The database version is Oracle 11.2.0.4.

/** pre setups:
SQL> select fnd_profile.value('WIN_UPS_FILE_INFO') from dual;
FND_PROFILE.VALUE('WIN_UPS_FILE_INFO')
--------------------------------------------------------------------------------
PUL/NT_USERID passWD WIN_HOSTNAME.domain.com

SQL> select * from all_directories where directory_name = 'CUSTOM_FTPIN';
OWNER   DIRECTORY_NAME  DIRECTORY_PATH
---------   -----------------------   -----------------------------
SYS         CUSTOM_FTPIN        /path/to/ftp/in
**/

SQL> set serveroutput on
SQL> DECLARE
      --  l_utl_file_handler          UTL_FILE.FILE_TYPE;
      l_conn  UTL_TCP.connection;
      l_chr_file_line varchar2(200);
      l_chr_user_id varchar2(20);
      l_chr_password varchar2(20);
      l_chr_directory_path varchar2(200);
      l_chr_server_name varchar2(40);
      l_chr_file_name varchar2(20) := 'WIN07-24-2016.csv';
      -- l_chr_file_name varchar2(20) := '&1';
      l_request_id Number;
 BEGIN
    l_chr_file_line := fnd_profile.value('WIN_UPS_FILE_INFO');
    select substr(l_chr_file_line,1,12) into l_chr_user_id from dual;
    select substr(l_chr_file_line,14,7) into l_chr_password from dual;
    select substr(l_chr_file_line,22,30) into l_chr_server_name from dual;

    l_conn := ftp.login(l_chr_server_name, '21',l_chr_user_id,l_chr_password);
    ftp.ascii(p_conn => l_conn);
    ftp.get(p_conn      => l_conn,
               p_from_file => l_chr_file_name,
               p_to_dir    => 'CUSTOM_FTPIN',
               p_to_file   => l_chr_file_name);
     ftp.logout(l_conn);
    utl_tcp.close_all_connections;

/**  commend this out for testing FTP
 l_request_id := FND_REQUEST.SUBMIT_REQUEST('MYAP','MYAPUFVW','','',FALSE,''||l_chr_file_name||'',
chr(0), '', '','','',
                  '', '', '', '', '', '', '', '', '', '',
                  '', '', '', '', '', '', '', '', '', '',
                  '', '', '', '', '', '', '', '', '', '',
                  '', '', '', '', '', '', '', '', '', '',
                  '', '', '', '', '', '', '', '', '', '',
                  '', '', '', '', '', '', '', '', '', '',
                  '', '', '', '', '', '', '', '', '', '',
                  '', '', '', '', '', '', '', '', '', '',
                  '', '', '', '', '', '', '', '', '');

**/
commit;
END;
 /

SQL>
220 Microsoft FTP Service
331 Password required for PUL/NT_USERID.
230 User logged in.
200 Type set to A.
227 Entering Passive Mode (177,79,176,170,278,170).
125 Data connection already open; Transfer starting.
226 Transfer complete.
221 Goodbye.

PL/SQL procedure successfully completed.

SQL> exit

No comments: