Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Passing an Array of Attributes to SQL

by cocl04 (Sexton)
on Oct 07, 2009 at 13:29 UTC ( [id://799715]=perlquestion: print w/replies, xml ) Need Help??

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

All, I am trying to pass an array of attributes to a sql statement. The array contains a list of comma delimited sql case statements that I want to append to the attributes in the sql statement. My sample data has two case statements to test the process. Of course, I can add one element of the array and it works. How do I add the entire array as on continuous comma delimited string?

# Create Array_ref with list of case statements for array. my $case = $case_query1->fetchall_arrayref(); #I get two case statements. foreach $r (@{$case}) { @case2 = join(", ", @{$r}); print "@case2"; } # Count number of rows selected my $num = $case_query1->rows; #I get two case statements. print "\n\nThis process returns $num of case statements.\n"; #I get one case statements. print "@case2"; # Set up Query for Creating case data my $case_stmt3 = "create table case_st_data1 as (S +ELECT rd, store_number @case2 FROM test_ptw_bottom_10 where week_end_date in ('19-SEP-09','26-sep-09') group by rd, store_number)";

Can someone help me with the array logic to pass the entire array as a comma delimited string? Note: I only added the parts of the code that are relivent. If you have questions let me know. Thanks...

Replies are listed 'Best First'.
Re: Passing an Array of Attributes to SQL
by mje (Curate) on Oct 07, 2009 at 14:01 UTC

    Why would you assign the result of join to an array (as in @case2 = join(", ", @{$r});. Doesn't join return a scalar. I think you need a comma after "store_number" too.

      The perl script works...sql and all in the current format. The case statements are as follows:

      ,max(CASE WHEN week_end_date = '26-SEP-09' THEN rank END) AS DATE_AS_O +F_09_26_2009 ,max(CASE WHEN week_end_date = '19-SEP-09' THEN rank END) AS DATE_AS_O +F_09_19_2009

      They already have a comma in them. Since I am introducing multiple lines, it has to come in with a comma. That part works. I just want to be able to read in more than one part of the array. I want to read the entire array as a string.

        Are you saying fetchrow_arrayref returns:

        [ [',max(CASE WHEN week_end_date = '26-SEP-09' THEN rank END) AS DATE_A +S_OF_09_26_2009'], [',max(CASE WHEN week_end_date = '19-SEP-09' THEN rank END) AS DATE_A +S_OF_09_19_2009'] ]

        or show us what use Data::Dumper;print Dumper($case); outputs.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://799715]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-19 05:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found