Greetings!
I'm trying to build a query based on a "Action". The Action value comes from a SELECT query.

I have a hash which contains the database field names and there equivalent values (These values will be populated by a SELECT query).
I want to build a query from this hash based on the "Action" value.
For example if the "Action" is "Problem Submit", I'd like a build a query like

INSERT INTO Ticket_Data(Data_id, Prov_id, Provclientid) VALUES (Ticket +_Data_seq.nextval, 'EECO::UTN:BPL', 'BPC::123');
All the other fields needs to be skipped if the Action is "Problem Submit"(other than Data_id, Prov_id, Provclientid)

I'm trying to achieve the same using the code below, but could not get the values while building the query.
The reason being, to skip some of the fields I need to traverse the hash using a foreach() loop, once done I've to
append the values outside the foreach() loop to the main query.

Could anyone throw some light about how to achieve the above said functionality.
$query = $dbh->prepare(qq{SELECT * from table_name}); ... my $data = $query->fetchrow_hashref; ... %MAPPING = ( 'Data_id' => 'Ticket_Data_seq.nextval', 'Action' => "Problem Submit", 'Cust_Ticket_Number' => "", 'Our_Ticket_Number' => "$data->{'TicketNumber'}", 'msg_id' => "", 'time_stamp' => "", 'Description' => "", 'EL_Edesc' => "", 'prov_id' => "EECO::UTN::BPL", 'provclientid' => "BPC::123", 'prov_client_org' => "", 'prov_client_buz' => "", 'prov_schema_type' => "" ); if ($action eq 'Problem Submit') my $query = "INSERT INTO Ticket_Data ("; for $key ( keys %ORA_MAPPING ) { $value = $ORA_MAPPING{$key}; next if $key eq "Data_id"; $q.=" $key, "; } $query .= " ) VALUES ($ORA_MAPPING{$_} )"; } elsif ($action eq 'Update') { ... }
!!@!!
May the force be with you!!

In reply to Dynamic query generation by chanakya

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.