I'm trying to do this function:
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;
I want to make a function to join two columns from tables "SD_PRJ".tbl_project and "SD_PRJ".tbl_project_timesheet and make the compare SUM(fld_allocated_time) / 24 < fld_allocated_days
from an input parameter p_project_id (WHERE t.fld_project_id = p_project_id) and make the insert if its ok or not.
The input column fld_project_id can have more than 1 appear in the table.
tbl_ = table name
fld_ = column / field name
"SD_PRJ" = my schema name
\ERROR: syntax error at or near "INTO"
Error:
LINE 12: INSERT INTO "SD_PRJ".tbl_project_timesheet(fld_emp_id, fl... ^
********** Error **********
ERROR: syntax error at or near "INTO" SQL state: 42601 Character: 550