I have an old system where the database is Oracle and the central business logic is in the DB, and now I need to move to Postgres DB.
I have a function that works to return an updated statement in Oracle.
I moved to Postgres, so I converted my Oracle schema to Postgres using Ora2Pg
When I run the function in Postgres, it returns an error as Statment not recognized.
My Code:
Function Create_Update_Statement(pMId Number)
return Clob is
Cursor Cursor_Salary_Formula(pId Number) is
Select s.*
from Salary_Formula_d s
where s.mid = pId ;
Update_Statement Clob;
type Salary_Formula_D_tabletype is table of Salary_Formula_d%rowtype index by varchar2(200);
Salary_Formula_table Salary_Formula_D_tabletype;
Begin
Update_Statement := null;
for Salary_Formula_D_Rec in Cursor_Salary_Formula(pMId) loop
if Update_Statement is null then
Update_Statement := 'Update Salary Set ' || Salary_Formula_D_Rec.Fieldname || ' = ';
else
Update_Statement := Update_Statement || ' , ' || Salary_Formula_D_Rec.Fieldname || ' = ';
end if;
if upper(Salary_Formula_D_Rec.Data_Type) =
'DATE' THEN
Update_Statement := Update_Statement ||
'TO_DATE(''' ||
Salary_Formula_Table(pMId)(Salary_Formula_D_Rec.fieldname).val_string ||
''',''DD/MM/YYYY'')';
elsif upper(Salary_Formula_D_Rec.Data_Type) =
'VARCHAR2' THEN
Update_Statement := Update_Statement || '''' ||
Salary_Formula_Table(pMId)(Salary_Formula_D_Rec.fieldname).val_string || '''';
END IF;
end loop;
return(Update_Statement);
end;
The error is the statement:
Salary_Formula_Table(pMId)(Salary_Formula_D_Rec.fieldname).val_string
The structure of the table SALARY_FORMULA_D is
create table SALARY_FORMULA_D
(
id NUMBER not null,
mid NUMBER,
fieldname VARCHAR2(200),
formula VARCHAR2(4000),
val_number NUMBER,
val_string VARCHAR2(500),
val_date DATE,
fieldname_ar VARCHAR2(400),
fieldname_en VARCHAR2(400),
data_type VARCHAR2(100)
)
Any help will be appreciated
Rewrite the function in a way that works with Postgres and utilizes its array capabilities. Try the following code;