从php发送字符串数组到postgres函数作为参数

时间:2015-09-16 08:21:26

标签: php arrays postgresql

我正在尝试调用postgresql函数来更新表。当我使用SQL编辑器运行以下查询时,它完美地运行

SELECT document.update_doc_attrib_on_opportunity(
    8, 'OPP', 1069701, array['OWNER','SOURCE','PRIORITY'], array['ERP','Unknown ERP','Medium']
);

但是当我尝试从像这样的php结尾那样调用它时

$query_insert_use_doc_attribs = 
$dbconn->prepare("SELECT document.update_doc_attrib_on_opportunity(
    8, 'OPP', 1069701, array['OWNER','SOURCE','PRIORITY'], array['ERP','Unknown ERP','Medium']
  )
");
$query_insert_use_doc_attribs->execute();

它不起作用,给出错误代码25P02 => "错误:当前事务中止,命令被忽略直到事务块结束"。

我想我是以错误的格式发送数组,但不确定。

准确地说,这是我的功能代码 -

CREATE OR REPLACE FUNCTION document.update_doc_attrib_on_opportunity(p_org_id numeric, p_document_code character varying, p_document_no numeric, p_doc_attrib_code character varying[], p_doc_attrib_value character varying[])
RETURNS integer AS
$BODY$
DECLARE
  v_doc_no integer;
  v_doc_column character varying;
  v_use_doc_material_col_name character varying;
  v_query_string character varying;
  -- v_query_string1 character varying;
  no_of_docs integer;
  owner_id_attrib character varying;
  v_user_id integer;
  i integer; 

BEGIN

  FOR i IN 1 .. array_upper(p_doc_attrib_code, 1)
  LOOP
    v_query_string := 'UPDATE use_doc_attribute
                  SET attrib_value = ''' || p_doc_attrib_value[i] || '''
                WHERE org_id = ' || p_org_id || '
                      AND document_no = ' || p_document_no || '
                      AND document_code = ''' || p_document_code || '''
                      AND attrib_code = ''' || p_doc_attrib_code[i] || '''';
    EXECUTE v_query_string;
  END LOOP;

  RETURN p_document_no;

  EXCEPTION
    WHEN OTHERS THEN
        RAISE EXCEPTION USING ERRCODE=SQLSTATE,MESSAGE=SQLERRM;

END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
  ALTER FUNCTION document.update_doc_attrib_on_opportunity(numeric, character varying, numeric, character varying[], character varying[])
  OWNER TO "ERP";

0 个答案:

没有答案