在创建'

时间:2016-06-15 05:34:21

标签: postgresql plpgsql

似乎无法将此函数设置为Create,即使我清空所有DECLARE块和BEGIN-END块并尝试返回一个字符串。谁能帮我解决我在这里做错了什么?

尝试在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

2 个答案:

答案 0 :(得分:1)

如果您通过“新功能...”屏幕创建功能,选项卡“代码”,则必须仅输入两个c[i]之间的部分(即从{{1}开始以$$结尾。参数,返回类型等在其他选项卡中定义。

答案 1 :(得分:0)

您的文件可以使用字节序标记(BOM)的UTF-8编码。这些不可见的数据将出现在文件CREATE的前面,足以使Postgres迷惑并给出此误导性错误消息。

尝试在Notepad ++中打开文件,然后在Encode in UTF-8菜单下选择Encoding

enter image description here