在postgresql中运行函数抛出运行时错误

时间:2014-08-09 07:28:17

标签: function postgresql stored-procedures plpgsql

当我尝试运行一个返回表的函数时,我遇到了一个神秘的错误,我的函数代码就像

CREATE OR REPLACE FUNCTION FN_JOURNEY_SUMMARY(IN ENTITY INTEGER,
                                              IN VEHICLE VARCHAR2,
                                              IN SDATE VARCHAR2,
                                              IN EDATE VARCHAR2,
                                              IN FLG VARCHAR,
                                              IN P_IS_DEBUG CHAR DEFAULT 'Y')
RETURNS TABLE(TRNNAME         VARCHAR2(100),
              SASSETID        VARCHAR2(50),
              DTDATE          VARCHAR2(50),
              IDAYS           NUMBER,
              SMOVINGTIME     VARCHAR2(30),
              SSTOPTIME       VARCHAR2(30),
              SDISTANCE       VARCHAR2(60),
              SCUMMDISTANCE   VARCHAR2(60),
              SAVARAGE        VARCHAR2(60),
              ICNTR           NUMBER)
LANGUAGE PLPGSQL 
AS $FUNCTION$
DECLARE

    TM_START            DATE;
    TM_END              DATE;   
    I_ELAPS_TIME        NUMBER;
BEGIN
TM_START:= CLOCK_TIMESTAMP();
    BEGIN
        CREATE TEMPORARY TABLE GTT_V4JOURNEY_SUMM1 (TRN_NAME        VARCHAR2(100),
                                  S_ASSET_ID        VARCHAR2(50),
                                  DT_DATE           VARCHAR2(50),
                                  I_DAYS            NUMBER,
                                  S_MOVING_TIME     VARCHAR2(30),
                                  S_STOP_TIME       VARCHAR2(30),
                                  S_DISTANCE        VARCHAR2(60),
                                  S_CUMM_DISTANCE   VARCHAR2(60),
                                  S_AVARAGE         VARCHAR2(60),
                                  i_cntr            number);
    EXCEPTION
        WHEN OTHERS THEN
        DROP TABLE GTT_V4JOURNEY_SUMM1;
        CREATE TEMPORARY TABLE GTT_V4JOURNEY_SUMM1 (TRN_NAME        VARCHAR2(100),
                                  S_ASSET_ID        VARCHAR2(50),
                                  DT_DATE           VARCHAR2(50),
                                  I_DAYS            NUMBER,
                                  S_MOVING_TIME     VARCHAR2(30),
                                  S_STOP_TIME       VARCHAR2(30),
                                  S_DISTANCE        VARCHAR2(60),
                                  S_CUMM_DISTANCE   VARCHAR2(60),
                                  S_AVARAGE         VARCHAR2(60),
                                  i_cntr            number);

    END;

        INSERT INTO GTT_V4JOURNEY_SUMM1
        SELECT * FROM FN_JOURNEY_SUMM_WEEK(ENTITY,VEHICLE, SDATE,EDATE,P_IS_DEBUG);

IF P_IS_DEBUG = 'Y' THEN
TM_END:=CLOCK_TIMESTAMP();
RAISE NOTICE 'THE START TIME WAS %',TM_START;
RAISE NOTICE 'THE END TIME WAS %',TM_END;
I_ELAPS_TIME:=EXTRACT(MICROSECONDS FROM (TM_END - TM_START))/1000;
RAISE NOTICE 'THE TIME TAKEN IS >>>>>>-------------->>>>>>> %',I_ELAPS_TIME;
END IF;
    RAISE NOTICE '*** end of all ***';
    RETURN QUERY SELECT * FROM GTT_V4JOURNEY_SUMM1 ;

END $FUNCTION$;

当我用:

运行该功能时
select * from FN_JOURNEY_SUMMARY(100,'NL01L0639','28/03/2014','23/06/2014','W',1);

这给我一个错误:

invalid input syntax for type timestamp: "28-Mar:28-Mar-14(13)"

存储在返回表的DTDATE列中,该列为character varying类型。

但是当我使用不同的名称编译相同的函数时,不会抛出任何错误并且它会顺利运行。

1 个答案:

答案 0 :(得分:1)

这是一团糟。它肯定不会像你声称的那样编译。

VARCHAR2NUMBER无效data types in Postgres

TM_STARTTM_END应该是timestamptimestamptz,而不是date

P_IS_DEBUG应为boolean

您的INSERT语句应该有目标列表。