【发布时间】:2016-05-19 19:50:41
【问题描述】:
我想为我的数据库执行完整的数据库子字符串搜索。所以如果一个字段有'hello world,你好吗,我正在寻找如何。结果应显示“hello world [..bla bla bla..]”。此外,我如何添加一个布尔变量,它用于区分大小写或不区分大小写。
我得到了以下代码,但仍然需要上面的东西来集成。有什么推荐吗?
CREATE OR REPLACE FUNCTION search_columns(
needle text,
haystack_tables name[] default '{}',
haystack_schema name[] default '{public}'
)
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(haystack_tables) OR haystack_tables='{}')
AND c.table_schema=ANY(haystack_schema)
AND t.table_type='BASE TABLE'
LOOP
EXECUTE format('SELECT ctid FROM %I.%I WHERE cast(%I as text)=%L',
schemaname,
tablename,
columnname,
needle
) INTO rowctid;
IF rowctid is not null THEN
RETURN NEXT;
END IF;
END LOOP;
END;
$$ language plpgsql;
干杯
【问题讨论】:
标签: postgresql search substring case-insensitive postgresql-9.4