Search code examples
postgresqlplpgsql

Invalid input syntax for integer POSTGRESQL


With ora2pg tool I converted my oracle func to the next function :

CREATE OR REPLACE FUNCTION Control_Reports_Pg.control_reports_fn (P_Report_Type bigint, P_Log_File_Name text,C_Path text) RETURNS bigint AS 
  $body$
  DECLARE

   V_Return smallint;
   V_Function_Return smallint:=1;
   C_Daily_Reports varchar(300);

    C_Function_Name    varchar(200) := 'Control_Reports_Fn';
    Rec_Daily_Reports  CONTROL_REPORTS%ROWTYPE;
    G_Log_File_Type    UTL_FILE.FILE_TYPE;

   BEGIN

    -- Open Log File
    --PERFORM  control_reports_pg.send_error_mail(C_Path ,C_Function_Name);
    G_Log_File_Type := UTL_FILE.FOPEN(C_Path, P_Log_File_Name,'w');

    C_Daily_Reports := 'SELECT REPORT_ORDER,PROCEDURE_NAME,DIRECTORY_NAME,FILE_NAME,TITLE FROM CONTROL_REPORTS WHERE RUN_FLAG=1 AND REPORT_TYPE=' || P_Report_Type || ' ORDER BY REPORT_ORDER';

   -- RAISE NOTICE '%',C_Daily_Reports;
    FOR Rec_Daily_Reports IN EXECUTE C_Daily_Reports LOOP


   PERFORM UTL_FILE.PUT_LINE(G_Log_File_Type,'Procedure_Name = '||Rec_Daily_Reports.Procedure_Name);
   PERFORM UTL_FILE.PUT_LINE(G_Log_File_Type,'start time= '|| to_char(clock_timestamp(),'dd/mm/yyyy hh24:mi:ss'));
   V_Return := control_reports_pg.chrg_in_bill_not_in_crm_fn(Rec_Daily_Reports.Directory_Name,
                                               Rec_Daily_Reports.File_Name,
                                               Rec_Daily_Reports.Title);


        IF V_Return = 0 THEN
           V_Function_Return := 0;
        END IF;
       ............

When I connect to psql and I summon the function chrg_in_bill_not_in_crm_fn with the relevant arguments I don't get any errors. However, when I summon the function control_reports_fn I get the next error :

   mydb=> select Control_Reports_Pg.control_reports_fn  (arg1,arg2,arg3);
   NOTICE:  FUNC : Control_Reports_Fn, SQLERRM: invalid input syntax for integer: "Chrg_In_Bill_Not_In_Crm_Fn"
   CONTEXT:  PL/pgSQL function control_reports_pg.daily_control_reports_fn() line 9 at assignment
   NOTICE:  Message : invalid input syntax for integer: "Chrg_In_Bill_Not_In_Crm_Fn", Func : Control_Reports_Fn
   CONTEXT:  SQL statement "SELECT control_reports_pg.send_error_mail(SQLERRM ,C_Function_Name)"
   PL/pgSQL function control_reports_pg.control_reports_fn(bigint,text,text) line 339 at PERFORM
   PL/pgSQL function control_reports_pg.daily_control_reports_fn() line 9 at 
   assignment
    daily_control_reports_fn
    --------------------------
                    1

*Inside Chrg_In_Bill_Not_In_Crm_Fn I run in a loop over result of an sql execution (when I run this sql in psql It works - no errors) and I write with UTL to a file.

Can someone answer me the next questions :

1)Why I'm getting that error ? When I summon the func Chrg_In_Bill_Not_In_Crm_Fn in psql I don't get any errors- just getting the next output :

    chrg_in_bill_not_in_crm_fn
    ----------------------------
                      0

I realized that in the func control_reports_fn the loop doesn't start. However, when I run the select that I have there in psql I got rows that came back. Why doesn't it enter the loop and why I got that error ?

2)As you can see I write to a file in the filesystem with UTL_FILE. However, I don't see that the files are created. Is it because I get an error or is it something else ?


Solution

  • My problem was that the variable Rec_Daily_Reports was of type %ROWTYPE and I tried to select specific columns.