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

i am using qq for a sql statement and the sql contains the table name as variable and this sql is used to insert german umlauts. My problem is when running sql using qq with table name as variable it is inserting a junk value in DB2 dabase. however when i use the table name instead of variable in the sql it is prinitng correctly. help required...

Replies are listed 'Best First'.
Re: qq sql - umlaut isssue
by tobyink (Canon) on Jul 30, 2012 at 07:52 UTC

    What character set is your Perl script saved in?

    The best idea is to make sure you're using UTF-8 everywhere. Instruct your text editor to save your Perl script in UTF-8 (and make sure you include the line use utf8; in the script to inform Perl that it's in UTF-8!); make sure your database is set up to accept UTF-8 data; make sure DBI is configured correctly to transmit UTF-8.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      UTF8 format is used already and this is not a problem. the problem is as described below

      In the below SQL when i use the table name ($updatetable)as variable inside 'qq' for update it is displaying junk.

      push @arraysql, qq{ UPDATE $updatetable a SET sometext = 'üseable term' where .... };

      however when i use the table name direclty inside 'qq' the umlaut character is updated correctly.

      push @arraysql, qq{ UPDATE user.updatetable a SET sometext = 'üseable term' where .... };

        UTF8 format is used already

        But is it used everywhere as I suggested?

        I concur with the anonymous monk above that $updatetable is probably not a utf8 character string, but a byte string. Where has that variable come from? Was it read from a file? If so, have you checked that the file was opened in utf8 mode?

        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

        I have a hunch. This is Perl's Unicode support messing it up. Perl says $updatetable is a character string (i.e. Unicode), and your string literal is a byte string (or vice versa), and when you try to combine them, Perl tries to upgrade both to character strings. How do you set $updatetable? Are you using the use utf8; pragma?

        If you are pushing a variable as a quoted character into an array, how does the variable get replaced with the actual value?

Re: qq sql - umlaut isssue
by chacham (Prior) on Jul 30, 2012 at 09:34 UTC

    The actual code would be helpful here.

    Note, allowing a table name as a variable is generally a bad idea. Besides being (in many cases) inefficient, it opens the door to SQL injection.