PostgreSQL-从嵌套的hstore中选择,然后删除

时间:2018-10-16 03:44:36

标签: hstore

下面的代码可以工作,但是有更好的方法吗?删除的语法是什么?

谢谢,我的主机背景已经显示。

      v_result          boolean; 
      v_all_skills      hstore [];
      v_skill           hstore;
      v_skill_details   hstore;
      v_num_skills      integer;
      v_ii              integer;
      v_found           boolean;


    raise notice 'BEFORE (%)', v_all_skills; 

        select array_length(v_all_skills,1 ) into v_num_skills;

        v_found = FALSE;
        FOR v_ii IN 1..v_num_skills LOOP
        v_skill = v_all_skills[v_ii];
            v_found = v_skill -> 'skill_id' = v_skill_id;
        IF v_found THEN 
            v_skill_details = v_skill -> 'skill_details';

    **-- here I need to delete the selected v_skill from v_all_skills for the given skill_id**

            EXIT; 
            END IF;
        END LOOP;

raise notice 'v_found (%)', v_found;    
raise notice 'AFTER v_all_skills    (%)', v_all_skills; 
raise notice 'AFTER v_skill_details (%)', v_skill_details;

注意::在此之前({“ \” skill_id \“ => \” aa000001 \“,\” skill_details \“ => \” \\“ skill_level \\” => \\“ 5 \\“,\\” level_confirmed_by = \\“ true \\”,\\“ level_confirmed_by \\” => \\“ 624 \\” \“”})) 注意:v_found(t) 注意:之后v_all_skills \ n({“ \” skill_id \“ => \” aa000001 \“,\” skill_details \“ => \” \\“ skill_level \\” => \\“ 5 \\“,\\” level_confirmed_by = \\“ true \\”,\\“ level_confirmed_by \\” => \\“ 624 \\” \“”})) 注意:在v_skill_details之后(“ skill_level” =>“ 5”,“ level_confirmed” =>“ true”,“ level_confirmed_by” =>“ 624”)

1 个答案:

答案 0 :(得分:0)

好的,我已经对其进行了重新设计,以便通过新的hstore v_all_skills_new

获得所需的结果。
FOR v_ii IN 1..v_num_skills LOOP
    v_skill = v_all_skills[v_ii];
    v_found = v_skill -> 'skill_id' = v_skill_id;

    CASE
   WHEN (v_action = 1 OR v_action = 2) THEN -- insert/update the a skill
        IF  NOT v_found THEN                -- keep existing skill before inserting the new one
            v_all_skills_new = array_append(v_all_skills_new , v_skill);
        END IF;
        -- build the new/updated skill from input
        v_skill_details  = hstore(ARRAY['skill_level','level_confirmed','level_confirmed_by'],
                                      ARRAY[v_skill_level::TEXT, v_skill_level_confirmed::TEXT, v_skill_level_confirmed_by::TEXT]);
        v_skill          = hstore(ARRAY['skill_id', 'skill_details'],
                              ARRAY[v_skill_id::TEXT, v_skill_details::TEXT]);

        v_all_skills_new = array_append(v_all_skills_new, v_skill);    -- insert new/updated skill

   WHEN v_action = 3 THEN                   -- delete(i.e. do not move to new array) skill if found
        IF  NOT v_found THEN                -- keep existing skill before inserting the new one
            v_all_skills_new = array_append(v_all_skills_new , v_skill);
        END IF;
    END CASE;

END LOOP;