【问题标题】:Stored procedure in T-SQL transform to PostgreSQLT-SQL 中的存储过程转换为 PostgreSQL
【发布时间】:2014-11-12 16:03:26
【问题描述】:

我有一个用 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 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_add_timesheet_record_new(integer,integer,numeric,numeric,character varying,character varying) 分配时的第 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 中更复杂的版本。

【问题讨论】:

  • 一开始你得给你的PostgreSQL版本,你的table ddl,你到现在都试过什么!?
  • 有两个表:1.Tbl_project 2.Tbl_project_timesheet

标签: sql-server postgresql tsql stored-procedures postgresql-9.1


【解决方案1】:

您并没有真正提供足够的信息来尝试解决您的问题,但是错误消息非常具有描述性。您正在尝试将 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.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-11
    • 2021-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多