如何在不提供参数的情况下增加值

时间:2013-10-16 09:36:45

标签: mysql sql postgresql postgresql-9.2

每当创建新会议时,如何增加会议和子会议ID的值

表:“员工”

CREATE TABLE employee 
(
  sno SERIAL,
  emp_id INTEGER,
  emp_name TEXT,
  supervisor TEXT,
  designation TEXT,
  department TEXT,
  dob DATE,
  off_ph BIGINT,
  mob_ph BIGINT,
  home_ph BIGINT,
  off_mail TEXT,
  pers_mail TEXT
);

表:“会议”

CREATE TABLE meeting
(
  sno SERIAL NOT NULL,
  meet_id INTEGER,
  submeet_id INTEGER,
  title TEXT,
  agenda TEXT,
  venue TEXT,
  date DATE,
  list TEXT,
  list_of_role TEXT,
  CONSTRAINT meeting_pk PRIMARY KEY (sno)
);

表:“重复发生”

CREATE TABLE recurrence
(
  sno SERIAL,
  meet_id INTEGER,
  submeet_id INTEGER,
  start_date DATE,
  start_time TIME WITH TIME ZONE,
  end_time TIME WITH TIME ZONE,
  repeat NUMERIC,
  remainder NUMERIC,
  range CHARACTER VARYING (30)
);

表:“与会者”

CREATE TABLE attendee
(
  sno SERIAL,
  meet_id INTEGER,
  submeet_id INTEGER,
  emp_name TEXT,
  status TEXT DEFAULT 'Pending' :: TEXT,
  role TEXT,
  reason TEXT DEFAULT ' ' :: TEXT
 );

表:“行动”

CREATE TABLE actions
(
  sno SERIAL,
  emp_id INTEGER,
  emp_name TEXT,
  action TEXT,
  action_status TEXT DEFAULT 'Pending' :: TEXT
);

插入表格“员工”

CREATE OR REPLACE FUNCTION insert_into_employee(_emp_id INTEGER, _emp_name TEXT, _supervisor TEXT, _designation TEXT, _department TEXT, _dob DATE, _off_ph BIGINT, _mob_ph BIGINT, _home_ph BIGINT, _off_mail TEXT, _pers_mail TEXT)
  RETURNS void AS
$BODY$
BEGIN

    INSERT INTO employee(emp_id,emp_name,supervisor,designation,department,dob,off_ph,mob_ph,home_ph,off_mail,pers_mail)
      VALUES (_emp_id,_emp_name,_supervisor,_designation,_department,_dob,_off_ph,_mob_ph,_home_ph,_off_mail,_pers_mail);


    PERFORM insert_into_actions_and_attendee(_emp_id, _emp_name);

END
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

插入表格“actions”和“与会者”emp_id和emp_name(此函数从“insert_employee”调用)

CREATE OR REPLACE FUNCTION insert_into_actions_and_attendee(_emp_id INTEGER, _emp_name TEXT)
  RETURNS void AS
$BODY$
BEGIN

  INSERT INTO actions (emp_id) VALUES (_emp_id);
  INSERT INTO attendee (emp_name) VALUES (_emp_name);

END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

创建新会议

CREATE OR REPLACE FUNCTION create_meet(_meet_id INTEGER, _submeet_id INTEGER, _title TEXT, _agenda TEXT, _venue TEXT, _date DATE, _list TEXT, _list_of_role TEXT, _start_date DATE, _start_time TIME WITH TIME ZONE, _end_time TIME WITH TIME ZONE, _repeat NUMERIC, _remainder NUMERIC, _range CHARACTER VARYING(30))
  RETURNS void AS
$BODY$
DECLARE

    _meet_id INTEGER;
    _submeet_id INTEGER := 1;

BEGIN

    _meet_id := ((SELECT MAX (meet_id) FROM meeting));
    _meet_id := coalesce (_meet_id, 0) + 1;

    INSERT INTO meeting(meet_id, submeet_id, title, agenda, venue, date, list, list_of_role)
      VALUES(_meet_id, _submeet_id, _title, _agenda, _venue, _date, _list, _list_of_role);

    WITH cte AS(
       SELECT _meet_id AS meet_id, _submeet_id AS submeet_id, regexp_split_to_table(_list, ';') AS participant_name, regexp_split_to_table(_list_of_role,';') AS participant_role
      )

    INSERT INTO attendee(meet_id,submeet_id,emp_name, role)
    SELECT meet_id, submeet_id, participant_name, participant_role
    FROM cte
    WHERE meet_id=_meet_id AND submeet_id=_submeet_id AND participant_name IS NOT NULL AND participant_name <> '' AND participant_role IS NOT NULL AND participant_role <> '' GROUP BY meet_id,submeet_id,participant_name,participant_role;

    INSERT INTO recurrence(meet_id, submeet_id, start_date, start_time, end_time, repeat, remainder, range)
    VALUES(_meet_id, _submeet_id, _start_date, _start_time, _end_time, _repeat, _remainder, _range);

END
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

创建子会议

CREATE OR REPLACE FUNCTION create_sub_meet(_meet_id INTEGER, _submeet_id INTEGER, _title TEXT, _agenda TEXT, _venue TEXT, _date DATE, _list TEXT, _list_of_role TEXT, _start_date DATE, _start_time TIME WITH TIME ZONE, _end_time TIME WITH TIME ZONE, _repeat NUMERIC, _remainder NUMERIC, _range CHARACTER VARYING(30))
  RETURNS void AS
$BODY$
DECLARE

    _meet_id INTEGER  := (SELECT MAX(meet_id) FROM meeting);
    _submeet_id INTEGER;

BEGIN

    _submeet_id := ((SELECT MAX (submeet_id) FROM meeting WHERE meet_id=_meet_id));
    _submeet_id := coalesce (_submeet_id, 0) + 1;

    INSERT INTO meeting (meet_id, submeet_id, title, agenda, venue, date, list, list_of_role)
      VALUES (_meet_id, _submeet_id, _title, _agenda, _venue, _date, _list, _list_of_role);

    WITH cte AS (
       SELECT _meet_id AS meet_id, _submeet_id AS submeet_id, regexp_split_to_table (_list, ';') AS participant_name, regexp_split_to_table (_list_of_role,';') AS participant_role
      )

    INSERT INTO attendee (meet_id, submeet_id, emp_name, role)
    SELECT meet_id, submeet_id, participant_name, participant_role
    FROM cte
    WHERE meet_id=_meet_id AND submeet_id=_submeet_id AND participant_name IS NOT NULL AND participant_name <> '' AND participant_role IS NOT NULL AND participant_role <> '' GROUP BY meet_id,submeet_id,participant_name,participant_role;

    INSERT INTO recurrence (meet_id, submeet_id, start_date, start_time, end_time, repeat, remainder, range)
    VALUES (_meet_id, _submeet_id, _start_date, _start_time, _end_time, _repeat, _remainder, _range);

END
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

将新参与者添加到同一会议

CREATE OR REPLACE FUNCTION add_participant_to_same_meet(_meet_id INTEGER, _submeet_id INTEGER, _emp_id INTEGER, _emp_name TEXT, _role TEXT, _action TEXT)
  RETURNS void AS
$BODY$
BEGIN
    UPDATE meeting SET list=CONCAT (list, _emp_name) WHERE meet_id=_meet_id AND submeet_id=_submeet_id;
    UPDATE meeting SET list_of_role=CONCAT (list_of_role, _role) WHERE meet_id=_meet_id AND submeet_id=_submeet_id;

    WITH abc AS (
       SELECT _meet_id AS meet_id, _submeet_id AS submeet_id, _emp_name AS emp_name, _role AS role, _action AS action)

    INSERT INTO attendee (meet_id, submeet_id, emp_name, role)
    SELECT meet_id, submeet_id, emp_name, role
    FROM abc
    WHERE meet_id=_meet_id AND submeet_id=_submeet_id;


    WITH def AS (
       SELECT _emp_id AS emp_id, _emp_name AS emp_name, _action AS action)

    INSERT INTO actions(emp_id, emp_name, action)
    SELECT emp_id, emp_name, action
    FROM def
    WHERE emp_id=_emp_id;

END
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

2 个答案:

答案 0 :(得分:1)

您需要使列类型为SERIAL这是postgres中的自动递增整数字段。

以下是SQLFiddle

的示例
CREATE TABLE meeting
(
  sno SERIAL NOT NULL,
  meet_id serial, --note serial rather than int
  submeet_id serial, --note serial rather than int
  title TEXT,
  agenda TEXT,
  venue TEXT,
  date DATE,
  list TEXT,
  list_of_role TEXT,
  CONSTRAINT meeting_pk PRIMARY KEY (sno)
);

答案 1 :(得分:0)

我不明白你的问题,似乎没有完全解释,但我们与我的口袋telepathist进行了一些讨论,我决定建议你使用触发器。触发器可以对任何事件做出反应,例如插入,删除或更新记录,并在执行操作之前或之后进行一些操作。我希望它会有所帮助,但不会相信,因为,我再说一遍,我无法完全理解你的问题。