我有json并使用postgresql存储过程将其保存在不同的表中

时间:2015-06-05 15:15:37

标签: json postgresql stored-procedures insert

这是我的json:

[
  {
    "user_id": "",
    "personal_detail": {
      "first_name": "",
      "last_name": "",
      "dob": "",
      "gender": "",
      "phone": "",
      "location": "",
      "skypeid": ""
    },
    "experience_detail": {
      "designation_id": "",
      "hotel_id": "",
      "group_id": "",
      "location_id": "",
      "time_from": "",
      "time_to": "",
      "current_check": ""
    },
    "eduction_detail": {
      "0": {
        "degree_id": "",
        "university_id": "",
        "area_of_study": "",
        "location_id": "",
        "completed_year": ""
      },
      "1": {
        "degree_id": "",
        "university_id": "",
        "area_of_study": "",
        "location_id": "",
        "completed_year": ""
      }
    },
    "course_detail": {
      "independent_course_name": "",
      "independent_course_institution": "",
      "location_id": "",
      "independent_courses_completed_year": ""
    },
    "refrence_detail": {
      "first_name": "",
      "last_name": "",
      "company_name": "",
      "contact_no": ""
    },
    "preferred_position": {
      "position_id": ""
    },
    "preferred_location": {
      "location_id": ""
    },
    "preferred_hotel": {
      "hotel_id": ""
    },
    "preferred_group": {
      "group_id": ""
    },
    "salary_detail": {
      "current_salary": "",
      "expected_salary": ""
    },
    "award_detail": {
      "award_title": "",
      "awarded_company": "",
      "award_year": ""
    }
  }
]

这是我解析这种类型的json后得到的。个人信息保存在个人表中:

$personal_detail[]=array('10','jhon','thomas','1985-02-08','9694969496','public','121','abcd','abc.jpg');
$personal_detail[]=array('11','shiva','shankar','1989-03-09','9694936666','friends','123','xyz','xyz.jpg');
$sql="SELECT * from test_info(ARRAY[$personal_detail])";

这是我的程序我想使用postgresql中的存储过程在表中保存这个数组数据:

CREATE OR REPLACE FUNCTION test_info(personel_array personal_detail[])
  RETURNS character varying AS
$BODY$
    DECLARE
        personel_element  personal_detail;
    BEGIN
        FOREACH personel_element IN ARRAY personel_array
        LOOP
            INSERT INTO personel_detail(
                  user_id,
                  first_name,
                  last_name,
                  gender,
                  dob,
                  phone,
                  visiblity,
                  location_id,
                  skypeid,
                  profile_image
            ) VALUES(
                personel_element.user_id,
                personel_element.first_name,
                personel_element.last_name,
                personel_element.gender,
                personel_element.dob,
                personel_element.phone,
                personel_element.visiblity,
                personel_element.location_id,
                personel_element.skypeid,
                personel_element.profile_image
            );
        END LOOP;
        RETURN 'OK';
    END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION test_info(personal_detail[])
  OWNER TO postgres;

0 个答案:

没有答案