in reply to Re^2: SQL queries using dynamic array
in thread SQL queries using dynamic array

Various bits of advice in no particular order:

Try to express yourself more clearly. Try to think more clearly. What information does your script start with? What information do you want it to produce as output? How do you come up with that extra information? It looks to me like you are having equal amounts of trouble with Perl syntax and with your overall conception of the task.

UPDATE: I just noticed that you have posted the exact same body of messy nonsense as replies to several other nodes in this thread. DON'T DO THAT. Apart from the fact that those others will be even more puzzled than I was by your reply (because you were copying from my posted code, not from theirs), it's very likely they would have seen your single reply to me, because many monks use the Monastery services "Newest Nodes" and/or "Recent Threads" to keep up with ongoing dialogs.

Replies are listed 'Best First'.
Re^4: SQL queries using dynamic array
by Anonymous Monk on Nov 16, 2008 at 20:43 UTC
    I appreciate your response. I have my password set to my email addres +s at work. I also left my laptop at work. So therefore, I can't get + to it. Secondly, I already have a process that works great and the +syntax is correct. I will post it on Monday. I apologize. Here is +what I am doing...I have a process that pulls data from oracle, forma +ts an excel worksheet, and auto emails reports to the field by id gro +up (District Manager, Regional Manager, VP, etc…). Currently, all of + the email addresses are hard coded and they are sent by group. The +business owner wants the reports to be tailored to individuals and se +nt to that specific individual. So, I have a test process that queries the database and loads a list o +f distinct id's into an array. The email addresses are id@...com. S +o that part is easy. By using a loop, I can successfully pass the id + to the Perl code and update the correct email heading and changes th +e title of the file to the correct id and emails it to the correct ad +dress. I want to have the select * from table where id = id. ...Whe +re the id is specific to the array index. I don’t want to use an IN +‘id’ for all id’s that match. Currently, the test sql process select +s the first array index for the id, but no data returns for the next +ids. So, only id 1 (array index '0') has data. The rest of the emai +ls return no data. I just need help dynamically selecting info for a + specific id. The current process works, but I need it to be dynamic +. I hope this helps you understand. I will send the code. Will you + help me? I have worked on this all week. I just happen to do a sea +rch at home and found this posting. I know the prepare statement sho +uld probably should not be done in a while loop or a loop for that ma +tter. But I am not sure how to approach the loop. I basically took +the current process that works and tried to wrap it into a loop and p +ass the new id from the distinct list to the sql statement and email. + The email process works, but the prepare sql statement only returns + the first index. I thought that the $value could be used with a pla +ceholder and updated each time the array index incremented. But is l +ooks like that is a bad idea. Please help!!!! i.e. @id = ('A01','B55') Thank you for your advice.
      Please help!!!!

      Sorry, but it looks like the kind of help you need is not technical in nature. You completely ignored my advice about formatting your posts, you are not posting any code that makes any sort of sense, and your rambling description is also nonsensical.

      The impression I get is that you have no idea what you are trying to do, which would explain why you are unable to describe it clearly. And since you are not paying any attention to the advice you've been given, there's not much I can offer that would help you.

      The current process works, but I need it to be dynamic.

      Well, you haven't really shown us anything that works yet, have you? So no, this doesn't help us understand. You might just need something as simple as "placeholders" in the sql statement, but I'm not sure, because nothing you've presented so far makes any sense.

        ############################ # # Set the Perl Modules # ############################ use strict; use DBI; use Spreadsheet::WriteExcel::Big; use Mail::Sender; my $i = 0; my $j = 1; ################################## # # Connect to Oracle database. # ################################### my $connection = DBI->connect('dbi:Oracle:xxxx','xxxx','xxxx',{ AutoCommit => 0, RaiseError => 1, PrintError => 1, }) || die "Database connection not made: $DBI::errstr"; ############################ # # Set up Query for Store # ############################ my $stmt_1 = "select alignment from rd_list"; ############################ # # Prepare Query # ############################ my $query = $connection->prepare($stmt_1); ############################ # # Execute Query # ############################ $query->execute() or die $connection->errstr; ############################### # # Declaration. # ############################### my ($alignment); ############################################ # # binds each column to a scalar reference # ############################################ $query->bind_columns(undef,\$alignment); ############################### # # Create Array_ref for array. # ############################### my $rows = $query->fetchall_arrayref; ############################### # # Load data in array. # ############################### foreach $i(0..$#{$rows}) { foreach $j (0..$#{$rows->[$i]} ) { $rows->[$i][$j]; } } my $num = $query->rows; print "\nThis process returns $num of Rd's.\n\n"; $query->finish(); $connection->rollback; $connection->disconnect(); ###################################################################### +################################################ while ($i<=2) { my $placeholder = ":p_$j"; print "$placeholder\n"; my $rd = "$rows->[$i][0]"; print "$rd\n"; ################################ # # create a new EXCEL instance # ################################ my $Excelfile = "Q4 $rd PERFORMANCE RPT.xls"; my $excel = Spreadsheet::WriteExcel::Big->new("$Excelfile"); my $worksheet = $excel->addworksheet("Q4_STORE"); ###################################################################### +####################### # # Start Store process... # ###################################################################### +####################### ########Create Worksheet format for first worksheet######## ################################ # # format worksheet for Headers # ################################# my $headings = $excel->add_format(); $headings->set_bold(); $headings->set_color('white'); $headings->set_bg_color('blue'); $headings->set_align('top'); $headings->set_text_wrap(); #################################### # # format / merge cells for Header # ##################################### $worksheet->merge_range('A1:D1',"4th Quarter District Manager Quarterl +y Performance Incentive!",$headings); $worksheet->merge_range('A2:D2'," ", $headings); $worksheet->merge_range('A3:D3', "Period Measured: 10-01-2008 through +12-31-2008", $headings); $worksheet->merge_range('A4:D4', " ", $headings); $worksheet->merge_range('A5:D5', "Growth:", $headings); $worksheet->merge_range('A6:D6', "plus 18 Customers OR", $headings); $worksheet->merge_range('A7:D7', "plus 27 BOR OR", $headings); $worksheet->merge_range('A8:D8', "plus 3300 in SMRR", $headings); $worksheet->merge_range('A9:D9', " ", $headings); $worksheet->merge_range('A10:D11', "Each store that meets one of the g +rowth goals must also achieve a 5.9% average for Q4.", $headings); ######################################## # # set column widths for Column Headers # ######################################### $worksheet->set_column('A:A',16.57); $worksheet->set_column('D:D',23.71); $worksheet->set_column('F:F',16.71); $worksheet->set_column('G:G',16.29); $worksheet->set_column('H:H',15.29); $worksheet->set_column('I:I',14.86); $worksheet->set_column('J:J',16.29); $worksheet->set_column('K:K',15.86); $worksheet->set_column('L:L',26.86); $worksheet->set_column('M:M',19.00); ######################################## # # format worksheet for Column Headers # ######################################### my $col_headings = $excel->add_format(); $col_headings->set_bold(); $col_headings->set_size(10); $col_headings->set_color('white'); $col_headings->set_align('center'); $col_headings->set_bg_color('blue'); $col_headings->set_border(2); $col_headings->set_shrink(); ################################ # # Write Heading(s) to worksheet # ################################ $worksheet->write(13, 0, "WEEK_END_DATE", $col_headings); $worksheet->write(13, 1, "SVP", $col_headings); $worksheet->write(13, 2, "RD", $col_headings); $worksheet->write(13, 3, "DM", $col_headings); $worksheet->write(13, 4, "STORE", $col_headings); $worksheet->write(13, 5, "WTD_SMRR_GAIN", $col_headings); $worksheet->write(13, 6, "QTD_SMRR_GAIN", $col_headings); $worksheet->write(13, 7, "WTD_BOR_GAIN", $col_headings); $worksheet->write(13, 8, "QTD_BOR_GAIN", $col_headings); $worksheet->write(13, 9, "WTD_CUST_GAIN", $col_headings); $worksheet->write(13, 10, "QTD_CUST_GAIN", $col_headings); $worksheet->write(13, 11, "WTD_CARD_CLOSED", $col_headings); $worksheet->write(13, 12, "QTD_AVG_CARD_CL", $col_headings); ###################### # # Freeze panes # ###################### $worksheet->freeze_panes(14,5); ############################ # # format rest of data data # ############################ my $format = $excel->add_format(); $format->set_size(10); $format->set_align('center'); $format->set_border(1); $format->set_num_format('General;[Red](-General);General'); ########Create Function / Subroutine to Get Data and insert into Works +heet######## sub get_store_data { ################################## # # Connect to Oracle database. # ################################### my $dbh = DBI->connect('dbi:Oracle:xxxx','xxxx','xxxx',{ AutoCommit => 0, RaiseError => 1, PrintError => 1, }) || die "Database connection not made: $DBI::errstr"; #DBI-> trace(2); ############################ # # Set up Query for Store # ############################ my $stmt = "select week_end_date, SVP, RD, DM, store, wtd_smrr_gain,QTD_SMRR_GAIN, wtd_bor_gain,QTD_BOR_GAIN, wtd_cust_gain,QTD_CUST_GAIN, WTD_CARD_CLOSED,QTD_AVG_CARD_CL from bonus_4Q_store where SVP <> 'RD' and RD = ? order by svp,dm,store"; ############################ # # Prepare Query # ############################ my $sth = $dbh->prepare($stmt); ############################ # # Bind Parameters. # ############################ #$sth->bind_param($placeholder, $rd); ############################ # # Execute Query # ############################ $sth->execute($rd) or die $dbh->errstr; ############################### # # Declaration. # ############################### #my ( $week_end_date,$SVP,$RD,$DM,$store, # $wtd_smrr_gain,$QTD_SMRR_GAIN, # $wtd_bor_gain,$QTD_BOR_GAIN, # $wtd_cust_gain,$QTD_CUST_GAIN, # $WTD_CARD_CLOSED,$QTD_AVG_CARD_CL); ############################################ # # binds each column to a scalar reference # ############################################ #$sth->bind_columns(undef,\$week_end_date,\$SVP,\$RD,\$DM,\$store, # \$wtd_smrr_gain,\$QTD_SMRR_GAIN, # \$wtd_bor_gain,\$QTD_BOR_GAIN, # \$wtd_cust_gain,\$QTD_CUST_GAIN, # \$WTD_CARD_CLOSED,\$QTD_AVG_CARD_CL); ############################### # # Create an array reference. # ############################### my $a_row = $sth->fetchall_arrayref(); ############################### # # insert Data into spreadsheet # ############################### foreach $stmt (@{$a_row}) { $worksheet->write_col(14,0,$a_row,$format); last; } ################## # # Finish part 1 # ################## $sth->finish(); $dbh->rollback; $dbh->disconnect(); }###end of function1### ############################### # # Call Functions / Subroutines # ############################### &get_store_data; print "$rd DONE WITH Q4 Store DATA! \n"; $excel->close(); ################################## # # Create email addresses for RD. # ################################## my $at = "@"; my $rentacenter_com = "rentacenter.com"; my $rd_at_rentacenter_com = "$rd$at$rentacenter_com"; print "$rd_at_rentacenter_com\n"; ################################## # # Create email addresses for ME. # ################################## my $cory_clay = "cory.clay"; my $cory_clay_at_rentacenter_com = "$cory_clay$at$rentacenter_com"; print "$cory_clay_at_rentacenter_com\n"; ################################## # # Create email Headding for RD. # ################################## my $Q4 = "Q4 "; my $PERFORMANCE_RPT = " PERFORMANCE RPT.xls"; my $Q4_RD_PERFORMANCE_RPT = "$Q4$rd$PERFORMANCE_RPT"; print "$Q4_RD_PERFORMANCE_RPT\n\n"; ######################################### # # Email the email with Send attachment. # ########################################## my $sender = new Mail::Sender { smtp => 'xx.xx.x.xx', from => 'cory.clay@rentacenter.com' }; $sender->MailFile( { to => $cory_clay_at_rentacenter_com, subject => $Q4_RD_PERFORMANCE_RPT, msg => "All, Test Body of email. Thanks, Cory Clay $cory_clay_at_rentacenter_com \n", file => $Q4_RD_PERFORMANCE_RPT, }); $sender->Close; $i++; $j++; } exit(0);

        I am sorry. I never should have written anything at home. I have a very simple task.

        Step 1: Select a list of id's and load into an array. Don't hard code because the list may change.

        Step2: Select data for a specific id by passing a single id from the array index to the sql statement.

        Step 3: Email the data to a specific id.

        Currently, I perform the following:

        Step 1: Select data for a group of id's. The data is hard coded for a specific group.

        Step 2: Email data to the group of id's.

        Note: When I hard code the data, I have to repeat the process for each group.

        I am trying to select a list of distinct id's and load them into an array. Iterate through the index and select data for a specific id. And email the data to a specific id. I am sorry if that is not clear. The hard coded group email process works. In terms of the dynamic select...As I iterate throught the array, it prints and emails the correct id, however the select only returns the first index in the array. Again from your tone, it was a mistake to send this at home without all the info I needed. I understand if you don't want to help...but I really need your help...I write sql all day...not perl...This dynamic process would be all the help I need. Will you help me?...Thanks, Cory