7

我有一个非常简单的 PL/pgSQL 脚本:

     declare x varchar(100);

当我运行它时,我收到一条消息:

    [WARNING  ] declare x varchar(100)
        ERROR:  syntax error at or near "varchar"
        LINE 1: declare x varchar(100)
                          ^

我真的不明白这有什么问题。

4

3 回答 3

22

您只能在 PostgreSQL 的函数体内使用过程语句。

CREATE OR REPLACE FUNCTION foo()
RETURNS int AS 
$$ -- here start procedural part
   DECLARE x int;
   BEGIN
     x := 10;
     RETURN x;
   END;
$$ -- here finish procedural part
LANGUAGE plpgsql; -- language specification 

或在临时功能中(匿名块)

DO $$
DECLARE x int;
BEGIN
  x := 10;
  RAISE NOTICE '>>>%<<<', x;
END;
$$;

不可能将过程语句用作 SQL 语句,如 T-SQL。

于 2013-02-20T06:46:24.760 回答
0

使用示例

DO $$
Declare
  test varchar;
begin
   test := 'teste';
   if (char_length(test) > 0) then
      RAISE NOTICE '>>>%<<<', test;
   end if;
end;
$$;
于 2013-02-20T19:38:24.340 回答
0

我已经通过以下代码解决了这个问题:

do $$
declare InvestigationTypeId int;
declare var_STDStatusMasterId int;
begin
   InvestigationTypeId := null;
   var_STDStatusMasterId := null;
end;
$$;
于 2021-08-04T10:53:36.237 回答