我正在尝试通过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;
答案 0 :(得分:0)