plpgsql CREATE FUNCTION语法错误,位于"CREATE"或附近 [英] plpgsql CREATE FUNCTION syntax error at or near 'CREATE'
问题描述
即使我清空所有的DECLARE块和BEGIN-END块并尝试返回一个字符串,似乎也无法使该函数创建.有人可以帮我解决我在这里做错的事吗?
Can't seem to get this function to Create, even when I empty out all the DECLARE block and the BEGIN-END block and just try to return a string. Can anyone help me with what I've done wrong here?
尝试在pgAdminIII(Ubuntu)中执行此操作:
Trying to execute this in pgAdminIII (Ubuntu):
CREATE OR REPLACE FUNCTION split_country()
RETURNS text as $$
DECLARE
titlecsv text;
arCountries text[];
country_cursor CURSOR
FOR SELECT DISTINCT country
FROM festfriend.films;
BEGIN
OPEN country_cursor;
LOOP
FETCH NEXT FROM country_cursor INTO titlecsv;
EXIT WHEN NOT FOUND;
SELECT regexp_split_to_array(titlecsv, ',') INTO arCountries;
RAISE NOTICE '%1', arCountries;
INSERT INTO festfriend.country (name, acronym)
SELECT trim(both ' ' from a.column2), upper(left(trim(both ' ' from a.column2), 3))
FROM unnest((SELECT arCountries)::text[]) WITH ORDINALITY a(column2)
WHERE (SELECT COUNT(id) FROM festfriend.country WHERE name = trim(both ' ' from a.column2)) = 0
AND char_length(trim(both ' ' from a.column2)) > 0;
END LOOP;
CLOSE country_cursor;
RETURN 'Split Countries, yo!';
END; $$
LANGUAGE 'plpgsql';
出现以下错误:
ERROR: syntax error at or near "CREATE"
LINE 1: CREATE OR REPLACE FUNCTION split_country()
^
********** Error **********
ERROR: syntax error at or near "CREATE"
SQL state: 42601
Character: 1
推荐答案
您的文件可能以UTF-8编码,且带有字节顺序标记(BOM).这些不可见的数据将出现在文件中,位于您的CREATE
之前,足以混淆Postgres并给出此误导性错误消息.
Your file may be encoded in UTF-8 with a byte-order mark (BOM). This invisible data will appear in the file just before your CREATE
and is enough to confuse Postgres and give this misleading error message.
尝试在Notepad ++中打开文件,然后在Encoding
菜单下选择Encode in UTF-8
.
Try opening the file in Notepad++ and selecting Encode in UTF-8
under the Encoding
menu.
这篇关于plpgsql CREATE FUNCTION语法错误,位于"CREATE"或附近的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!