0

Below is my Perl script

str_replace(rtrim(c_manager),'''','_'),'."\n".
 throws error  

str_replace(rtrim(c_manager), '''"

Bad name after _' at pl_recert_output.pl line 262.

Please help to solve this issue:

$sql = 'select rtrim(f_admin_disabled),'."\n".
                '       convert(varchar,t_password,101),'."\n".
                '       rtrim(c_email),'."\n".
                '       str_replace(rtrim(c_manager),'''','_'),'."\n".
                '       rtrim(c_mgr_email)'."\n".
                '  from tuserprofile'."\n".
                ' where ic_user1 = '."'$user_id'"."\n";

2 Answers 2

3

You have single quotes within your single quoted string.

Single quotes within a quote string must be escaped with \'.

However, you would be better off using the multi-line quoting syntax for cleaner code:

$sql = <<EOF;
select rtrim(f_admin_disabled),
       convert(varchar,t_password,101),
       rtrim(c_email),
       str_replace(rtrim(c_manager),'''','_'),
       rtrim(c_mgr_email)
    from tuserprofile
    where ic_user1 = '$user_id'
EOF
;

This has the intended result without messy escaping and string concatenation.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to escape the single quotes in your variable assignment:

my $user_id = 'test';
my $sql = 'select rtrim(f_admin_disabled),'."\n".
            '       convert(varchar,t_password,101),'."\n".
            '       rtrim(c_email),'."\n".
            '       str_replace(rtrim(c_manager),\'\'\'\',\'_\'),'."\n".
            '       rtrim(c_mgr_email)'."\n".
            '  from tuserprofile'."\n".
            ' where ic_user1 = '."'$user_id'"."\n";

print $sql;

results in :

   select rtrim(f_admin_disabled),
   convert(varchar,t_password,101),
   rtrim(c_email),
   str_replace(rtrim(c_manager),'''','_'),
   rtrim(c_mgr_email)
   from tuserprofile
   where ic_user1 = 'test'

Check if that is the result you expected.

3 Comments

Canot skip the sinle quoted variable arugument...throws error Incorrect syntax near 'Leary'. (SQL-42000) [state was 42000 now HY000] [MERANT][ODBC Sybase ASE driver][SQL Server]Unclosed quote before the character string ' help for another solution
What if you just write last line as: ' where ic_user1 = '.$user_id."\n"; You can print the output from your perl program run on your DB and tweak your variable assignment so that it remains a valid perl script and emits correct DB query.
str_replace(rtrim(c_manager),\'\'\'\',\'_\'),'."\n" these lines throws the same error canot skip the single quoted variable arugument..... ex :in the c_manager column values are 'S'achin' inside the single quotes variable name replaces script ie.,S_achin as i want.....help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.