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

hi i am a new user of perl. i have two sql queries written with perl script.the problem is when i run the script i need the queries to be linked and then run.but this is not happening.can you tell me what is wrong here.here is the query and perl sript in between it. # Query the database for a list of accounts, contracts and products that are of interest. # Where "of interest" is defined as may have terminated a required product in the last # 2 months. my $sqlcmd="select cmf.account_no, cc.contract_type,to_char(cc.start_dt,'mon dd YYYY HH:MM:ss')start_dt, ciem.external_id, emf_p.element_id,to_char(emf_p.product_inactive_dt,'mon dd YYYY HH:MM:ss')product_inactive_dt from CMF cmf, CUSTOMER_CONTRACT cc, SERVICE emf, SERVICE emf_c, PRODUCT emf_p, CUSTOMER_ID_EQUIP_MAP ciem ,product_charge_map pcm where cmf.account_no = emf_c.parent_account_no and ((cmf.account_no = cc.parent_account_no and cc.contract_level = 1) or (emf_c.subscr_no = cc.parent_subscr_no and emf_c.subscr_no_resets = cc.parent_subscr_no_resets and cc.contract_level = 3))"; # Add list of interesting contracts. $sqlcmd ="$sqlcmd and ("; $i = 0; foreach $contract_key (sort(keys(%contracts))) { $sqlcmd="$sqlcmd cc.contract_type = $contract_key"; $i++; if ($i < $contract_count) { $sqlcmd=" $sqlcmd or "; } } $sqlcmd="$sqlcmd )"; $sqlcmd =" $sqlcmd and (cc.end_dt is NULL or cc.end_dt >= emf_p.product_inactive_dt) and cmf.account_no = emf.parent_account_no and emf.subscr_no = emf_c.subscr_no and emf.subscr_no = ciem.subscr_no and emf.subscr_no_resets = ciem.subscr_no_resets and ciem.external_id_type = 1 and emf.subscr_no = emf_p.parent_subscr_no and emf.subscr_no_resets = emf_p.parent_subscr_no_resets and emf_p.tracking_id =pcm.tracking_id and emf_p.tracking_id_serv=pcm.tracking_id_serv and emf_p.parent_account_no=pcm.parent_account_no and PCM.inactive_dt is not NULL and PCM.billed_thru_dt is not NULL and emf_p.product_inactive_dt <= to_date('$now','mm/dd/yyyy') and emf_p.product_inactive_dt > to_date('$one_month','mm/dd/yyyy')"; please help with this. thanks in advance.

Replies are listed 'Best First'.
Re: query linking
by toolic (Bishop) on Feb 11, 2009 at 01:36 UTC
    icantmakemuchsenseofyourquestionbecasueyoudontusemarkuptoseparateyour codefromyourtextandyoudontcaptalizethefirstwordofyoursentencesandyoudontuseenoughcommas:)

    Please read Writeup Formatting Tips, then edit your original post to make your question more readable.

Re: query linking
by monarch (Priest) on Feb 11, 2009 at 03:48 UTC

    You had a preview button and you didn't use it.

    This is my interpretation of your code, laid out in a more readable manner. Just out of curiosity, what company is this code coming from?

    # Query the database for a list of accounts, # contracts and products that are of interest. # Where "of interest" is defined as may have # terminated a required product in the last # 2 months. my $sqlcmd=qq^ SELECT cmf.account_no, cc.contract_type, to_char(cc.start_dt,'mon dd YYYY HH:MM:ss') start_dt, ciem.external_id, emf_p.element_id, to_char(emf_p.product_inactive_dt, 'mon dd YYYY HH:MM:ss') product_inactive_dt FROM CMF cmf, CUSTOMER_CONTRACT cc, SERVICE emf, SERVICE emf_c, PRODUCT emf_p, CUSTOMER_ID_EQUIP_MAP ciem , product_charge_map pcm WHERE cmf.account_no = emf_c.parent_account_no AND ( (cmf.account_no = cc.parent_account_no AND cc.contract_level = 1 ) OR (emf_c.subscr_no = cc.parent_subscr_no AND emf_c.subscr_no_resets = cc.parent_subscr_no_resets AND cc.contract_level = 3 ) )^; # Add list of interesting contracts. $sqlcmd ="$sqlcmd AND ("; $i = 0; foreach $contract_key (sort(keys(%contracts))) { $sqlcmd="$sqlcmd cc.contract_type = $contract_key"; $i++; if ($i < $contract_count) { $sqlcmd=" $sqlcmd OR "; } } $sqlcmd="$sqlcmd )"; $sqlcmd =qq^ $sqlcmd AND (cc.end_dt is NULL OR cc.end_dt >= emf_p.product_inactive_dt) AND cmf.account_no = emf.parent_account_no AND emf.subscr_no = emf_c.subscr_no AND emf.subscr_no = ciem.subscr_no AND emf.subscr_no_resets = ciem.subscr_no_resets AND ciem.external_id_type = 1 AND emf.subscr_no = emf_p.parent_subscr_no AND emf.subscr_no_resets = emf_p.parent_subscr_no_resets AND emf_p.tracking_id =pcm.tracking_id AND emf_p.tracking_id_serv=pcm.tracking_id_serv AND emf_p.parent_account_no=pcm.parent_account_no AND PCM.inactive_dt is not NULL AND PCM.billed_thru_dt is not NULL AND emf_p.product_inactive_dt <= to_date('$now','mm/dd/yyyy') AND emf_p.product_inactive_dt > to_date('$one_month','mm/dd/yyyy' )^;
      Oh, by the way, this seems fairly typical of code that I've witnessed in the investment banking industry. Biggest bunch of cowboys that ever lived.. God only knows how any of their financial transactions and calculations ever actually produced viable answers - clearly logical thought (such as programming) is way beyond the average investment banker coder..
        I think you have found the secret of the present financial crisis. It was brought upon us by crappy SQL-code! LOL

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      A reply falls below the community's threshold of quality. You may see it by logging in.
Re: query linking
by CountZero (Bishop) on Feb 11, 2009 at 07:07 UTC
    Thanks to monarch we now have a more legible form of your question. But what do you mean exactly by "linking" the two sql queries? What does "this is not happening" mean to you? Did your script throw any errors? If so, which ones? Did it produce unexpected output? If so, what was it and what did you expect?

    So many questions ...

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: query linking
by CountZero (Bishop) on Feb 11, 2009 at 21:37 UTC
    I got the following answer in my mailbox from ritzi86:
    Hi what I meant by linking and all is that, as you can see, the SQL query is in two parts, separated by a Perl script. Now when I run the script the query should run as 1 which it did not. So I want to know is it because of something in the Perl script?

    (edited the text for typos and so)

    As far as I can tell, neither the "first" nor the "second" SQL queries ever run. Actually there is only one SQL query. There seems to be a "fixed" part (everything before # Add list of interesting contracts) and a dynamic part which is written by the foreach loop. Then follows another (semi-)fixed part which is tacked to the previous part. ... And that's it.

    No code to run the SQL code, it must be somewhere else in the script and if what we have seen is in any way an example of how this script is written, I have no desire to look at it.

    For instance, the dynamic part of the SQL query can be much clearer written as one line of code:

    $sqlcmd .= 'AND (' . join 'OR ', map {"cc.contract_type = $_ "} sort k +eys %contracts;
    No $i iterator-variable is necessary and no need to keep a $contract_count variable around (and coordinated); no risk of having a subtle "off by one" (did we start counting from zero or one?) error. I'm almost certain the sort function is superfluous: database-engines don't care whether the query is sorted. The internal optimizer will take care of such things.

    Quite a lot of comments for a small snippet of code. One wonders (and shudders) if one thinks of the whole script: obviously there are things no sane man is meant to know.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James