我在插入时在hstore变量上遇到语法错误

时间:2018-10-09 06:46:13

标签: postgresql plpgsql postgresql-9.2 hstore

CREATE EXTENSION hstore;

CREATE TABLE skm_employee_skillset
(
    employee_id                   integer PRIMARY KEY,
    created_date                  DATE DEFAULT current_date,
    last_update                   DATE DEFAULT current_date,
    created_by                    integer NOT NULL,
    updated_by                    integer NOT NULL,
    employee_skill_values         hstore
);

CREATE OR REPLACE FUNCTION skm_skills_manager__employee_skillset_insert(
                v_employee_id           integer,
                v_user              integer,
                v_skill_type_prefix         char(2),
                v_skill_type_id             integer,
                v_skill_level_confirmed         boolean,
                v_skill_level_confirmed_by      integer )

  RETURNS boolean AS '

BEGIN    

    Insert into skm_employee_skillset
        (employee_id ,
        created_date,
        last_update ,
        created_by,
        updated_by,
        employee_skill_values)
    VALUES
        (v_employee_id,
        DEFAULT,
        DEFAULT,
        v_user,
        v_user,
        '"skill_type_prefix" => v_skill_type_prefix,
         "skill_type_id" => v_skill_type_id,
         "skill_level_confirmed"    => v_skill_level_confirmed, 
         "skill_level_confirmed_by" => v_skill_level_confirmed_by' );

    return TRUE; 
EXCEPTION
    when others then return FALSE;

end;' language 'plpgsql';
  

错误:“ skill_type_prefix”或附近的语法错误   LINE 508:““ skill_type_prefix” => v_skill_type_prefix,                   ^ **

2 个答案:

答案 0 :(得分:0)

当代码块由单引号引起来时,您不能在代码块本身内部使用未加引号的单引号,因为单引号不会嵌套

最简单的解决方案是对代码主体使用特定的dollar-quoting分隔符,例如

CREATE FUNCTION name(arguments...) RETURNS ...
AS $BODY$

   ...
   ... body of the function
   ...

 $BODY$ language plpgsql;

这部分解决之后,您就可以将hstore的语法作为字符串文字值集中处理,可以自由地使用单引号将其括起来。

答案 1 :(得分:0)

CREATE EXTENSION hstore;


CREATE TABLE skm_employee_skillset
(
    employee_id                   integer PRIMARY KEY,
    created_date                  DATE DEFAULT current_date,
    last_update                   DATE DEFAULT current_date,
    created_by                    integer NOT NULL,
    updated_by                    integer NOT NULL,
    employee_skill_values         hstore
);

CREATE OR REPLACE FUNCTION skm_skills_manager__employee_skillset_insert(
                v_employee_id           integer,
                v_user              integer,
                v_skill_type_prefix         char(2),
                v_skill_type_id             integer,
                v_skill_level_confirmed         boolean,
                v_skill_level_confirmed_by      integer )
RETURNS boolean AS $$
BEGIN    

    Insert into skm_employee_skillset
        (employee_id ,
        created_date,
        last_update ,
        created_by,
        updated_by,
        employee_skill_values)
    VALUES
        (v_employee_id,
        DEFAULT,
        DEFAULT,
        v_user,
        v_user,
        hstore(ARRAY['skill_type_prefix','skill_type_id','skill_level_confirmed','skill_level_confirmed_by']
             , ARRAY[v_skill_type_prefix::TEXT,v_skill_type_id::TEXT,v_skill_level_confirmed::TEXT,v_skill_level_confirmed_by::TEXT]) );

    return TRUE; 
EXCEPTION
    when others then return FALSE;
end;
$$ language plpgsql;


select skm_skills_manager__employee_skillset_insert(4,2,'aa',4,true,2);

select * from skm_employee_skillset;



employee_id |created_date |last_update |created_by |updated_by |employee_skill_values                                                                                             |
------------|-------------|------------|-----------|-----------|------------------------------------------------------------------------------------------------------------------|
4           |2018-10-09   |2018-10-09  |2          |2          |"skill_type_id"=>"4", "skill_type_prefix"=>"aa", "skill_level_confirmed"=>"true", "skill_level_confirmed_by"=>"2" |