LeeC79 has asked for the wisdom of the Perl Monks concerning the following question:

From a Perl script, I'm need to run an SQL Script which generates an output file. From there, I need to FTP that output file to another FTP server. I think I have the FTP part already, I'm just looking for a way to run the SQL Script, and then make sure the script is done before attempting to FTP the ouput file. Any help is greatly appreciated.

edited by ybiC: Retitle from nondescriptive and search hostile "Very New to Perl"

Replies are listed 'Best First'.
Re: Waiting for an SQL script to finish
by atcroft (Abbot) on Aug 26, 2003 at 17:57 UTC

    First of all, welcome to the Monastery, and I hope you find it both helpful and enjoyable to visit here.

    Now, to your question. First of all, I would suggest using the DBI module to make connections to the database, then you can just execute the SQL commands you have (there are some articles in the Tutorials section which may also be helpful in looking at that). You can also use Net::FTP for handling the FTP connection (although it might also be possible for your to install the appropriate modules on the remote machine and run the script from there, eliminating the FTP requirement-just a thought).

    HTH.

      There isn't anything I can do to just run an SQL script? Since I've already created a script file, which outputs the result set into a file, I think just running the SQL script from a Perl script would be easiest. ??
Re: Waiting for an SQL script to finish
by sweetblood (Prior) on Aug 26, 2003 at 19:20 UTC
    Welcome ... You've come to the right place to start learning Perl.
    I'm sure you can get the help you need here, but it would be most helpful if you provide what you've done to this point. You say you have a script and the FTP portion completed. Let's have a look so we can point you in the right direction.
      The script I've made mention of is an SQL Script, here it is:
      rem finished_goods.sql set feedback off set heading off set echo off set pagesize 0 set linesize 510 spool invent_sap1.unl SELECT b.article_id || ' ' || 'CWL' || ' ' || b.mill_id || ' ' || b.track_num || ' ' || stat || ' ' || class || ' ' || TRIM(' ' FROM TO_CHAR(SUM(A.wgt_est), '999999990.9')) || ' ' || TRIM(' ' FROM TO_CHAR(SUM(sheet_count), '999999990.9')) FROM invent a, (SELECT unit_id, article_id, track_num, mill_id FROM invent WHERE (class = 'U' OR class = 'W' OR(class = 'T' AND class_orig IN('U', 'W')) OR (class = 'L' AND class_orig IN('U', 'W'))) AND stat IN('G','J','T') AND ts_scaled > 0 AND unit_id=roll_id_lead) b WHERE a.roll_id_lead=b.unit_id AND (a.type_code = 'S' OR a.type_code = 'C') GROUP BY b.article_id, 'CWL', b.mill_id, b.track_num, stat, class UNION SELECT b.article_id || ' ' || 'CWL' || ' ' || b.mill_id || ' ' || b.track_num || ' ' || stat || ' ' || class || ' ' || TRIM(' ' FROM TO_CHAR(SUM(A.wgt_scaled), '999999990.9')) || ' ' || TRIM(' ' FROM TO_CHAR(SUM(sheet_count), '999999990.9')) FROM invent a, (SELECT unit_id, article_id, track_num, mill_id FROM invent WHERE (class = 'U' OR class = 'W' OR (class = 'T' AND class_orig IN('U', 'W')) OR (class = 'L' AND class_orig IN('U', 'W'))) AND stat IN('G','J','T') AND ts_scaled > 0 AND unit_id=roll_id_lead) b WHERE a.roll_id_lead=b.unit_id AND a.type_code='R' GROUP BY b.article_id, 'CWL', b.mill_id, b.track_num, stat, class; spool OFF set feedback on set heading on set echo on set pagesize 1000 set linesize 100
      When I said I think I have the FTP part, I should have said I've found a code snippet that I should be able to use. My starting point is figuring out how to call that SQL script. Thanks.
        Most, if not all, relational databases come with a commandline tool. Assuming the tool takes commands from standard input, the tool is named toolsql and your SQL file is called whatever.sql, you could do something like this in your program:
        system "toolsql < whatever.sql"; die "toolsql failed with exit code ", $? >> 8 if $?;

        Abigail

        I'm going to take a wild guess and say that that script is not a shell script, right? What database are you using? That script actually looks like a session from a database client, like the MySQL client "mysql>", or the Oracle client "sqlplus>", or the PostgreSQL client "psql>". Are you sure you just want to know how to run that "script" from Perl?

        Forget about Perl for the moment and using Perl to execute that "script" - how do you execute that "script" now?

        What program do you feed this script to?

        CountZero

        "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law