1

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

2
  • Insert into its the error sorry for mistake :) Commented Nov 4, 2014 at 22:06
  • Your error is because you cannot insert inside a select statement. Instead try putting your case value in a variable and determine if you should insert based on the variable value in an if statement. Commented Nov 4, 2014 at 22:13

1 Answer 1

2

Yeah, you're trying to write an insert inside a select statement.

What you probably want is something like this:

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

Not completely sure if everything there is right, but that's the general gist of it I think.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.