PGDAC - Postgres - Create Function
Posted: Thu 28 Nov 2013 15:37
Hi,
I am trying to create a function with pgdac itself instead of running the script directly in sql (for example with pgAdmin). I need to do this because, I need to do data base version control, so for example when my software starts, it looks to the da base the version number and creates the new objects if has an old version.
But the following code gives me an error,
“Cannot insert multiple commands into a prepare statement”.
I can create/change tables, but cannot create/change functions. Is there a way I can do that?
/////////////////////////////////////////////////////////////////////
String sScript;
sScript = " CREATE OR REPLACE FUNCTION Test1(p_site_id integer, p_master_id integer, p_master_desc text, p_host_con_string text, p_port_con_string text, p_type text, p_export_type text, p_file_name text, b_down_cons boolean)";
sScript = sScript + " RETURNS boolean AS $BODY$ declare v_count integer; begin";
sScript = sScript + " BEGIN";
sScript = sScript + " select count(*) into v_count from masters where site_id = p_site_id and master_ids = p_master_id;";
sScript = sScript + " if(v_count>0) then";
sScript = sScript + " update masters set master_desc = p_master_desc, host_con_string = p_host_con_string, port_con_string = p_port_con_string, type = p_type, down_cons_enabled = b_down_cons, default_export_type = p_export_type, file_name = p_file_name, change_date = current_date+current_time";
sScript = sScript + " where site_id = p_site_id and master_ids = p_master_id;";
sScript = sScript + " end if;";
sScript = sScript + " exception when others then return false; END;";
sScript = sScript + " return true; end;";
sScript = sScript + " $BODY$ LANGUAGE plpgsql VOLATILE COST 100;";
sScript = sScript + " ALTER FUNCTION Test1(integer, integer, text, text, text, text, text, text, boolean)";
sScript = sScript + " OWNER TO postgres;";
pgTable->SQL->SetText(sScript.c_str());
pgTable->Execute();
/////////////////////////////////////////////////////////////////////////
I am trying to create a function with pgdac itself instead of running the script directly in sql (for example with pgAdmin). I need to do this because, I need to do data base version control, so for example when my software starts, it looks to the da base the version number and creates the new objects if has an old version.
But the following code gives me an error,
“Cannot insert multiple commands into a prepare statement”.
I can create/change tables, but cannot create/change functions. Is there a way I can do that?
/////////////////////////////////////////////////////////////////////
String sScript;
sScript = " CREATE OR REPLACE FUNCTION Test1(p_site_id integer, p_master_id integer, p_master_desc text, p_host_con_string text, p_port_con_string text, p_type text, p_export_type text, p_file_name text, b_down_cons boolean)";
sScript = sScript + " RETURNS boolean AS $BODY$ declare v_count integer; begin";
sScript = sScript + " BEGIN";
sScript = sScript + " select count(*) into v_count from masters where site_id = p_site_id and master_ids = p_master_id;";
sScript = sScript + " if(v_count>0) then";
sScript = sScript + " update masters set master_desc = p_master_desc, host_con_string = p_host_con_string, port_con_string = p_port_con_string, type = p_type, down_cons_enabled = b_down_cons, default_export_type = p_export_type, file_name = p_file_name, change_date = current_date+current_time";
sScript = sScript + " where site_id = p_site_id and master_ids = p_master_id;";
sScript = sScript + " end if;";
sScript = sScript + " exception when others then return false; END;";
sScript = sScript + " return true; end;";
sScript = sScript + " $BODY$ LANGUAGE plpgsql VOLATILE COST 100;";
sScript = sScript + " ALTER FUNCTION Test1(integer, integer, text, text, text, text, text, text, boolean)";
sScript = sScript + " OWNER TO postgres;";
pgTable->SQL->SetText(sScript.c_str());
pgTable->Execute();
/////////////////////////////////////////////////////////////////////////