Amazon Redshift CREATE FUNCTION ERROR: 42601: 在“默认”或接近“默认”时出现语法错误

Posted

技术标签:

【中文标题】Amazon Redshift CREATE FUNCTION ERROR: 42601: 在“默认”或接近“默认”时出现语法错误【英文标题】:Amazon Redshift CREATE FUNCTION ERROR: 42601: syntax error at or near "default" 【发布时间】:2019-09-20 09:00:56 【问题描述】:

我正在尝试通过 Aginity (v4.9.1.2686) 在 Redshift 中创建一个函数,但出现以下错误:

错误:42601:“默认”或接近“默认”的语法错误

功能代码:

CREATE OR REPLACE FUNCTION search_columns (
    searchfield text,
    searchtables name[] default '',
    searchschema name[] default '')
RETURNS table(schemaname text, tablename text, columnname text, rowctid text)
AS $$
begin
    FOR schemaname, tablename, columnname IN
        SELECT c.table_schema, c.table_name, c.column_name
        FROM information_schema.columns c
        JOIN information_schema.tables t ON (t.table_name = c.table_name AND t.table_schema = c.table_schema)
        WHERE (c.table_name=ANY(searchtables) OR searchtables='')
        AND (c.table_schema=ANY(searchschema) OR searchschema='')
        AND t.table_type='BASE TABLE'
    LOOP
        EXECUTE format('SELECT ctid FROM %I.%I WHERE cast(%I as text)=%L', schemaname, tablename, columnname, searchfield)
        INTO rowctid;
    IF rowctid IS NOT NULL 
        THEN RETURN NEXT;
    END IF;
    END LOOP;
END;
$$ language plpgsql;

编辑

尝试删除默认值并得到不同的错误:

错误:42601:“表”处或附近的语法错误

CREATE OR REPLACE FUNCTION search_columns (
    searchfield text,
    searchtables name[] ,
    searchschema name[] )
RETURNS table(schemaname text, tablename text, columnname text, rowctid text)
AS $$
begin
    FOR schemaname, tablename, columnname IN
        SELECT c.table_schema, c.table_name, c.column_name
        FROM information_schema.columns c
        JOIN information_schema.tables t ON (t.table_name = c.table_name AND t.table_schema = c.table_schema)
        WHERE (c.table_name=ANY(searchtables) OR searchtables='')
        AND (c.table_schema=ANY(searchschema) OR searchschema='')
        AND t.table_type = 'BASE TABLE'
    LOOP
        EXECUTE format('SELECT ctid FROM %I.%I WHERE cast(%I as text)=%L', schemaname, tablename, columnname, searchfield)
        INTO rowctid;
    IF rowctid IS NOT NULL 
        THEN RETURN NEXT;
    END IF;
    END LOOP;
END;
$$ language plpgsql;

【问题讨论】:

Redshift 不支持参数的默认值:docs.aws.amazon.com/redshift/latest/dg/r_CREATE_FUNCTION.html @a_horse_with_no_name 有没有办法解决这个问题? @a_horse_with_no_name 或者我可以删除默认值吗?还是用别的? Redshift 也不支持返回集合的函数:docs.aws.amazon.com/redshift/latest/dg/… @a_horse_with_no_name Redshift 中是否有这样的功能可以工作? 【参考方案1】:

Redshift 也不支持返回集合的函数:https://docs.aws.amazon.com/redshift/latest/dg/c_unsupported-postgresql-features.html

【讨论】:

以上是关于Amazon Redshift CREATE FUNCTION ERROR: 42601: 在“默认”或接近“默认”时出现语法错误的主要内容,如果未能解决你的问题,请参考以下文章

Amazon Redshift 中的用户创建时间戳

Amazon EMR 与 Amazon Redshift

amazon-redshift 中的 CONCAT 函数

Amazon Redshift 按特定值排序

不使用 jdbc 驱动程序查询 Amazon Redshift

与 Amazon Redshift 同步