我有一个非常简单的PL/pgSQL脚本:
declare x varchar(100);
当我运行它时,会出现以下提示信息:
[WARNING ] declare x varchar(100)
ERROR: syntax error at or near "varchar"
LINE 1: declare x varchar(100)
^
我真的不明白这个有什么问题。
我有一个非常简单的PL/pgSQL脚本:
declare x varchar(100);
[WARNING ] declare x varchar(100)
ERROR: syntax error at or near "varchar"
LINE 1: declare x varchar(100)
^
在PostgreSQL中,您只能在函数体内使用过程语句。
Translated:you can use procedural statements only inside function body in 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;
$$;
不能像T-SQL那样将过程性语句用作SQL语句。
do $$
declare TypeId int;
declare MasterId int;
begin
TypeId := null;
MasterId := null;
end;
$$;
使用示例
DO $$
Declare
test varchar;
begin
test := 'teste';
if (char_length(test) > 0) then
RAISE NOTICE '>>>%<<<', test;
end if;
end;
$$;