【问题标题】:How do I fix this PostgreSQL 9.1 stored procedure?如何修复这个 PostgreSQL 9.1 存储过程?
【发布时间】:2012-09-13 02:54:32
【问题描述】:

我们的软件有问题,为了解决这个问题,我必须编写一个存储过程,该过程将作为升级安装升级过程的一部分运行。此存储过程需要查找特定表中与特定条件匹配的每一行并更新该行。由于内部原因,必须通过我们专门为插入和更新数据编写的存储过程来完成更新。

这是我为解决此问题而编写的存储过程:

CREATE OR REPLACE FUNCTION FixDataProblem() RETURNS VOID AS $$
DECLARE
    FixCursor   NO SCROLL CURSOR FOR
        SELECT * FROM MyTable WHERE ProblemColumn IN ( '?', 'PR' );
    RowToUpdate         MyTable%ROWTYPE;
BEGIN
    -- Open the cursor
    OPEN FixCursor;

    -- Start a loop
    LOOP
        -- Fetch the next row from thr cursor
        FETCH FixCursor INTO RowToUpdate;

        -- Did we get anything back?
        IF RowToUpdate IS NULL THEN
            -- We didn't. Exit the loop
            EXIT;
        END IF;

        -- Call the UpsertMyTable stored procedure to set the ProblemColumn column to NULL
        SELECT CarSystem.UpsertMyTable( RowToUpdate.RowId,
                               RowToUpdate.ForeignId,
                               RowToUpdate.CountryId,
                               NULL,
                               RowToUpdate.Plate,
                               RowToUpdate.HashedData,
                               RowToUpdate.PlateClassId,
                               RowToUpdate.AlarmClassId,
                               RowToUpdate.BeginDate,
                               RowToUpdate.EndDate,
                               RowToUpdate.ListPriorityId,
                               RowToUpdate.VehicleTypeId,
                               RowToUpdate.MakeId,
                               RowToUpdate.ModelId,
                               RowToUpdate.Year,
                               RowToUpdate.ColorId,
                               RowToUpdate.Notes,
                               RowToUpdate.OfficerNotes,
                               NULL,
                               UUID_GENERATE_V4() );
    END LOOP;

    -- Close the cursor
    CLOSE ListDetailsCursor;
END;
$$ LANGUAGE plpgsql;

这个存储过程很好,但是当我运行它时,我得到:

ERROR:  query has no destination for result data
HINT:  If you want to discard the results of a SELECT, use PERFORM instead.
CONTEXT:  PL/pgSQL function "fixdataproblem" line 22 at SQL statement


********** Error **********

ERROR: query has no destination for result data
SQL state: 42601
Hint: If you want to discard the results of a SELECT, use PERFORM instead.
Context: PL/pgSQL function "fixdataproblem" line 22 at SQL statement

我该如何解决这个问题?我相信我正确地调用了存储过程。我真的不知道这个存储过程有什么问题。

谢谢

托尼

【问题讨论】:

  • 您为什么不按照错误消息中的建议进行操作?

标签: postgresql stored-procedures postgresql-9.1


【解决方案1】:

上面写着:

ERROR:  query has no destination for result data
HINT:  If you want to discard the results of a SELECT, use PERFORM instead.
CONTEXT:  PL/pgSQL function "fixdataproblem" line 22 at SQL statement

在第 22 行:

    -- Call the UpsertMyTable stored procedure to set the ProblemColumn column to NULL
    SELECT CarSystem.UpsertMyTable( RowToUpdate.RowId,
    ...

将其从 SELECT 更改为 PERFORM。请参阅PERFORM 了解原因。

【讨论】:

  • 每次我调用存储过程时,它总是在 SELECT 语句中。当我尝试使用PERFORM 调用FixDataProblem 存储过程时,出现语法错误。我未能建立的连接是需要PERFORM 来调用UpsertMyTable 存储过程。谢谢
猜你喜欢
  • 2015-06-13
  • 2012-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多