Amazon Redshift创建功能错误:42601:“默认”或附近的语法错误

时间:2019-09-20 09:00:56

标签: amazon-redshift plpgsql stored-functions

我正在尝试通过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;

1 个答案:

答案 0 :(得分:0)