I have a question about my script. I cant seem to figure out why it keeps appending data from the previous query to the new query. So for instance the 1st query returns (1,2,3) then second query runs and returns (1,2,3(this is not suppose to be in the new file) and then 2,3,4 this is what its only suppose to return). Thanks in advance, below is the code.
print "Setting Database Connection to CDW-P02.....\n"; # setting up database connection to CDW-V my $dbv = DBI->connect( 'dbi:Oracle:Test' , "$user1" , "$passw") || die "Database connection not made: $DBI::errstr"; # Query To find APR Decline $apr_decl = "SELECT CRAC_EVNT_DATA_T, COUNT(*)\n". "FROM CAMMGR.CRAC_EVNT_FCT a, CAMMGR.CRAC_DIM B\n". "WHERE A.CRAC_DIM_I =B.CRAC_DIM_I\n". "AND CRAC_EVNT_SCRN_I = 'RDGR '\n". "AND CRAC_EVNT_D >= TO_DATE('$first_date','YYYYMMDD')\n". "AND CRAC_EVNT_D <= TO_DATE('$last_date','YYYYMMDD')\n". "GROUP BY CRAC_EVNT_DATA_T\n"; #Opens files to write to die("Cannot open SAS file to write to.") unless(open(APR, ">APR_Decline_$today.csv")); # Runs Query @ap = &run_query($apr_decl); print APR "CRAC_EVNT_DATA_T, Number Declined\n"; print APR "@ap"; close(APR); # Query captures credit line determinator declines by reason $decl_reason = "SELECT CRAC_EVNT_DATA_T, COUNT(*)\n". "FROM CAMMGR.CRAC_EVNT_TYPE A, CAMMGR.CRAC_EVNT_FCT B, CAMMGR.CRAC_DIM + C\n". "WHERE A.CRAC_EVNT_TYPE_C =B.CRAC_EVNT_TYPE_C\n". "AND B.CRAC_DIM_I = C.CRAC_DIM_I\n". "AND A.CRAC_EVNT_TYPE_C = 500\n". "AND CRAC_EVNT_DATA_T LIKE ' +00000000000.00CL00%'\n". "AND CRAC_EVNT_D >= TO_DATE('$first_date','YYYYMMDD')\n". "AND CRAC_EVNT_D <= TO_DATE('$last_date','YYYYMMDD')\n". "GROUP BY CRAC_EVNT_DATA_T\n"; #Opens files to write to die("Cannot open SAS file to write to.") unless(open(REASON, ">Decline_Reason_$today.csv")); # Runs Query @reason = &run_query($apr_decl); print REASON "CRAC_EVNT_DATA_T, Number Declined\n"; print REASON "@reason"; close(REASON); sub run_query { my ($query) = @_; # Run Query print "$query\n"; ### Prepare a SQL statement for execution $prepare_exe = $dbv->prepare( "$query"); ### Execute the statement in the database $prepare_exe->execute; # Each element will be comma seperated while ( @row = $prepare_exe->fetchrow_array( ) ) { $string = join(",", @row); $string = $string."\n"; #print "$string\n"; # Will contain all rows push(@new_row, $string); } return @new_row; $prepare_exe->finish; }

In reply to Query Subroutine by Fuism

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.