SQL查询错误插入

时间:2014-11-04 22:03:50

标签: sql postgresql aggregate-functions postgresql-9.1

我正在尝试执行此功能:

CREATE OR REPLACE FUNCTION "SA_PRJ".usp_verify_allocated_time(p_uid integer, p_project_id integer,   p_allocated_time numeric, p_achieved_time numeric, p_task_desc character varying, p_obs character  varying, p_date timestamp without time zone)
RETURNS void AS

$BODY$
declare alloc_id integer;

BEGIN
   if not "SA_ADM".usp_check_permission(p_uid, 'SA_PRJ', 'usp_add_timesheet_record') then
    raise exception 'User ID % nu are permisii pentru aceasta operatie!', p_uid;
   end if;
SELECT *,
   case when SUM(fld_allocated_time) / 24 < fld_allocated_days 
then 
   INSERT INTO "SD_PRJ".tbl_project_timesheet(fld_emp_id, fld_project_id,        fld_is_allocated,fld_allocated_time, fld_achieved_time,    fld_task_desc, fld_obs)
    VALUES (p_uid,p_project_id,coalesce(alloc_id,0), p_allocated_time, p_achieved_time,p_task_desc, p_obs);

else 'Not OK' 
end as Alocated
from
(
  SELECT p.fld_id, p.fld_allocated_days, t.fld_allocated_time
  FROM "SD_PRJ".tbl_project p
  INNER JOIN "SD_PRJ".tbl_project_timesheet t
  ON p.fld_id=t.fld_id
  where t.fld_project_id = p_project_id
)AS Alias
GROUP BY fld_id, fld_allocated_days, fld_allocated_time
END;

$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION "SA_PRJ".usp_usp_verify_allocated_time(integer, integer, numeric, numeric, character varying,   character varying, timestamp without time zone)
OWNER TO postgres;

我想创建一个函数来连接表"SD_PRJ".tbl_project"SD_PRJ".tbl_project_timesheet中的两列并进行比较SUM(fld_allocated_time) / 24 < fld_allocated_days 从输入参数p_project_id (WHERE t.fld_project_id = p_project_id)开始,如果可以,则进行插入。

输入列fld_project_id可以在表中显示多于1个。

tbl_ = table name
fld_ = column / field name

"SD_PRJ" = my schema name
\ERROR:  syntax error at or near "INTO"

错误:

  

第12行:插入“SD_PRJ”.tbl_project_timesheet(fld_emp_id,fl ...                  ^

     

**********错误**********

     

错误:“INTO”SQL状态或附近的语法错误:42601字符:550

1 个答案:

答案 0 :(得分:2)

是的,您正试图在insert声明中写一个select

你可能想要的是这样的:

INSERT INTO "SD_PRJ".tbl_project_timesheet(fld_emp_id, fld_project_id,        fld_is_allocated,fld_allocated_time, fld_achieved_time,    fld_task_desc, fld_obs)
select p_uid,p_project_id,coalesce(alloc_id,0), p_allocated_time, p_achieved_time,p_task_desc, p_obs
from
(
  SELECT p.fld_id, p.fld_allocated_days, t.fld_allocated_time
  FROM "SD_PRJ".tbl_project p
  INNER JOIN "SD_PRJ".tbl_project_timesheet t
  ON p.fld_id=t.fld_id
  where t.fld_project_id = p_project_id
)AS Alias
GROUP BY fld_id, fld_allocated_days, fld_allocated_time
having SUM(fld_allocated_time) / 24 < fld_allocated_days

不完全确定一切是否正确,但这是我认为的一般要点。