Hi,
I am trying to construct a flexible update SQL so that I can just pass the parameters that I want and it will construct the update SQL correctly for me. I have the code below.
#!/usr/bin/perl
use warnings;
use strict;
update_table( 'test_name', 'test_phone',
'test_city', 'test_country' );
update_table( 'test_name', 'test_phone',
'', 'test_country' );
update_table( 'test_name', '',
'test_city', '' );
sub update_table {
my ($name, $phone, $city, $country) = @_;
my $sql = qq! UPDATE table SET !
. qq! phone = '$phone', !
. qq! city = '$city', !
. qq! country = '$country' !
. qq! WHERE name = '$name' !;
print $sql;
return 1;
}
If I pass all the parameters to the subroutine, it gives me the correct update SQL. The problem starts when I start passing some null parameters.
For example, if I start passing some null parameters, the subroutine would create it like this:
UPDATE table SET phone = 'test_phone',
city = '', country = 'test_country' FROM ...
The correct one that I am hoping for is this (without the city assuming that I pass a null value for this parameter):
UPDATE table SET phone = 'test_phone',
country = 'test_country' FROM ...
So, to summary the problems:
How can I pass one or more null parameters to my subroutine and it will not add it in the SQL that it will create?
The subroutine should also know how to place the comma otherwise it will not be able to create the correct SQL.
Thanks.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.