1
votes

I have a stored procedure writen in T-SQL and I want to make it for PostgreSQL but I'm not so familiar with PostgreSQL.

My stored procedure look like this:

CREATE PROCEDURE usp_insert_allocated_time
@fld_project_id INT,
@fld_allocated_time INT
AS

DECLARE @project int;
SET @project = @fld_project_id;

DECLARE @allocated int;
DECLARE @time int;

BEGIN

SET @time = (SELECT SUM(fld_allocated_time)
FROM dbo.tbl_project_timesheet
WHERE fld_project_id =@project)

SET @allocated = (SELECT fld_allocated_days FROM dbo.tbl_project where fld_id = @project);

    IF @allocated > @time 
    BEGIN 

    INSERT into dbo.tbl_project_timesheet(fld_project_id,fld_allocated_time)
    VALUES(@fld_project_id,@fld_allocated_time);

    END
      ELSE
      PRINT 'Not OK';

END

And I have to do something like this, but on line 10 I get this error:

ERROR: invalid input syntax for integer: "293.00"

SQL state: 22P02

Context: PL/pgSQL function "SA_PRJ".usp_add_timesheet_record_new(integer,integer,numeric,numeric,character varying,character varying) line 10 at assignment

CREATE OR REPLACE FUNCTION "SA_PRJ".usp_add_timesheet_record_new(p_uid integer, p_project_id integer, p_allocated_time numeric, p_achieved_time numeric, p_task_desc character varying, p_obs character varying)
  RETURNS void AS
$BODY$
declare alloc_id integer;
declare project integer;
declare allocated integer;
declare allocated_time integer;
BEGIN

    project := p_project_id;

    allocated_time := (SELECT SUM(fld_allocated_time)
    FROM "SD_PRJ".tbl_project_timesheet
    WHERE fld_project_id = project);

    allocated := (SELECT fld_allocated_days FROM "SD_PRJ".tbl_project where fld_id = project);

    if not "SA_ADM".usp_check_permission(p_uid, 'SA_PRJ', 'usp_add_timesheet_record') then
    raise exception 'User ID % no have the permission!', p_uid;
    end if;

    select fld_id into alloc_id from "SD_PRJ".tbl_project_allocation where fld_emp_id = p_uid and fld_project_id = p_project_id;

    BEGIN
    IF (allocated > allocated_time) 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
        RAISE NOTICE 'Not OK!!';
    END IF;
    END;
END
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

It's more complex version in PostgreSQL for what I want.

1
At first, you have to give your PostgreSQL version, your table ddl, and what did you try till now!? - Houari
There are two tables: 1.Tbl_project 2.Tbl_project_timesheet - Alienware

1 Answers

1
votes

You don't really give enough information to try and fix your problem, but the error message is pretty descriptive. You are trying to put 293.00 into an integer. Here I can reproduce:

DO
$$
DECLARE
    i INT;
BEGIN
    i := 293.00;
    RAISE NOTICE 'i=%', i;
END
$$;

This will raise:

ERROR: invalid input syntax for integer: "293.00"
SQL state: 22P02
Context: PL/pgSQL function inline_code_block line 6 at assignment

You need to change your variable to the same datatype as the data you are trying to assign to it. For example:

DO
$$
DECLARE
    i NUMERIC(5, 2);
BEGIN
    i := 293.00;
    RAISE NOTICE 'i=%', i;
END
$$;

This works and outputs:

NOTICE:  i=293.00
Query returned successfully with no result in 14 ms.