Sql server T-SQL到PostgreSQL转换中的存储过程
我有一个用T-SQL编写的存储过程,我想为PostgreSQL编写,但我对PostgreSQL不太熟悉 我的存储过程如下所示:Sql server T-SQL到PostgreSQL转换中的存储过程,sql-server,postgresql,tsql,stored-procedures,postgresql-9.1,Sql Server,Postgresql,Tsql,Stored Procedures,Postgresql 9.1,我有一个用T-SQL编写的存储过程,我想为PostgreSQL编写,但我对PostgreSQL不太熟悉 我的存储过程如下所示: 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 i
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
我必须这样做,但在第10行我得到一个错误:
错误:整数“293.00”的输入语法无效
SQL状态:22P02
上下文:PL/pgSQL函数
“SA_PRJ”。usp_添加_时间表_记录_新(整数、整数、数字、数字、字符变化、字符变化)第10行分配
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;
在PostgreSQL中,我想要的是更复杂的版本。您没有提供足够的信息来尝试解决您的问题,但是错误消息非常具有描述性。您正在尝试将293.00转换为整数。在这里我可以复制:
DO
$$
DECLARE
i INT;
BEGIN
i := 293.00;
RAISE NOTICE 'i=%', i;
END
$$;
这将提高:
ERROR: invalid input syntax for integer: "293.00"
SQL state: 22P02
Context: PL/pgSQL function inline_code_block line 6 at assignment
您需要将变量更改为与试图分配给它的数据相同的数据类型。例如:
DO
$$
DECLARE
i NUMERIC(5, 2);
BEGIN
i := 293.00;
RAISE NOTICE 'i=%', i;
END
$$;
这项工作和产出:
NOTICE: i=293.00
Query returned successfully with no result in 14 ms.
首先,您必须给出您的PostgreSQL版本、表ddl,以及您到目前为止尝试了什么!?有两个表:1.Tbl_项目2.Tbl_项目时间表