Problem running db2 commands in awk

Hi ,

I am trying to use the below in awk but failed, any one assist please,

 awk '{ print $0; 
 if ( $0 ~ /LOADTMP1/ ) 
 { 
 print $4;
 Table_name=system($( db2 -x "SELECT TRIM(TD.BSCHEMA) || '.' || TRIM(TD.BNAME) AS TABLE_NAME FROM SYSCAT.TABDEP TD WHERE 
TD.BTYPE='T' AND TD.TABSCHEMA='XXXXXXX' AND  TD.TABNAME='$view' order by BSCHEMA desc fetch first 1 row only ));
 print $Table_name;
 }
 }' test
  
  1. I have tried replacing ' with '\'' but no help
    please assist.

Please use code tags as required by forum rules!

This is a hell of a quoting problem. Try

awk -v SQ="'" '
1
/LOADTMP1/      {print $4;
                 Table_name=system("db2 -x \"SELECT TRIM(TD.BSCHEMA) || " SQ "." SQ " || TRIM(TD.BNAME) AS TABLE_NAME FROM SYSCAT.TABDEP TD WHERE TD.BTYPE=" SQ "T" SQ " AND TD.TABSCHEMA=" SQ "XXXXXXX" SQ " AND TD.TABNAME=" SQ "$view" SQ " order by BSCHEMA desc fetch first 1 row only\"")
                 print Table_name;
                }
'  test

You may want to echo that sql string to verify it's correctly spelled...

Hi, Thanks for your reply.

But this code returns "1" instead of table name.