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

Hi,

I am developing a portal in perl/tk and need to get some data from database. The quesry I use to get the data from DB is little complicated. Please find the query below.

SELECT gamm.PROCESS_NAME, GAMM.APPLICATION_NAME, TO_CHAR (TRUNC (gmz.testtime - 7 / 24, 'DD'),'MM/DD/YYYY') Fiscal_Day,ROUND (AVG(DECODE (total_response_time, 0, NULL, total_response_time) / DECODE (total_page_count,0, 1,total_page_count))/ 1000,2) Average_Response_time FROM   portal_gomez_bb_load gmz, GOMEZ_APP_MAPPING_MASTER gamm WHERE   gmz.monitor_id = 3805260 AND (gamm.PROCESS_NAME = 'SAM_Exact_Product_Search' OR gamm.APPLICATION_NAME = 'SAM_Exact_Product_Search') AND trunc(gmz.testtime) BETWEEN to_date('09/12/2011','MM/DD/YYYY') AND to_date('09/15/2011','MM/DD/YYYY') AND success_flag = 0 GROUP BY   gamm.PROCESS_NAME,GAMM.APPLICATION_NAME,TO_CHAR (TRUNC (gmz.testtime - 7 / 24, 'DD'),'MM/DD/YYYY')

I am getting data for this query when I execute in Toad Oracle. But the same code is not giving any data in perl/tk. Simple queries are working in my perl/tk. Is there any method for writing complicated queries in perl/tk? Can you please help.</P?

Replies are listed 'Best First'.
Re: SQL in perl/tk
by chacham (Prior) on Jan 13, 2012 at 11:59 UTC
    The query should be executed by Oracle, not by the driver, hence the complexity should not matter. As marto said, turn on error reporting.

    BTW, you can rewrite "AND (gamm.PROCESS_NAME = 'SAM_Exact_Product_Search' OR gamm.APPLICATION_NAME = 'SAM_Exact_Product_Search')" as "AND 'SAM_Exact_Product_Search' IN (gamm.PROCESS_NAME, gamm.APPLICATION_NAME)"

    . Get's rid of the OR and only requires the literal once.

    trunc(gmz.testtime) will obviate the use of any INDEX on gmz.testtime, better to use AND gmz.testtime >= to_date('09/12/2011','MM/DD/YYYY') AND gmz.testtime < to_date('09/16/2011','MM/DD/YYYY')

    IIUC, i don't think you need to specify TO_CHAR in the GROUP BY clause.

    For formatting the query, so people can read it, try:

    SELECT
    	gamm.PROCESS_NAME,
    	GAMM.APPLICATION_NAME,
    	TO_CHAR(TRUNC(gmz.testtime - 7 / 24, 'DD'),'MM/DD/YYYY')
    	Fiscal_Day,
    	ROUND
    	(
    	 AVG
    	 (
    	  DECODE(total_response_time, 0, NULL, total_response_time)
    	  / DECODE (total_page_count,0, 1,total_page_count)
    	 )
    	 / 1000,
    	 2
    	) Average_Response_time
    FROM
    	portal_gomez_bb_load		gmz,
    	GOMEZ_APP_MAPPING_MASTER	gamm
    WHERE
    	gmz.monitor_id			= 3805260
      AND	'SAM_Exact_Product_Search'	IN (gamm.PROCESS_NAME, gamm.APPLICATION_NAME)
      AND	gmz.testtime			>= to_date('09/12/2011','MM/DD/YYYY')
      AND	gmz.testtime			< to_date('09/16/2011','MM/DD/YYYY')
      AND	success_flag			= 0
    GROUP BY
    	gamm.PROCESS_NAME,
    	GAMM.APPLICATION_NAME,
    	TRUNC (gmz.testtime - 7 / 24, 'DD');
    

      Hi All, Thanks a lot for all the replies. I have tried to include the piece of SQL code by inserting into my code. But this too is not giving any results. I am using the correct Userid/pwd/schema etc. for this. Please find the piece of code I have written.

      use strict; use Tk; use Tk::BrowseEntry; use Tk::LabFrame; use Tk::JPEG; use DBI; use DBD::Oracle; use Spreadsheet::WriteExcel; . . . . . . . sub generateDailyReport{ #Create Excel my $workbook = Spreadsheet::WriteExcel->new('Report.xls'); my $worksheet = $workbook->add_worksheet(); my $from_date = @_[0]; my $to_date = @_[1]; #Connect to DB my $dbh = DBI->connect("dbi:Oracle:cecdev9","asdm_dev","c1sc0pass" +) or die "cannot connect $DBI::errstr\n"; my $sth = $dbh->prepare("SELECT gamm.PROCESS_NAME, GAMM.APPLI +CATION_NAME, TO_CHAR (TRUNC (gmz.testtime - 7 / 24, 'DD'),'MM/DD/YYYY +') Fiscal_Day,ROUND (AVG(DECODE (total_response_time, 0, NULL, total_ +response_time) / DECODE (total_page_count,0, 1,total_page_count))/ 10 +00,2) Average_Response_time FROM portal_gomez_bb_load gmz, GOMEZ_AP +P_MAPPING_MASTER gamm WHERE gmz.monitor_id = 3805260 AND (gamm.PROC +ESS_NAME = 'SAM_Exact_Product_Search' OR gamm.APPLICATION_NAME = 'SAM +_Exact_Product_Search') AND trunc(gmz.testtime) BETWEEN to_date('09/1 +2/2011','MM/DD/YYYY') AND to_date('09/15/2011','MM/DD/YYYY') AND succ +ess_flag = 0 GROUP BY gamm.PROCESS_NAME,GAMM.APPLICATION_NAME,TO_CH +AR (TRUNC (gmz.testtime - 7 / 24, 'DD'),'MM/DD/YYYY')"); #execute the query $sth->execute(); my $i = "A1"; my $j = 1; while ( my @row = $sth->fetchrow_array( ) ) { print "@row\n"; $worksheet->write($i, \@row); $j=$j+1; $i="A$j"; open(FILE,'Report.xls'); } }
      Please help. Thanks, Vidya Surendran

        Did you read the replies you already have? I advised that you look at tracing, which you've not done in this example.

        Check for an error: die $dbh->errstr if ! defined $msg;

        Not only after the DB connect, but after the statement is run.

Re: SQL in perl/tk
by marto (Cardinal) on Jan 13, 2012 at 11:00 UTC

    If it works in Toad or sqlplus but not in your perl code, I'd expect DBD::Oracle to throw some errors* which will point you in the right direction. Are you connecting to the same database as the same user in both Toad and your Perl code? Post a short example of your perl code which reproduces the problem, and any output. See Writeup Formatting Tips.

    Update * if it doesn't see DBD::Oracles trace method.

      Is vsurend using DBD::Oracle? Could be DBD::ODBC or something for all we know.

      Also, are all the connections really using the same database/username/password/schema? Different connections may point to different tables (similar looking) tables with different content. (e.g. test database vs. production database).

      Another thing, especially with Oracle: vsurend, did you compile DBD::Oracle yourself against the Oracle client you have installed on your machine or did you use some precompiled package like ActiveState's PPMs? DBD::Oracle and Oracle's libraries don't always play nice together, even on minor version differences when using a precompiled version.

      "Believe me, Mike, I calculated the odds of this succeeding against the odds I was doing something incredibly stupid… and I went ahead anyway." (Crow in "MST3K The Movie")